我正在尝试检查设备的蓝牙是打开还是关闭。这是我到目前为止编写的代码
CBCentralManager *cbManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
[cbManager scanForPeripheralsWithServices:nil options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
if (cbManager.state==CBCentralManagerStatePoweredOff)
{
//do stuff
}
- (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;
}
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state"
message:stateString
delegate:nil
cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease];
[alert show];
}
问题是,无论设备上的蓝牙是否打开,管理器的状态始终为“未知”。 关于为什么会发生这种情况的任何想法?
答案 0 :(得分:3)
两件事 -
您的CBCentralManager
应该是属性,否则会在退出时将其初始化后立即释放
在您处于开机状态之前,您不应该致电scanForPeripheralsWithServices
。
@property (strong,nonatomic) CBCentralManager *cbManager;
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(central.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.";
[central scanForPeripheralsWithServices:nil options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
break;
default:
stateString = @"State unknown, update imminent.";
break;
}
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state"
message:stateString
delegate:nil
cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease];
[alert show];
}
答案 1 :(得分:-1)
方法
- (无效)centralManagerDidUpdateState(CBCentralManager *)中央
被调用。试着实现这个
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { // Possible states that the central manager can return. char *managerStrings[] = {"Unknown Error", "Resetting", "Unsupported Device", "Unauthorized", "Bluetooth is deactivated", "PoweredOn"}; NSString *managerString = [NSString stringWithFormat:@"%s", managerStrings[central.state]]; // the search is enabled if everything is ok if ([managerString isEqualToString:@"PoweredOn"]) { //Do something } }
答案 2 :(得分:-1)
从Sierra,Xcode 8,Swift 3升级到High Sierra,Xcode 9,Swift 4后,我遇到了同样的问题。
解决方案1 :(仅用于测试)在init和扫描开始之间等待第二位
var scanner = BLESensorScanner(sensorName: sensorName);
sleep(1);
DispatchQueue.global(qos: .userInteractive).async {
if (scanner.isPoweredOn()) {
DBG("Bluetooth is powered on");
scanner.startScan();
}
else {
DBG("Bluetooth is not powered");
exit(-1);
}
}
注意:BLESensorScanner
实施NSObject
,CBCentralManagerDelegate
,CBPeripheralDelegate
。 sensorName
是蓝牙设备的名称
解决方案2:使用事件处理程序等待状态变为poweredOn
public func centralManagerDidUpdateState(_ central: CBCentralManager) {
DBG("CBCentralManager.centralManagerDidUpdateState");
DBGI("state=\(toString(central.state))");
if (state == CBManagerState.poweredOn) {
scanner.startScan();
}
}