1

Is there anyway to get the connected devices list from support profiles (HDD,Spp and audio). The requirement is like my device will support HDD,SPP and Audio, so i have to filter the devices which supports all these profiles. Is there anyway to filter the devices?

Sainath
  • 31
  • 1
  • 6

1 Answers1

5

Yes that is possible but your Android application must target SDK 11 or later (Android 3.0.X).

The solution to your question is that you have to query all BluetoothDevices known by your Android device. By known I mean all paired connected or unconnected devices and unpaired connected devices.

We will filter out unconnected devices later since you only want currently connected devices.


  • First you need to retrieve the BluetoothAdapter:

final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

  • Second you need to make sure Bluetooth is available and turned on :

if (btAdapter != null && btAdapter.isEnabled()) // null means no Bluetooth!

If the Bluetooth is not turned out you can either use btAdapter.enable() which is not recommended in the documentation or ask the user to do it : Programmatically enabling bluetooth on Android

  • Third you need to define an array of states (to filter out unconnected devices):

final int[] states = new int[] {BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING};

  • Fourth, you create a BluetoothProfile.ServiceListener which contains two callbacks triggered when a service is connected and disconnected :

    final BluetoothProfile.ServiceListener listener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
        }
    
        @Override
        public void onServiceDisconnected(int profile) {
        }
    };
    

Now since you have to repeat the querying process for all available Bluetooth Profiles in the Android SDK (A2Dp, GATT, GATT_SERVER, Handset, Health, SAP) you should proceed as follow :

In onServiceConnected, place a condition that check what is the current profile so that we add the found devices into the correct collection and we use : proxy.getDevicesMatchingConnectionStates(states) to filter out unconnected devices:

switch (profile) {
    case BluetoothProfile.A2DP:
        ad2dpDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.GATT: // NOTE ! Requires SDK 18 !
        gattDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.GATT_SERVER: // NOTE ! Requires SDK 18 !
        gattServerDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.HEADSET: 
        headsetDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.HEALTH: // NOTE ! Requires SDK 14 !
        healthDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
    case BluetoothProfile.SAP: // NOTE ! Requires SDK 23 !
        sapDevices.addAll(proxy.getDevicesMatchingConnectionStates(states));
        break;
}

And finally, the last thing to do is start the querying process :

btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.A2DP);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.GATT_SERVER); // NOTE ! Requires SDK 18 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEADSET);
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.HEALTH); // NOTE ! Requires SDK 14 !
btAdapter.getProfileProxy(yourContext, listener, BluetoothProfile.SAP); // NOTE ! Requires SDK 23 !
Community
  • 1
  • 1
Mackovich
  • 3,319
  • 6
  • 35
  • 73
  • How to get all paired connected or unconnected devices and unpaired connected devices. – Sainath Jan 18 '16 at 04:50
  • To get all connected/unconnected paired devices, simply call `BluetoothAdapter.getBondedDevices()`(note: Bluetooth must be enabled for this call to work). For the unpaired connected devices, simply cross-match the list obtained with my code above and the one you get with `getBondedDevices()`. For that, check their **MacAddresses** (`BluetoothDevice.getAddress()`). If the device is found in both list, discard it. If not, it means it's a connecting or connected device **not paired**. There you go ! ;-) – Mackovich Jan 18 '16 at 07:59
  • @Sainath you are welcome. If my answer helped you accomplished what you were looking for, accept it so that other people in your situation can get the answer as well. And if the answer is satisfactory enough, feel free to upvote ;-) – Mackovich Jan 18 '16 at 13:55
  • Acutally my requirement is little different, its like after turning on bluetooth we will get list of devices,in that i have to filter only a specific devcies. currently the devices i have to filter will support all three profiles like SPP,AUdio and HID. with these information i have to filter the specific devices. If you can provide some more information it will be very helpful. Thanks – Sainath Jan 19 '16 at 16:09