Packet-less SPP Bluetooth disconnect detection

I have some Bluetooth devices that connect to my Android phone, however I am having problems detecting outages. Bluetooth devices do not send data packets if they do not need them, so you do not need to use a watchdog timer to receive packets to detect a disconnect. I read that you can use ACLDisconnected broadcast, but this event never fires for me (I waited for minutes). What is a reliable way to detect outages in Android 6?

Here is my AclDisconnect registration code:

_filter = new IntentFilter(); _filter.AddAction(BluetoothDevice.ActionFound); _filter.AddAction(BluetoothDevice.ActionBondStateChanged); _filter.AddAction(BluetoothAdapter.ActionDiscoveryStarted); _filter.AddAction(BluetoothDevice.ActionAclDisconnected); _filter.AddAction(BluetoothDevice.ActionAclDisconnectRequested); context.RegisterReceiver(_bluetoothDeviceReceiver, _filter); 

And a callback (which does not work when disconnected)

 public override void OnReceive(Context context, Intent intent) { string action = intent.Action; if (action == BluetoothDevice.ActionAclDisconnected || action == BluetoothDevice.ActionAclDisconnectRequested) { Interlocked.CompareExchange(ref Disconnected, null, null)?.Invoke(); } } 
+5
source share
2 answers

I do not know about Xamarin, but in simple Android / Java this is usually done by catching the IOException that an InputStream throws when the stream is closed. For instance:

 // In your listening thread InputStream inputStream = socket.getInputStream(); while (true) { try { int nextByte = inputStream.read(); // Do something with it } catch (IOException e) { // Here you know that you are disconnected } } 
+2
source

BluetoothDevice.ActionAclDisconnected is really unreliable, and the only sure way to detect a disconnect is to catch an IOException, like previous response states.

So, for this you will have to develop a "ping" mechanism where data is sent every X times.

0
source

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


All Articles