作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检查设备的蓝牙是打开还是关闭。这是我到目前为止编写的代码
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];
}
问题是无论设备上的蓝牙是否打开,管理器的状态始终为未知。关于为什么会发生这种情况的任何想法?
最佳答案
两件事-
您的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];
}
关于ios - CBManager 状态始终未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25932366/
我正在尝试检查设备的蓝牙是打开还是关闭。这是我到目前为止编写的代码 CBCentralManager *cbManager = [[CBCentralManager alloc] initWit
我正在尝试制作一个 iOS 应用程序,该应用程序使用 CoreBluetooth 库来启动/停止和传输来自配备 HM-10 BLE 4.0 模块的多个惯性测量单元的数据。我只从一个模块连接/写入/读取
我是一名优秀的程序员,十分优秀!