【问题标题】:How can I check whether the device is connected to bluetooth or not in react native?如何在本机反应中检查设备是否连接到蓝牙?
【发布时间】:2019-04-26 11:27:24
【问题描述】:

我正在创建一个应用程序,用于检查移动设备是否连接到任何支持蓝牙的设备,如果它已连接,那么我想执行一些操作。

我尝试了下面列出的许多库

  1. react-native-ble-manager
  2. react-native-ble-plx
  3. react-native-bluetooth-serial
  4. react-native-bluetooth-serial-next 等等。

这些库都没有提供设备是否已连接。 如果他们有方法 isConnected,无论设备是否连接,他们总是返回 false。为了测试,我使用的是蓝牙耳机。

【问题讨论】:

    标签: react-native npm


    【解决方案1】:

    您也可以使用react-native-netinfo

    https://github.com/react-native-community/react-native-netinfo

    NetInfo.getConnectionInfo().then(data => {
      console.log("Connection type", data.type);
      console.log("Connection effective type", data.effectiveType);
    });
    

    您可以检查data.type 是否为蓝牙。 (仅适用于安卓)

    【讨论】:

      【解决方案2】:

      使用 react-native-ble-plx 可以为蓝牙状态添加监听器:

      const subscription = manager.onStateChange((state) => {
          console.log("Bluetooth state: ", state);
          if (state == 'PoweredOn') {
              perform_bluetooth_operations();
          }
      }, true);
      
      
      perform_bluetooth_operations () {
          // this is where you will begin bluetooth operations.
      }
      

      此答案适用于 ios 和 android。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多