问题描述
我正在尝试实现我的应用程序的蓝牙服务。但是当扫描盯着我的应用程序时,没有显示移动蓝牙和蓝牙耳机蓝牙。
请帮忙解决这个问题。
import React,{ Component } from 'react';
import { NativeModules,NativeEventEmitter,View,Text } from 'react-native';
import BleManager from 'react-native-ble-manager';
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
export default class App extends Component{
state = {
peripherals: new Map(),};
componentDidMount() {
BleManager.start({ showAlert: false })
this.handlerdiscover = bleManagerEmitter.addListener(
'BleManagerdiscoverPeripheral',this.handlediscoverPeripheral
);
this.handlerStop = bleManagerEmitter.addListener(
'BleManagerStopScan',this.handleStopScan
);
this.scanForDevices(); // I chose to start scanning for devices here
}
scanForDevices() {
BleManager.scan([],30);
}
handlediscoverPeripheral = (peripheral) => {
console.log('Got ble peripheral',peripheral);
const { peripherals } = this.state;
if (peripheral.name) {
peripherals.set(peripheral.id,peripheral.name);
}
this.setState({ peripherals });
};
handleStopScan = () => {
console.log('Scan is stopped. Devices: ',this.state.peripherals);
}
render(){
return(
<View>
<Text>Bluetooth</Text>
</View>
)
}
}
解决方法
react-native-ble-manager 可让您的手机与低功耗蓝牙 (BLE) 设备之间进行通信。 BLE 设备需要通告它们的存在才能被检测到。请记住,BLE 与蓝牙经典不同。
您只能使用 react-native-ble-manager 检测 BLE 设备。如果您找不到您的设备,它们可能不是 BLE 设备,而是蓝牙经典设备。其他手机通常不宣传 BLE 服务,需要应用程序才能这样做。蓝牙耳机可能会使用 BLE 来实现某些功能,但音频传输由蓝牙经典处理。
您可以使用通用 BLE 扫描仪应用(例如 nRF Connect)来扫描附近的 BLE 设备。该应用程序甚至允许您在另一部手机上启动 GATT 服务器,以便使用 BLE 进行访问。