Android detect Bluetooth disconnect immediately Max 2 seconds
Asked Answered
B

3

16

I'm looking for a way to detect the disconnection of a Bluetooth device immediately after it has happened (2 second max), typically in a "device too far" scenario or Device battery is dead. Currently I can detect it with a BroadcastReceiver by getting a BluetoothDevice.ACTION_ACL_DISCONNECTED, but it takes about 16 to 20 seconds to fire.

  1. Is there any way to get notified in 2 seconds Max.
  2. I used BroadcatReceiver but it is not fast enough to get alert in 2 seconds Max, so is there any other kind of approach available to get notification quickly that bluetooth is disconnected.
  3. I use this createRfcommSocketToServiceRecord(UUID); to connect a paired device and i am bound to use it using UUID.

I have visited a lot of links regarding this issue, but no one matches with my needs.that's why any help would be appreciated.

thanks.

Bhayani answered 5/3, 2014 at 14:19 Comment(3)
Any news on this? Could u find a solution for that?Derive
not yet, but will surely post answer if i find something!Bhayani
I think the only way is to send pings and get pong messages somehow. Only probleme here where the device has batteries, than the continuous send/receive will drain down the battery very soon.Derive
H
5

I think the only way you can reliably sense loss of connection quickly (within two seconds) is via your own application protocol that you use over the Bluetooth connection. For example, your application protocol might implement a heartbeat that occurs every 500ms. If you don't see a heartbeat within two seconds then you could trigger your own event.

Bluetooth is a socket-based stream protocol that is designed to work over an unreliable medium (i.e. radio), and as such has to tolerate errors in (or loss of) packets. For this reason it will take significantly more than 2 seconds before your Bluetooth stack declares it has given up and disconnected the device, as you have found.

I have an application on Play which is designed to talk with an automotive ECU via Bluetooth and my strategy for sensing disconnection is exactly as I suggested in my first paragraph.

Update 20th June 14

I see in your bounty comment and also your comment below that you're asking for a code example, but it's kind of difficult for me to provide one without knowing anything about the application protocol that you're running over the socket connection. Or to put it another way, what exactly is it about my first paragraph (i.e. the heartbeat suggestion) that you do not understand or cannot create code for yourself? The concept of using a heartbeat really is quite simple. You would define a certain message type in your application protocol that represents a heartbeat message. One end of the connection sends this heartbeat message periodically, say every one second. The other end of the connection checks that this heartbeat message is received every second or so and drops the connection after a two-second time-out. It is impossible to be any more specific than that, because I can't see your existing code and I don't know what kind of messages you are currently exchanging over the socket.

Holomorphic answered 5/3, 2014 at 14:53 Comment(4)
hm!!! but how? code example or tutorial links regarding this would be helpful @HolomorphicBhayani
thanks for reply @trevor but the problem is! other end of connection, can not reply or provide the acknowledgement! that's why i am in problem!Bhayani
@Bhayani That's what suggested to implement Heart beat mechanism. In case your app don't receive heartbeat response from your hardware Bluetooth device (it means its out of range or switched off), then you would be proceeding the device disconnection and showing the disconnected alert on app side.Chicane
@Holomorphic In my perspective about Heart beat mechanism to check the connectivity between app and device, it seems like lot of battery consumption on app (phone) and Bluetooth hardware device as well. Do you think so?Chicane
B
2

After nothing work around! I got two things to get my work done.

  1. I need to check that is my Bluetooth socket is not in use(Sending Receiving) till 2 to 5 sec I disconnect that and when user wants to send data to the receiver device I connect that again.

  2. Or I'll try to connect the socket after 2 to 5 sec so that if it is not ready to connect means it is already connected, else it will be connected and I refresh the previous socket references.

but first option is more valuable to work perfectly in my problem.

Bhayani answered 27/6, 2014 at 5:26 Comment(0)
F
1

This is a problem with old bluetooth and more hardware than software.

If you want to notice that the connection is broken you need to do polling (a heartbeat), something like "are you alive? are you alive?"... This is bad for battery so... the users will finally uninstall your app.

I recommend you to change to BTLE (bluetooth low energy), devices like Nexus 5 has this. With BTLE you have a proximity profile which can tell you the quality of the signal, so, you can guess the distance (near, far, out of range) and therefore you can also tell if the devices are disconnected.

Another nice point is that if the devices are out of range but one is again in range you could get noticed as well, so this is really nice for apps to open doors by proximity for example.

Check this: https://developer.bluetooth.org/TechnologyOverview/Pages/PXP.aspx

In the other hand Apple has invented the concept of iBeacons, devices that are distance aware, and the good thing is that there is also an implementation of iBeacons for Android: http://developer.radiusnetworks.com/ibeacon/android/

Faso answered 27/6, 2014 at 8:39 Comment(5)
Verdas problem is not hte distance! problem is unexpected loss of connection! like the hardware is out of battery or dead! then i want to quickly notify that connection is lost!Bhayani
hi, the distance is calculated with RSSI which actually tells the quality of signal. Let me say that RSSI=0 is a lost of signal then we can tell that the device is out of range (due to distance or missed battery). All the BTLE devices that acts as Gatt Servers provides a RSSI value, so this solution is standard for all the BTLE devices.Faso
I am not developing for BTLE supported devices only, but I am building app for all the cheapest to highest paid devices.Bhayani
It's what I said, this is a hardware problem. This is one of the reasons that made normal bluetooth to fail (too much battery consumption). Time ago people only send messages via SMS and nowadays everybody does with Whatsapp, gtalk, etc. For me this is the same scenario. Anyway, if this doesn't fit your needs just ignore it but I have passed over the same problems and finally decided to develop ONLY for BTLE. I hope anyway that this helped you a bit.Faso
Thanks for your help! but the problem is hardware is more important than application.Bhayani

© 2022 - 2024 — McMap. All rights reserved.