React native React Native-检查蓝牙是否已打开,如果未打开,是否提醒用户?
有一种方法可以检查蓝牙是否已打开,如果未打开,则通知用户?谢谢大家! 您必须创建自己的BT模块并将其作为组件导入 本机模块必须包括React native React Native-检查蓝牙是否已打开,如果未打开,是否提醒用户?,react-native,react-native-android,react-native-ios,React Native,React Native Android,React Native Ios,有一种方法可以检查蓝牙是否已打开,如果未打开,则通知用户?谢谢大家! 您必须创建自己的BT模块并将其作为组件导入 本机模块必须包括 #导入 检查BT的状态 // This delegate will monitor for any changes in bluetooth state - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString *stateString = nil;
#导入
检查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安装反应本机蓝牙状态--保存
链接此插件:react native Link react native bluetooth status
导入:从'react native bluetooth status'导入{BluetoothStatus}代码>
检查蓝牙开启或关闭代码:
BluetoothStatus.state()是返回布尔值。如果返回true,蓝牙将打开如果返回false,蓝牙将关闭
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'
);
}
}
谢谢!我会调查的!不幸的是,使用这种方法,我一直收到“false”事件,即在“常规设置”和“应用程序设置”中打开蓝牙时。@GrzegorzPawlik您是否正确地响应了本机蓝牙状态链接?是的,我确实按照RN 0.60+文档中的指示,将其链接到您希望在ios/文件夹中运行pod安装的自动链接。