首页 > 解决方案 > react-native蓝牙设备不断断开连接

问题描述

我正在构建一个需要与蓝牙设备通信的 react-native 应用程序(目前仅适用于 Android)。我正在使用这个蓝牙

我可以成功扫描设备,当我尝试连接时问题开始:等待几秒钟后我收到错误

设备 MAC_ADDRESS 已断开连接

这是代码:

bluetoothInstance.startDeviceScan(null, null, (error, device) => {
    console.log('start scanning');
if (error) {
  console.log(error);
  return
} else {
  if (device.id = DEVICE_ID) {
    console.log('device found');

    console.log('stop scanning');
    bluetoothInstance.stopDeviceScan();

    console.log('connecting...');
    device.connect()
      .then((device) => {
        console.log("Discovering services and characteristics");
        return device.discoverAllServicesAndCharacteristics()
      })
      .then((device) => {
        console.log("Setting notifications");
        return device;
      })
      .then(() => {
        console.log("Listening...");
      }, (error) => {
        console.log(error.message);
      })
  }
}
});

console.log连接到设备后我看不到任何打印。我没有阅读“发现服务和特征”,也没有阅读“设置通知”,也没有阅读“正在侦听...”,只是对应于该行的“设备 MAC_ADDRESS 已断开连接” console.log(error.message)

我究竟做错了什么?

标签: javascriptreact-nativebluetooth

解决方案


推荐阅读