如何在 react native 中检查设备是否已连接到蓝牙? [英] How can I check whether the device is connected to bluetooth or not in react native?

查看:43
本文介绍了如何在 react native 中检查设备是否已连接到蓝牙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating an app in that checking the mobile is connected to any Bluetooth enabled device if it's connected then I want to perform some action.

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

I've tried many libraries listed below

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

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

None of these libraries are providing whether the device is connected or not. If they have method isConnected, they always return false if the device is connected or not. For testing, I'm using a bluetooth headset.

推荐答案

您也可以使用 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 是否为蓝牙.(仅适用于安卓)

you can check whether the data.type is bluetooth or not. (Only works on android)

这篇关于如何在 react native 中检查设备是否已连接到蓝牙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆