我一直在尝试设置一个应用程序,使设备既可以扫描外围设备,又可以作为外围设备进行广告推送。目标是通过蓝牙发现,当两个设备靠近时在后台唤醒它们。根据苹果文档,似乎你应该能够在后台运行BLE(启用蓝牙中心和蓝牙外围后台模式),并且当一个设备在前台时,我的应用程序可以正常工作。 首先,我这样广告推送数据:
NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",
CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:identifier]]};
// Start advertising over BLE
[peripheralManager startAdvertising:advertisingData];
然后我将设备设置为扫描数据:
NSArray *services = @[[CBUUID UUIDWithString:identifier]];
[centralManager scanForPeripheralsWithServices:services options:nil];
然而,当两者进入后台(设备必须被锁定)时,蓝牙无法被发现。- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
这个方法在任何设备上都没有被调用。我该怎么解决?谢谢。