Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bluetooth connected devices using BluetoothHeadset API

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 ?

like image 709
Priyank Patel Avatar asked Sep 20 '12 08:09

Priyank Patel


People also ask

What is Bluetooth API?

The app framework provides access to the Bluetooth functionality through Bluetooth APIs. These APIs let apps connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features. Using the Bluetooth APIs, an app can perform the following: Scan for other Bluetooth devices.

How do I get a list of Bluetooth devices on my Android phone?

By using BluetoothAdapter method getBondedDevices(), we can get the Bluetooth paired devices list. Following is the code snippet to get all paired devices with name and MAC address of each device.

How do you check Bluetooth is on or off in Android programmatically?

Call isEnabled() to check whether Bluetooth is currently enabled. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() , passing in an ACTION_REQUEST_ENABLE intent action.


1 Answers

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);
like image 69
Priyank Patel Avatar answered Oct 11 '22 18:10

Priyank Patel