0

I am working on BLE Apps where i need to scan the device in Background mode. While debugging i found that it scan for the device but the discovery of devices is not called and returned. Everything works fine in Foreground Mode.

Why can't my app scan the device while it is running in the background?

Note: I did input the Required background modes 1.App communicates using CoreBluetooth 2.App shares data using CoreBluetooth. Below is the code i called for scan device in foreground

[[LGCentralManager sharedInstance] scanForPeripheralsByInterval:1 completion:^(NSArray *peripherals) {
      // If we found any peripherals sending to test
      if (peripherals.count > 0) {
         for (LGPeripheral *peripheral in peripherals) {
              if (peripheral.advertisingData) {
         }
      }
}]; 

Output Advertisement data:

Printing description of peripheral->_advertisingData:
        {
            kCBAdvDataIsConnectable = 1;
            kCBAdvDataLocalName = "Device Name";
            kCBAdvDataServiceUUIDs =     (
                "00000000-64B0-5B82-3F51-000000027D2"
            );
            kCBAdvDataTxPowerLevel = "-2";
        }
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Can you show the code you are using? In particular, note that in background mode you must scan for specific service(s) - you cannot scan for `nil` – Paulw11 Oct 01 '15 at 08:43
  • Right now i am using the below code [[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4 services:@[scanned.deviceUUID] options:nil completion:^(NSArray *peripherals) where i can get the specific device uuid. – Pratik Mehta Oct 01 '15 at 09:09
  • It looks like you are specifying a service UUID, but I am suspicious because you have `deviceUUID` - what is this value? It needs to be the Bluetooth service you are looking for. Please edit your question to show more code and context. For example, which class is this code in? – Paulw11 Oct 01 '15 at 09:10
  • @Paulw11 Updated question with code lines – Pratik Mehta Oct 01 '15 at 13:10
  • I am not familiar with the `LGCentralManager` class you are using, I have always just used Core Bluetooth directly, but the method signature `scanForPeripheralsByInterval` makes me suspicious since it takes a time interval. This means it is probably using NSTimer and this won't work in the background. – Paulw11 Oct 01 '15 at 16:14
  • Ok. I looked at the source code of that library and that method doesn't use NSTimer but it is a one-shot scan that is cancelled after the specified duration, which means you are probably trying to call it in a loop or something which Is why I asked to see more code as to how you are calling the scan but this approach won't work in the background. You need to use `scanForPeripheralsWithServices:options:` – Paulw11 Oct 01 '15 at 16:21

0 Answers0