Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android detect Bluetooth disconnect immediately Max 2 seconds

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.

like image 320
Hamad Avatar asked Mar 05 '14 14:03

Hamad


People also ask

Why does my Bluetooth keep disconnecting on Android?

Why Does My Bluetooth Keep Disconnecting From My Android? Many apps are designed to block Bluetooth; however, clearing the cache can help solve the issue. To reset the apps for Android phones, go to Settings > System > Advanced > Reset Options > Reset Wi-fi, Bluetooth, mobile.

What causes Bluetooth to disconnect?

Battery Optimization Or Power Saving Settings Android phones have a Battery Optimization feature that saves your battery from the background apps and running programs. If the setting is enabled, the Bluetooth connection will be disconnected.

How do you reset Bluetooth on Android?

Reset Bluetooth Settings 1: Go to Settings -> System and tap the Advanced drop-down button. 2: Select Reset options and then tap Reset Wi-Fi, mobile, & Bluetooth. 3: Tap the Reset settings button below and enter your phone's PIN when asked.


2 Answers

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.

like image 167
Trevor Avatar answered Oct 14 '22 15:10

Trevor


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.

like image 45
Hamad Avatar answered Oct 14 '22 17:10

Hamad