蓝牙LE设备从iOS后台扫描

时间:2014-10-28 08:55:47

标签: ios iphone bluetooth-lowenergy core-bluetooth 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];

- (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

2 个答案:

答案 0 :(得分:13)

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

  

指定了蓝牙中央后台模式的应用是   允许在后台扫描。那说,他们必须   通过在中指定它们来显式扫描一个或多个服务   serviceUUIDs参数。

答案 1 :(得分:2)

要让您的应用继续在后台接收蓝牙更新,您需要在UIBackgroundModes中添加Info.plist条目,并在列表中添加值bluetooth-central