React Native - 检查蓝牙是否打开,如果没有提醒用户?
React Native - Check if Bluetooth is turned on, and if not alert the user?
有没有办法检查蓝牙是否打开,如果没有,然后提醒用户?谢谢!
您将必须创建自己的 BT 模块并将其作为组件导入。
本机模块必须包含
#import <CoreBluetooth/CoreBluetooth.h>
查看BT状态
// This delegate will monitor for any changes in bluetooth state
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(_bluetoothManager.state)
{
case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
default: stateString = @"State unknown, update imminent."; break;
}
//stateString
//do the react native thing and send the stateStringback or w/e you want. Maybe send an alert.
}
就是这样。我不会详细介绍如何创建本机组件,网上有很多教程。
或者当状态改变时,您可以调用 UIAlertView
委托,该委托将允许您向用户发送警报。
试试这个:
安装:npm install react-native-bluetooth-status --save
Link 这个插件:react-native link react-native-bluetooth-status
导入:import { BluetoothStatus } from 'react-native-bluetooth-status';
检查蓝牙打开或关闭代码:
BluetoothStatus.state() 是 return 布尔值。如果 return true 蓝牙是 ON 如果 return false 蓝牙是 OFF
componentDidMount()
{
this.checkInitialBluetoothState();
}
async checkInitialBluetoothState()
{
const isEnabled = await BluetoothStatus.state();
console.log("check bluetooth on or off", isEnabled);
if(isEnabled == true){
Alert.alert(
'Bluethooth',
'Bluetooth is turn on'
);
} else{
Alert.alert(
'Bluethooth',
'Bluetooth is turn off'
);
}
}
有没有办法检查蓝牙是否打开,如果没有,然后提醒用户?谢谢!
您将必须创建自己的 BT 模块并将其作为组件导入。
本机模块必须包含
#import <CoreBluetooth/CoreBluetooth.h>
查看BT状态
// This delegate will monitor for any changes in bluetooth state
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(_bluetoothManager.state)
{
case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
default: stateString = @"State unknown, update imminent."; break;
}
//stateString
//do the react native thing and send the stateStringback or w/e you want. Maybe send an alert.
}
就是这样。我不会详细介绍如何创建本机组件,网上有很多教程。
或者当状态改变时,您可以调用 UIAlertView
委托,该委托将允许您向用户发送警报。
试试这个:
安装:npm install react-native-bluetooth-status --save
Link 这个插件:react-native link react-native-bluetooth-status
导入:import { BluetoothStatus } from 'react-native-bluetooth-status';
检查蓝牙打开或关闭代码:
BluetoothStatus.state() 是 return 布尔值。如果 return true 蓝牙是 ON 如果 return false 蓝牙是 OFF
componentDidMount()
{
this.checkInitialBluetoothState();
}
async checkInitialBluetoothState()
{
const isEnabled = await BluetoothStatus.state();
console.log("check bluetooth on or off", isEnabled);
if(isEnabled == true){
Alert.alert(
'Bluethooth',
'Bluetooth is turn on'
);
} else{
Alert.alert(
'Bluethooth',
'Bluetooth is turn off'
);
}
}