有一种方法可以检查蓝牙是否已打开,如果没有,则提醒用户?谢谢!
答案 0 :(得分:1)
尝试一下:
安装:npm install react-native-bluetooth-status --save
链接此插件:react-native link react-native-bluetooth-status
导入:import { BluetoothStatus } from 'react-native-bluetooth-status';
检查蓝牙打开或关闭代码:
BluetoothStatus.state()返回boolean。如果返回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'
);
}
}
答案 1 :(得分:0)
您必须创建自己的BT模块并将其作为组件导入。
Native模块必须包含
#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
委托,此委托将允许您将警报发送给用户。