我正在创建一个应用程序,检查手机是否连接到任何启用蓝牙的设备,如果连接了,那么我想执行一些操作。
我尝试过下面列出的许多库
这些库中没有一个提供设备是否连接。如果它们有方法isConnected,则如果设备是否连接,则总是返回false。为了测试,我用了蓝牙耳机。
发布于 2019-04-27 11:38:21
您也可以使用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是否是蓝牙。(只在android上工作)
发布于 2019-09-03 22:30:22
使用react-本机-ble,您可以将侦听器添加到蓝牙状态:
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。
https://stackoverflow.com/questions/55866641
复制相似问题