4

I have an application that will listen for voice input through bluetooth if available and if not available then will read through the phone microphone. I can't seem to find a way to check if their are any bluetooth devices already connected (not just paired). Any suggestions?

user2812299
  • 149
  • 2
  • 14

3 Answers3

2

The following worked for me (API >= 18):

final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> gattServerConnectedDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT_SERVER);
for (BluetoothDevice device : gattServerConnectedDevices) {
    Log.d("Tag", "Found connected device: " + device.getAddress());
}
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
1

Try this method, API 18 though.

https://developer.android.com/reference/android/bluetooth/BluetoothManager.html#getConnectedDevices(int)

pelotasplus
  • 9,852
  • 1
  • 35
  • 37
  • 1
    Can you provide an example please. Everything I have tried uses broadcast receivers, but I want to query in onCreate method. – user2812299 Feb 27 '14 at 21:58
  • 1
    FYI, this only works for BluetoothProfile.GATT and BluetoothProfile.GATT_SERVER. – swooby May 17 '16 at 05:00
1

It's possible to list connected headset devices via BluethoothHeadset service

   btAdapter.getProfileProxy(context, object : BluetoothProfile.ServiceListener {
            override fun onServiceDisconnected(p0: Int) {
                //
            }

            override fun onServiceConnected(p0: Int, headset: BluetoothProfile?) {
                headset?.connectedDevices?.forEach {
                    Timber.d("${it.name} ${it.address}")
                }
            }
        }, BluetoothProfile.HEADSET)

adray
  • 1,408
  • 16
  • 20