13

I want to get list of bluetooth connected devices...not just paired devices.

I found BluetoothHeadset API in API level 11 which provides method getConnectedDevices() to get list of connected bluetooth devices.

How to get list of bluetooth connected devices using this API ?

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85

2 Answers2

29

Finally got the solution. Below are a few code snippets for getting Bluetooth audio connected devices using BluetoothHeadset API.

BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);


// Define Service Listener of BluetoothProfile
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            mBluetoothHeadset = null;
        }
    }
};


// call functions on mBluetoothHeadset to check if Bluetooth SCO audio is connected.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();                        
for ( final BluetoothDevice dev : devices ) {           
     return mBluetoothHeadset.isAudioConnected(dev);
}


// finally Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • That's a good solution.. and as said in the comment above, congrats ;) – andrea.rinaldi May 20 '14 at 12:35
  • I'm using a similar solution but I'm seeing `E/BluetoothA2dp﹕ Could not bind to Bluetooth A2DP Service with Intent { act=android.bluetooth.IBluetoothA2dp cmp=com.android.bluetooth/.a2dp.A2dpService }` when I try to connect after exiting my app via the back button. I'm sure it must be something to do with not cleaning up properly, but since I'm taking care to call `closeProfileProxy` I'm not sure what it could be. Is this something you've seen? – head in the codes Mar 30 '15 at 00:19
1

First of all you need to define Permission in Androindmanifest.xml

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

Search Connected Devices Activity,

private static BluetoothAdapter mBtAdapter;
private final static int REQUEST_ENABLE_BT = 1;

            // Register for broadcasts when a device is discovered
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            this.registerReceiver(mReceiver, filter);

            // Register for broadcasts when discovery has finished
            filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            this.registerReceiver(mReceiver, filter);


            filter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_STARTED );
            this.registerReceiver( mReceiver, filter );

BroadCastReceiver Class

private final BroadcastReceiver mReceiver = new BroadcastReceiver() 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            String action = intent.getAction();

            // When discovery finds a device
            if ( BluetoothDevice.ACTION_FOUND.equals(action) ) 
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            }
        }
        catch ( Exception e )
        {
            logger.info( DateFormat.format( ConstantCodes.dateFormat ,new java.util.Date()).toString(),"Broadcast Error : " + e.toString(), ConstantCodes.SEARCH_ACTIVITY );
            System.out.println ( "Broadcast Error : " + e.toString() );
        }
    }
};
Lucifer
  • 29,392
  • 25
  • 90
  • 143