我正在努力在后台模式下扫描 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];
}
在这里,我将 nil 作为服务传递。
我在 Xcode 的设备部分收到登录信息。但不在应用程序中。
Notice>: (Error) Discovered unknown type for scan: {
kCBAdvDataChannel = 37;
kCBAdvDataIsConnectable = 1;
kCBAdvDataManufacturerData = <00003962 6708f4c1 00000000 00d02b00 20d03300 20d03300 20>;
kCBAdvDataWSaturated = 0;
kCBAdvDataWlanRSSI = 0;
}, -51, puck type: 57