4

For bluetooth device found we receive Brodcast from Android with action :

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    // New bluetooth device found    
}

When we set device to discover able then this happens.

My question is what are the intents that are fired when device discoverable is turned off or bluetooth of other device turned off.

In my list view I am showing devices that are "FOUND" I am able to do this using above code. But I want to remove entry of the device which is no longer in range, no longer discoverable or has turned off the bluetooth are there any specific intent that Android platform fires ?

I have looked through BluetoothDevice, BluetoothAdapter reference APIs. But did n't found any useful broadcast action.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Prashant
  • 4,474
  • 8
  • 34
  • 82
  • 1
    I too have the same probelm, and still working on that.. – Kartheek Jun 15 '15 at 12:05
  • [This](http://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected-android-2-2) should help you out. – Strider Jun 15 '15 at 12:10
  • @Strider I have already gone though it. That didn't work in my case. – Prashant Jun 15 '15 at 12:12
  • 1
    You can't detect whenever a 'found' device turns off it's bluetooth or gets out of range, unless you do a rescan – Strider Jun 15 '15 at 12:35
  • @Strider, I see that the only way for now. To rescan and compare result with last scan and eliminate entries which are vanished in new scan. But thats expensive. – Prashant Jun 15 '15 at 12:39
  • @pcj Yeah, but that seems like the best and only solution for the moment. – Strider Jun 15 '15 at 12:41
  • Thank you all @ Jordi Castilla, @ Strider, @Kartheek for your suggestions. – Prashant Jun 15 '15 at 12:46

1 Answers1

3

The intent you are searching for is BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED

Intent used to broadcast the change in connection state of the local Bluetooth adapter to a profile of the remote device.

This intent will have 3 extras:

EXTRA_CONNECTION_STATE or EXTRA_PREVIOUS_CONNECTION_STATE can be:

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Thanx for reply. What is happening I have implemented BluetoothDevice.ACTION_FOUND which ever device in range turns on the bluetooth and discoverable it pops into my list. But when it is turned off discoverable or bluetooth it should be removed from list will above intent will work in that case ? – Prashant Jun 15 '15 at 12:00
  • I have already this ACTION_CONNECTION_STATE_CHANGED but not working – Kartheek Jun 15 '15 at 12:04
  • you have to act depending the state you receive to manage your app devices, but AFAIK, yes, it will also work in your case with `STATE_DISCONNECTING` – Jordi Castilla Jun 15 '15 at 12:04
  • Hi I have added filter as you suggested. I have enabled debugger at the point of onReceive(Context context, Intent intent) { final String action = intent.getAction(); but don't see any intent fired when other device is out of range, or left , or no longer discoverable. Is this scenario possible ? Thank you @Jordi Castilla. – Prashant Jun 15 '15 at 12:19
  • This only works if you are connecting to the device, not if you are just observing broadcasts – Tim Feb 11 '16 at 13:53
  • I don't think this is good answer. Quoting documentation: "This intent is useful for applications that are only concerned about whether the local adapter is connected to any profile of any device and are not really concerned about which profile." So if your device X is connected to some other device Y and you want to check if device Z is out of range this intent will have no use. – LLL Sep 10 '19 at 10:43