首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bleManager.startDeviceScan在iOS react-native-ble-plx上不起作用

bleManager.startDeviceScan在iOS react-native-ble-plx上不起作用
EN

Stack Overflow用户
提问于 2021-06-08 16:59:22
回答 1查看 362关注 0票数 0

我正在尝试使用react- native -ble-plx配对一个网关来反应本机应用程序。下面的源代码在Android中运行良好,而在iOS中,bleManager.startDeviceScan()不会被触发。在这一步之后什么也不会发生。

任何帮助都是非常感谢的!

源代码:

代码语言:javascript
复制
const connectBLE = () => {
    const subscription = bleManager.onStateChange(async (state) => {
      if (state === 'PoweredOn') {
          subscription.remove();
          scanAndConnect();
      } 
  };
}
  const scanAndConnect = () => {
    bleManager.startDeviceScan(null, null, async (error, device) => {
      if (error) {
        showToast(error, 'error');
        console.log('Handle error - scanning will be stopped automatically');
        return;
      }
      console.log('Devices');
      console.log(device.name);
      // Check if it is a device you are looking for based on device name
      if (device.name === "BLE_0006") {
        // Stop scanning as we have found the device.
        bleManager.stopDeviceScan();
        // Establish Device connection.
        device
          .connect()
          .then((deviceData) => {
            /** Show Toast on Device disconnect */
            bleManager.onDeviceDisconnected(
              deviceData.id,
              (connectionError, connectionData) => {
                if (connectionError) {
                  console.log(connectionError);
                }
                console.log('Device is disconnected');
                console.log(connectionData);
              },
            );
            /** Discover All Services and Characteristics */
            return device.discoverAllServicesAndCharacteristics();
          })
          .then(async (deviceObject) => {
            console.log('deviceObject');
            console.log(deviceObject);
            /** Subscribe for the Readable service */
            device.monitorCharacteristicForService(
              Enum.bleConnectionInfo.customServiceUUID,
              Enum.bleConnectionInfo.readCharacteristicUUID,
              (error, characteristic) => {
                if (error) {
                  console.log('Error in monitorCharacteristicForService');
                  console.log(error.message);
                  return;
                }
                console.log(characteristic.uuid, characteristic.value);
                ]);
              },
            );
          })
          .catch((error) => {
            console.warn(error);
            showToast(error, 'error');
          });
      }
    });
  }
EN

回答 1

Stack Overflow用户

发布于 2021-06-14 19:27:53

这可能会对某些人有帮助!

已通过将bleManager()初始化移出功能组件解决此问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67884535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档