当我的iOS应用打开时,它会显示本地蓝牙设备的列表,然后单击一个。如果蓝牙已关闭,则会出现系统警报,要求您将其打开(一个按钮转到“设置”,另一个按钮单击“确定”);如果您打开设置,则不会再次扫描,您必须
蓝牙打开时,如何注册一种运行方法的通知?
我看到了一些有关“ BluetoothAvailabilityChangedNotification”的信息,但是当打开/关闭蓝牙时似乎没有被调用。
这是我尝试的代码:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(bluetoothAvailabilityChanged:)
name:@"BluetoothAvailabilityChangedNotification"
object:nil];
...
-(void) bluetoothAvailabilityChanged: (NSNotification*) notification {
NSLog(@"Bluetooth changed %@",notification.description);
}
最佳答案
要获取蓝牙状态更改指示,可以尝试使用CoreBluetooth lib
1.将CoreBluetooth.framework链接到您的项目
2.您的.h文件中的#import <CoreBluetooth/CoreBluetooth.h>
3.实施CBCentralManagerDelegate
4.以自己为委托人创建经理CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
5.只要需要更新,就将其保留为属性:self.centralManager = centralManager;
6.实现此方法:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if ([central state] == CBCentralManagerStatePoweredOff) {
NSLog(@"Bluetooth off");
}
else if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(@"Bluetooth on");
}
}
现在尝试启用/禁用蓝牙并查看控制台以获取日志