9

我正在努力在后台模式下扫描 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
4

2 回答 2

13

您无法nil在后台扫描服务 - 您必须指定您感兴趣的服务。来自文档

已指定 bluetooth-central 后台模式的应用程序可以在后台进行扫描。也就是说,他们必须通过在 serviceUUIDs 参数中指定一项或多项服务来显式扫描它们。

于 2014-10-28T10:33:10.917 回答
3

为了让您的应用程序继续在后台接收蓝牙更新,您需要向您的应用程序添加一个UIBackgroundModes条目Info.plist并将该值包含bluetooth-central在列表中。

于 2014-10-28T10:29:40.817 回答