【问题标题】:Can't stop scanning and connect to device via react native ble manager无法通过本机 ble 管理器停止扫描并连接到设备
【发布时间】:2021-07-15 12:08:40
【问题描述】:

React Native 应用程序与 BLE 管理器用于与 BLE 设备交互。

我可以扫描设备,默认运行 10 秒。

预期的行动, 当我找到一个设备并单击连接按钮 [在同一页面上,正在扫描设备的位置],我需要停止进一步扫描并启动连接。

问题是, 它在扫描期间没有响应按钮操作。只有在给定时间完成扫描后,才会发起连接请求。

下面给出了代码 sn-p,当点击连接按钮时。

    try {
      console.log('connect....');
      await BleManager.stopScan();
      setIsScanning(false);
      await connectBle(peripheral.id);
      Alert.alert(
        'Alert Title',
        `Successfully Connected to ${peripheral.name}`,
        [
          {
            text: 'OK',
            onPress: () => handleConnected(peripheral),
          },
        ],
      );
      getConnectedPeripherals();
      var newPeripherals1 = newPeripherals.filter(n => n.id !== peripheral.id);
      console.log('newPeripherals1111', newPeripherals1);
      setNewPeripherals(newPeripherals1);
    } catch (error) {
      Alert.alert(
        'Alert Title',
        `Successfully Connected to ${peripheral.name}`,
        [
          {
            text: 'OK',
            onPress: () => {},
            // navigation.navigate('Configuration', {
            //   peripheral: peripheral,
            // }),
          },
        ],
      );
    }
  }```

【问题讨论】:

    标签: react-native react-native-ble-plx react-native-ble-manager


    【解决方案1】:

    参考这个例子:

    https://github.com/innoveit/react-native-ble-manager/blob/master/example/App.js
    

    在此示例中,您可以连接和断开 ble 设备。

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2017-06-24
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多