How to find out if bluetooth is connected?

Go To StackoverFlow.com

2

Can someone teach me how can I find out if bluetooth is connected to other device (mobile, headset, etc.)

2012-04-04 20:44
by Adam


4

I don't know of any way to get a list of currently connected devices, but you can listen for new connections using the ACL_CONNECTED intent: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED

This intent includes an extra field with the remote device that the connection is with.

On Android, all Bluetooth connections are ACL connections, so registering for this intent will get you all new connections.

So, your receiver would look something like this:

public class ReceiverBlue extends BroadcastReceiver {
  public final static String CTAG = "ReceiverBlue";
  public Set<BluetoothDevice> connectedDevices = new HashSet<BluetoothDevice>();

  public void onReceive(Context ctx, Intent intent) {

    final BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase( action ) )   {
      Log.v(CTAG, "We are now connected to " + device.getName() );
      if (!connectedDevices.contains(device))
        connectedDevices.add(device);
    }

    if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase( action ) )    {
      Log.v(CTAG, "We have just disconnected from " + device.getName() );
      connectedDevices.remove(device);
    }
  }
}
2012-04-04 23:48
by Tom
I have tried this one. whenever i connect device it shows "ACTIONACLCONNECTED"..but it immediately shows that "ACTIONACLDISCONNECTED"..Am i doing wrong anything - Bhoomika Brahmbhatt 2013-08-26 11:12
That's odd. That sounds like the connection is only maintained briefly and then lost. I'm not sure why you are getting that - Tom 2013-08-26 16:28
On Android, all Bluetooth connections are ACL connections are you sure? got a link for that ? - Soheil 2013-09-25 18:18
@Soheil It is my understanding that Android Bluetooth socket comm is built on RFCOMM, and RFCOMM is generally built on L2CAP, which it turn uses ACL links - Tom 2013-10-02 16:05
I am also getting broadcast randomly for random devices. I am receiving broadcast just after pairing to device also and sometimes without pairing also and that too though device is not in pairing mode then also. I am running code on 6.0 I guess implementation must be different for 6.0 - AdiAtAnd 2016-11-10 17:09


0

I think getBondedDevices() will help you :)

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
    // Add the name and address to an array adapter to show in a ListView
    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}

Thanks :)

2012-04-04 21:30
by Ibraheem Osama
getBondedDevices() will get you paired devices, not connected ones - Tom 2012-04-04 23:49
A hacky way is to try to connect to all to see if they are connected, downside is that this is done on the main-thread - jobbert 2016-12-16 08:42


0

To get currently connected devices:

val adapter = BluetoothAdapter.getDefaultAdapter() ?: return // null if not supported
adapter.getProfileProxy(context, object : BluetoothProfile.ServiceListener {
    override fun onServiceDisconnected(p0: Int) {
    }

    override fun onServiceConnected(profile: Int, profileProxy: BluetoothProfile) {
        val connectedDevices = profileProxy.connectedDevices
        adapter.closeProfileProxy(profile, profileProxy)
    }

}, BluetoothProfile.HEADSET) // or .A2DP, .HEALTH, etc
2018-11-01 19:49
by karl
Ads