2

I've connected to Xiaomi air buds via bluetooth settings on my phone. Then, I tried to use getConnectedDevices(BluetoothProfile.GATT_SERVER) and getConnectedDevices(BluetoothProfile.GATT) methods to get a list of connected devices, but in both cases I got an empty array as a result. If I try to use some other profile in getConnectedDevices() I get an exception that says that I'm using a wrong profile.

How can I correctly get a list of currently connected bluetooth devices to my phone.

code example, in onCreate:

mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> connectedDevices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
        for (BluetoothDevice b :
                connectedDevices) {
            Log.e(TAG, "onStart: connectedDevice - " + b.toString() );
        }
Keselme
  • 3,779
  • 7
  • 36
  • 68

1 Answers1

1

To get a list devices that you connected to ( in the past )

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedList = btAdapter.getBondedDevices();


for (BluetoothDevice pairedDevice : pairedList ) 
{
Log.d("BT", "pairedDevice.getName(): " + pairedDevice.getName());
Log.d("BT", "pairedDevice.getAddress(): " + pairedDevice.getAddress());
}

To get the device you correctly connected to you will have to use a broadcast receiver like in the following

    private final BroadcastReceiver btReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    

            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
               Log.i("Device",device.getName()+ "\n"+ device.getAddress()+"\n"+ device.getBondState());
            }
         
        }
    };

There's a few more ways to just find out what the state of the connection of the headset as shown here.

if that still not answering your question then I'm sorry I probably misunderstood your question / need.

Yakir Malka
  • 308
  • 2
  • 11
  • Thaks Yakir, but in your example you're showing devices that I've paired with at some point. However, it doesn't mean that I'm actually connected to any of them at the current moment. – Keselme Jun 06 '21 at 12:58
  • As I mentioned in my question, I did use `getConnectedDevices(GATT)`, but I get an empty array and that's what I try to figure out. – Keselme Jun 06 '21 at 13:11
  • can you add a log that count the size of the connected devices? and if its more then one, add a log with nowConnected[0] and tell me what it prints? – Yakir Malka Jun 06 '21 at 13:13
  • I added `Log.e(TAG, "onStart: connectedDevices = " + connectedDevices.size() );` after calling `getConnectedDevices` and I get 0 – Keselme Jun 06 '21 at 13:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233384/discussion-between-yakir-malka-and-keselme). – Yakir Malka Jun 06 '21 at 13:41