Bluetooth LE Device scan in background from iOS

老子叫甜甜 提交于 2019-12-18 22:31:50

问题


I am working on to scan BLE in Background mode.

Issue is not working in Background scan. Its working very fine in Foreground mode.

Below is few code lines.

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];
}

here i am passing nil as a services.

I receive log in Devices section in Xcode. But not in application.

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

回答1:


You cannot scan for nil services in the background - you must specify the service(s) that you are interested in. From the documentation

Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter.




回答2:


For your app to continue to receive Bluetooth updates in the background, you need to add a UIBackgroundModes entry to your Info.plist and include the value bluetooth-central in the list.



来源:https://stackoverflow.com/questions/26604177/bluetooth-le-device-scan-in-background-from-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!