How can I get a Bluetooth connection notification when an iOS application is based on Xamarin?

We need our application to receive notifications from the OS when it connects or disconnects from a Bluetooth audio device (in particular, in a car).

The application receives a notification when the BT device initially connects, but then it immediately disconnects and logs an error:

BTCentralManager :: DisconnectedPeripheral> SoundCore mini (ATT) ERROR Error Domain = CBErrorDomain Code = 7 "The specified device has disconnected from us."

... and the DisconnectedPeripheral event never fires.

We are not sure how easy it is to receive connections and disconnect events while the application is running.

Do I need to connect peripherals to the central manager? We only need to know if the audio device is connected or not - we do not need to interact with it in any way.

Events never trigger a second time from the background, after receiving a peripheral disconnect. Presumably due to the error message we receive.

Sample code below:

public class BTCentralManager : CBCentralManagerDelegate { public CBCentralManager centralManager; public static CBPeripheral peripheral; public BTCentralManager() { System.Diagnostics.Debug.WriteLine("BTCentralManager::Constructor > "); centralManager = new CBCentralManager(this, new DispatchQueue("myqueue"), new CBCentralInitOptions { ShowPowerAlert = true, RestoreIdentifier = "myRestoreIdentifier" }); NSUuid[] arr = { new NSUuid("7e9002be-547f-42bc-8d56-209736f70aa2") }; //Sound core mini var devices = centralmanager.retrieveperipheralswithidentifiers(arr); peripheral = devices.firstordefault(); //is the only way to trigger the events, we need to first connect the peripheral to central manager??? centralManager.connectPeripheral(peripheral, new PeripheralConnectionOptions { NotifyOnConnection = true, NotifyOnDisconnection = true, NotifyOnNotification = true }); } //Always is triggered inclusive in background public override void UpdatedState(CBCentralManager central) { System.Diagnostics.Debug.WriteLine("BTCentralManager::UpdatedState > " + central.State.ToString()); } //Only is triggered when the device is first time connected ( Inclusive in background) public override void ConnectedPeripheral(CBCentralManager central, CBPeripheral peripheral) { System.Diagnostics.Debug.WriteLine("BTCentralManager::ConnectedPeripheral > " + peripheral.Name); //After the connect made successfully I receive this error, and never connect again to the device //Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." } public override void DisconnectedPeripheral(CBCentralManager central, CBPeripheral peripheral, NSError error) { System.Diagnostics.Debug.WriteLine("BTCentralManager::DisconnectedPeripheral > " + peripheral.Name + " ERROR>" + error.Description); } public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI) { System.Diagnostics.Debug.WriteLine("BTCentralManager::DiscoveredPeripheral > " + peripheral.Name); // base.DiscoveredPeripheral(central, peripheral, advertisementData, RSSI); } } 
+5
source share
1 answer

As far as I understand your question, you need to monitor for the presence of a Bluetooth audio device. Several times I did some research, and the result did not really satisfy me. Here is my conclusion:

1.) CoreBluetooth is for use only with Bluetooth 4.0 / Bluetooth Low Energy devices. Many Bluetooth headsets or even car radios are still not Bluetooth 4.x. Thus, if you cannot rely on your Bluetooth audio device, which should be 4.x, CoreBluetooth can be a waste of time.

2.) Your application will not be notified when an audio device is connected when your application is in the background.

Nothing good so far. But there may be some approaches that may help.

1.) Using the CLLocationManager, you can start monitoring, for example. a compass (not a place to save battery) to receive a notification when the phone has been moved. Just check the connected audio devices when the application calls CLLocationManagerDelegate. This, of course, is not very effective, but it may work.

2.) Use iBeacons and CLBeaconRegions, if available. Put the iBeacon in the custom car and start watching the lighthouse.

I know this is not exactly what you want to hear, but I am afraid that there is no direct solution to your problem.

amuses Peter

0
source

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


All Articles