0

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

我能够扫描设备,默认运行 10 秒。

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

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

当点击连接按钮时,代码片段如下。

    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,
            // }),
          },
        ],
      );
    }
  }```
4

1 回答 1

0

参考这个例子:

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

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

于 2021-07-15T13:51:13.787 回答