Use Bloothtooth LE when the app is in the background

I am building an iOS application with Xamarin, with this BLE plugin:

https://github.com/aritchie/bluetoothle

I just pass the UUID through BLE and it works. Here is my code:

var data = new Plugin.BluetoothLE.Server.AdvertisementData { LocalName = "MyServer", }; data.ServiceUuids.Add(new Guid("MY_UUID_HERE")); await this.server.Start(data); 

The only problem is that it stops broadcasting when I put the application in the background. And resumes again when I open the application again.

How can I let him continue to broadcast it in the background? I read the documentation here:

https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

And it says that I have to use the CBCentralManager class to get the save and restore function (so I can always support broadcast UUIDs), but it's hard for me to translate this into Xamarin / C #.

EDIT

After exploring a few more, I read that I need to create an instance of CBCentralManager and implement WillRestoreState in the delegate. I did this in AppDelegate :

 [Register("AppDelegate")] public class AppDelegate : MvxApplicationDelegate, ICBCentralManagerDelegate { private IGattServer server = CrossBleAdapter.Current.CreateGattServer(); private CBCentralManager cbCentralManager; public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { // irrelevant code... this.Ble(); return true; } private async Task Ble() { try { await Task.Delay(5000); // wait for it to finish initializing so I can access BLE (it crashes otherwise) var options = new CBCentralInitOptions(); options.RestoreIdentifier = "myRestoreIndentifier"; this.cbCentralManager = new CBCentralManager(this,null,options); var data = new Plugin.BluetoothLE.Server.AdvertisementData { LocalName = "MyServer", }; data.ServiceUuids.Add(new Guid("MY_UUID_HERE")); await this.server.Start(data); } catch (Exception e) { } } public void UpdatedState(CBCentralManager central) { //throw new NotImplementedException(); } [Export("centralManager:willRestoreState:")] public void WillRestoreState(CBCentralManager central, NSDictionary dict) { //never gets called } 

But that didn't matter to me. And the WillRestoreState method WillRestoreState never called ... I don't mind using another plugin / library if I need at this point ...

EDIT 2

I just realized that the application is still broadcasting while it is in the background, I no longer see the UUID of the service (on the web portal of the beacon I'm testing with), I see only the phone identifier.

+5
source share
2 answers

After some research, I found this to be just an iOS limitation - you cannot broadcast the BLE service UUIDs while your application is in the background. Background work is very limited in iOS.

EDIT include comment Paulw11 (which is true): You can advertise the service, but it is advertised in such a way that only another iOS device that specifically scans this service UUID can see it.

+1
source

Although you cannot broadcast the UUID of the BLE service while your iOS application is in the background, for those trying to do something, you should take a peek at iBeacon. This is an Apple protocol that allows iOS apps to make bluetooth stuff while it is in the background.

0
source

Source: https://habr.com/ru/post/1269043/


All Articles