I am starting a service that registers broadcast receiver for BT device connect/disconnect. It seem to work just fine. Although, I ran into a problem. I need to detect if BT headset is already connected, so that I know a current state. I use code below, but it looks like it doesn't do its job:
// ...
// get BluetoothAdapter
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
// to tell if BT headset is connected
boolean isBtHeadsetOn = false;
// if device supports BT
if (ba != null) {
// get list of paired devices
Set<BluetoothDevice> devices = ba.getBondedDevices();
if (devices != null) {
// loop through devices
for (BluetoothDevice device : devices) {
// get device class
BluetoothClass bc = device.getBluetoothClass();
if (bc == null) continue;
int devClass = bc.getDeviceClass();
// check if device is handsfree, headphones or headset
if (devClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || devClass == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES || devClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
// yes, it is
isBtHeadsetOn = true;
break;
}
}
}
}
// now I got my result in variable isBtHeadsetOn
// ...
I am developing for android 2.1 and code above is placed in Service#onCreate(). Thanks for your help.