我正在努力在后台模式下扫描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 = <00003962 6708f4c1 00000000 00d02b00 20d03300 20d03300 20>;
kCBAdvDataWSaturated = 0;
kCBAdvDataWlanRSSI = 0;
}, -51, puck type: 57
您不能nil
在后台扫描服务-您必须指定您感兴趣的服务。来自文档
指定了蓝牙中心背景模式的应用程序可以在后台扫描。也就是说,它们必须通过在serviceUUIDs参数中指定它们来显式扫描一个或多个服务。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句