Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Bluetooth advertise and scan in the background

I have been trying to setup an app to make the device both scan for peripherals and advertise as a peripheral. The goal is for two devices to be woken up in the background when they become near each other via bluetooth discovery. From the Apple Documentation, it seems that you should be able to run BLE in the background (with bluetooth-central and bluetooth-peripheral background modes enabled), and my application works when one device is in the foreground. First, I advertise data like so:

NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",                               CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:identifier]]};  // Start advertising over BLE [peripheralManager startAdvertising:advertisingData];  

I then set the device to scan for data:

NSArray *services = @[[CBUUID UUIDWithString:identifier]];  [centralManager scanForPeripheralsWithServices:services options:nil]; 

However, when both go into the background (device has to be locked), the bluetooth cannot discover and

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 

never gets called on either device. How can I fix this? Thanks

like image 439
Kyle Rosenbluth Avatar asked Dec 06 '13 15:12

Kyle Rosenbluth


People also ask

What is active scan in Ble?

This process of scanning for devices is called device discovery. There are two types of scanning; active and passive. The difference is that an active scanner can send a scan request to request additional information from the advertiser, while a passive scanner can only receive data from advertising device.

What is core Bluetooth?

The Core Bluetooth framework is an abstraction of the Bluetooth low energy protocol stack. That said, it hides many of the low-level details of the specification from you, the developer, making it much easier for you to develop apps that interact with Bluetooth low energy devices.

What is background mode in iOS?

iOS restricts the use of background operations to improve user experience and extend battery life. Your app can run in the background for specific use cases, including: playing audio, updating location and fetching the latest content from a server.

How do I find my BLE device?

To locate a specific Bluetooth device using the app, look for it by name in the device list. If you do not see it, ensure the device is not already connected to your mobile device or anything else, because most devices will stop advertising when they are connected and LightBlue® will not be able to detect it.


2 Answers

I'm afraid what you are trying to do will not work. I have tried to achieve the same thing.

The problem is the difference in scanning in foreground and background. When you are scanning for devices in the foreground you can scan for anything. In the background you must specify the actual service UUID you are scanning for. Ok, this isn't actually a problem as you know the UUID you are looking for.

Peripheral: Broadcasting as a peripheral again works differently in foreground and background. In foreground it works like any normal BT peripheral. In the background it has a very limited amount of space to work with, so your peripherals UUID is hidden away and not broadcast. Only when a central device (an iPhone in foreground) requests the information from it will it wake your app and show it's UUID.

So the 2 cancel each other out. As your background scan can only scan for devices with a specific UUID and your background peripheral cannot advertise its UUID, they cannot see each other.

1 of your devices (either peripheral or central) must be in the foreground to work.

This has been discussed several times on the Apple Bluetooth mail list.

like image 181
Darren Avatar answered Sep 17 '22 01:09

Darren


You should elaborate on how you're testing this, because theoretically it looks like it should work. There's two primary issues you may be facing:

1.) Scanning is throttled down when iOS devices are in the background.

  • While scanning in the foreground will likely immediately discover a device advertising next to it, discovery in the background can take up to ~60 times longer. The iOS system makes no assumptions that the user would prefer one app to have better Bluetooth functionality than another (or that only one app wants to use it). And since it is shared functionality, they want users to have a uniform experience across apps. You should check out the technical specifications regarding Advertising and Scanning intervals to get a better idea of what Apple has to do under the covers.

2.) Your devices may have already discovered each other before entering the background.

  • We must remember that Apple disables the CBCentralManagerScanOptionAllowDuplicatesKey scanning flag when we enter the background. Since you're not even specifying this flag, it defaults to NO anyways. So if they've even seen each other once, you will not get another callback when they are in the background.
like image 27
Tommy Devoy Avatar answered Sep 20 '22 01:09

Tommy Devoy