我想使用 CoreBluetooth(因为我的应用程序必须符合 App Store 的条件)来检查所有当前配对和连接的蓝牙设备。这应该不需要任何蓝牙扫描,对吧?我只是想看看系统与什么配对。如果我做不到,第二个选择是扫描。
我正在尝试的方法不起作用。它说蓝牙未通电并崩溃,但 CBCentralManager 的状态为开启!关于如何解决这个问题的任何想法,还是我完全偏离了轨道?
现在所有这些都在 ViewController 中:
- (void)viewDidLoad{
[super viewDidLoad];
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
[self.centralManager retrieveConnectedPeripherals]; //makes the system call didRetrieveConnectedPeripherals
}
- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{
NSLog(@"didRetrieveConnectedPeripherals called");
for (CBPeripheral *a in peripherals){
NSLog(a.name); //just log the name for now to see if it recognized it
} //but it never ends up logging anything, and I have a BT keyboard paired/connected with the iPhone 5
} //and I get an error at some point, <CBConcreteCentralManager: 0x71ab020> is not powered on
- (void)centralManagerDidUpdateState:(CBCentralManager *)manager{
if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn"); //this is what gets logged when I run it on an iPhone 5
if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");
}