当我的iOS应用程序打开时,它会显示本地蓝牙设备列表,然后单击一个。如果蓝牙已关闭,则会出现一个系统警报,要求您打开它(使用按钮转到“设置”,另一个按钮单击“确定”);如果您打开设置,它不会再次扫描,您必须
如何在蓝牙开启时注册一种运行方法的通知?
我看到了一些关于“BluetoothAvailabilityChangedNotification”的内容,但是当打开/关闭蓝牙时似乎没有调用它。
这是我尝试过的代码:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(bluetoothAvailabilityChanged:)
name:@"BluetoothAvailabilityChangedNotification"
object:nil];
...
-(void) bluetoothAvailabilityChanged: (NSNotification*) notification {
NSLog(@"Bluetooth changed %@",notification.description);
}
答案 0 :(得分:7)
获取蓝牙状态更改指示,您可以尝试使用CoreBluetooth lib
1.将CoreBluetooth.framework链接到您的项目
2.。#import <CoreBluetooth/CoreBluetooth.h>
在您的.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");
}
}
现在尝试启用/禁用蓝牙并查看控制台以获取日志
答案 1 :(得分:0)
实施CBCentralManagerDelegate协议。这是监控蓝牙的唯一方法,
答案 2 :(得分:-1)
尝试:@"BluetoothConnectabilityChangedNotification"
我继承了注册这些通知的代码:
BluetoothAvailabilityChangedNotification
BluetoothConnectabilityChangedNotification
BluetoothPowerChangedNotification
并为每个人调用应用程序。
我不知道定义的来源。底层实现是发布这些。