I am working on a app where I require to know if there is a bluetooth headset connected. I have created a broadcast receiver that receives the broadcast intents if we connect to the bluetooth headset while the app is on. on the other hand if a connect to the bluetooth device and then start the app I do not receive any broadcast for the bluetooth device connection. I would like to know if any bluetooth headset is connected on the app start. following is my broadcast receiver and manifest.
public class BluetoothDeviceConnectionReciver extends BroadcastReceiver {
static OnBluetoothDeviceStateChangeLisener onBluetoothStateChangedListener;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//int headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
//Toast.makeText(context, "New Test", Toast.LENGTH_SHORT).show();
//BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("headsetAudioState","in broadcast");
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
int bluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
BluetoothHeadset.STATE_DISCONNECTED);
Log.i("headsetAudioState",bluetoothHeadsetState+" ");
//Device found
if (bluetoothHeadsetState == BluetoothHeadset.STATE_CONNECTED) {
if (onBluetoothStateChangedListener != null)
onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
Log.i("headsetAudioState",bluetoothHeadsetState+" connected");
}
if(bluetoothHeadsetState == BluetoothHeadset.STATE_DISCONNECTED){
if (onBluetoothStateChangedListener != null)
onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();
Log.i("headsetAudioState",bluetoothHeadsetState+" disconnected");
}
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
if (onBluetoothStateChangedListener != null)
onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
} else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
if (onBluetoothStateChangedListener != null)
onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();
}
}
public void setOnBluetoothStateChangeListener(OnBluetoothDeviceStateChangeLisener listener){
onBluetoothStateChangedListener = listener;
}
public interface OnBluetoothDeviceStateChangeLisener{
void OnBluetoothHeadsetConnected();
void OnBluetoothHeadsetDisconnected();
}
}
manifest.xml
.....
.....
.....
<receiver android:name=".BluetoothDeviceConnectionReciver">
<intent-filter>
<action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"/>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
.....
.....
I would like to know if there are any other intents that i should be listening to. Or please let me know what other options I have to achieve the intended goal.
P.S : the device connected should be a headset as I intend to use the mic of the bluetooth headset.