蓝牙LE设备从iOS后台扫描
我正在以背景模式扫描BLE。
问题在后台扫描中不起作用。 它在前景模式下工作得非常好。
下面是几个代码行。
dispatch_queue_t centralQueue = dispatch_queue_create("com.XXXXX.BLEback", DISPATCH_QUEUE_SERIAL);// or however you want to create your dispatch_queue_t manager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:nil];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { if (central.state == CBCentralManagerStatePoweredOn) { [self startScan]; } if (![self supportLEHardware]) { @throw ([NSError errorWithDomain:@"Bluetooth LE not supported" code:999 userInfo:nil]); } }
- (void)startScan { NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:false] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; [manager scanForPeripheralsWithServices:nil options:options]; }
在这里,我作为服务传递零。
我在Xcode中收到登录设备部分。 但不是在申请中。
Notice>: (Error) Discovered unknown type for scan: { kCBAdvDataChannel = 37; kCBAdvDataIsConnectable = 1; kCBAdvDataManufacturerData = ; kCBAdvDataWSaturated = 0; kCBAdvDataWlanRSSI = 0; }, -51, puck type: 57
您无法在后台扫描nil
服务 – 您必须指定您感兴趣的服务。从文档中
指定了蓝牙中央背景模式的应用程序可以在后台扫描。 也就是说,他们必须通过在serviceUUIDs参数中指定它们来显式扫描一个或多个服务。
为了让您的应用程序继续在后台接收蓝牙更新,您需要在Info.plist
添加UIBackgroundModes
条目,并在列表中包含值bluetooth-central
。