蓝牙LE设备从iOS后台扫描

Sol*_*oft 8 iphone ios core-bluetooth bluetooth-lowenergy cbcentralmanager

我正在以背景模式扫描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];
Run Code Online (Sandbox Code Playgroud)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{
    if (central.state == CBCentralManagerStatePoweredOn) {

        [self startScan];
    }

    if (![self supportLEHardware]) 
    {
        @throw ([NSError errorWithDomain:@"Bluetooth LE not supported"
                                    code:999
                                userInfo:nil]);
    }
}
Run Code Online (Sandbox Code Playgroud)
- (void)startScan
{
    NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:false] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
    [manager scanForPeripheralsWithServices:nil options:options];
}
Run Code Online (Sandbox Code Playgroud)

在这里,我作为服务传递零.

我在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
Run Code Online (Sandbox Code Playgroud)

Pau*_*w11 13

您无法nil在后台扫描服务 - 您必须指定您感兴趣的服务.从文档中

指定了蓝牙中央背景模式的应用程序可以在后台扫描.也就是说,他们必须通过在serviceUUIDs参数中指定它们来显式扫描一个或多个服务.

  • **你不能传递 nil 并期望在你的应用程序中得到回调**。您在设备控制台中看到的内容并不重要。我在回答中引用的文档对此非常清楚 (2认同)