2

I am using this code to get notified whenever a bluetooth device is connected or disconnected, however, it does not check whether the bluetooth device is connected as an audio device or not

// ... IntentFilter filter1 = new IntentFilter( BluetoothDevice.ACTION_ACL_CONNECTED); IntentFilter filter2 = new IntentFilter( BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED); IntentFilter filter3 = new IntentFilter( BluetoothDevice.ACTION_ACL_DISCONNECTED); this.registerReceiver(BTReceiver, filter1); this.registerReceiver(BTReceiver, filter2); this.registerReceiver(BTReceiver, filter3); } // The BroadcastReceiver that listens for bluetooth broadcasts private final BroadcastReceiver BTReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { // Do something if connected Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_SHORT).show(); } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { // Do something if disconnected Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show(); } // else if... } }; 

how do i detect A2DP, btA2dp audio devices?

1 Answer 1

4

There are three broadcasts you're supposed to register for to keep track of Bluetooth device connections:

// ... IntentFilter filter1 = new IntentFilter( BluetoothAdapter.ACTION_STATE_CHANGED); IntentFilter filter2 = new IntentFilter( BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); IntentFilter filter3 = new IntentFilter( BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); // ... switch (action) { case BluetoothAdapter.ACTION_STATE_CHANGED: // Bluetooth state changed (turned on/off) break; case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED: // Bluetooth connection state changed (device got connected/disconnected) break; case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: // Bluetooth device gained/lost it's state as the media audio device if(intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1) == BluetoothA2dp.STATE_CONNECTED) { Toast.makeText(context, "A2DP device connected!", Toast.LENGTH_LONG).show(); } break; } 

From the documentation of BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:

This intent will have 3 extras:

EXTRA_STATE - The current state of the profile.

EXTRA_PREVIOUS_STATE- The previous state of the profile.

EXTRA_DEVICE - The remote device.

EXTRA_STATE or EXTRA_PREVIOUS_STATE can be any of STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING.

To check if the A2DP device is streaming or not, register for the BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED broadcast.

From the documentation of BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:

This intent will have 3 extras:

EXTRA_STATE - The current state of the profile.

EXTRA_PREVIOUS_STATE - The previous state of the profile.

EXTRA_DEVICE - The remote device.

EXTRA_STATE or EXTRA_PREVIOUS_STATE can be any of STATE_PLAYING, STATE_NOT_PLAYING.

Sign up to request clarification or add additional context in comments.

1 Comment

can you invite me to a chat? it is difficult discussing on here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.