Havoc
Havoc

Reputation: 213

Android 2.2 progammically tell if my Bluetooth Headset is connected on application start

I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.

Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.

I have also figured out how to see if the device has any paired devices via the following.

BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() != 0)
    {
        // the device has paired devices
        for (BluetoothDevice device : pairedDevices)
        {
            Log.i(TAG, "device name: " + device.getName());
            Log.i(TAG, "device address: " + device.getAddress());
        }
    }
    else
    {
        // no paired devices
        Log.i(TAG, "no paired devices");
    }
}

I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as BluetoothProfile and BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8

I am willing to try anything at this point.

Thanks in advance

-H

Upvotes: 2

Views: 2914

Answers (2)

shantanu
shantanu

Reputation: 2418

Following code works for me. First connect your BT device with android manually.

AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
    //bt device is connected with phone
}
else{
   // no device is connected with phone
}

Upvotes: 4

Femi
Femi

Reputation: 64700

Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.

Upvotes: 1

Related Questions