9

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
Solid Soft
  • 1,872
  • 2
  • 25
  • 55

2 Answers2

14

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.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I think so, But which serviceUUIDs required. Is it relates to Hardware services ? https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx – Solid Soft Oct 28 '14 at 10:35
  • 1
    You need to identify the service that your peripheral is advertising. LightBlue can help with this or you can use your app in foreground mode to discover the services - for example heart rate is 0x180d – Paulw11 Oct 28 '14 at 10:39
  • Is LightBlue app scan in background ? – Solid Soft Oct 28 '14 at 10:54
  • 1
    No, but it will show you the services that your peripheral is advertising – Paulw11 Oct 28 '14 at 10:57
  • OK, Let me check.. Do you have any idea how to achieve this Background scan stuff ? – Solid Soft Oct 28 '14 at 11:01
  • 1
    Yes, you need to pass the UUIDs of the services that you want to scan for when you start the scan. – Paulw11 Oct 28 '14 at 13:01
  • In Background scan, Actual issue is, when i pass nil as services, I got above values {Notice>: (Error) Discovered unknown type for scan: } in Device console. Not in Application. So i think there is something need to change will solve my issues. – Solid Soft Oct 30 '14 at 06:59
  • 2
    **You cannot pass nil and expect to get call backs in your application**. It doesn't matter what you see in the device console. The documentation I quoted in my answer is quite clear on this – Paulw11 Oct 30 '14 at 07:04
  • Achieved. Problem with hardware side. Thanks for your time to support. accepted answer.. Peripheral was not advertising services, That is issue. – Solid Soft Oct 31 '14 at 07:00
3

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.

grahamparks
  • 16,130
  • 5
  • 49
  • 43