我正在使用此代码在蓝牙设备连接或断开连接时收到通知,但是,它不会检查蓝牙设备是否作为音频设备连接
// ...
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...
}
};
如何检测A2DP,btA2dp音频设备?
答案 0 :(得分:2)
您应该注册三个广播以跟踪蓝牙设备连接:
// ...
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;
}
来自BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED
的文档:
此意图将有3个额外内容:
EXTRA_STATE
- 个人资料的当前状态。
EXTRA_PREVIOUS_STATE
- 个人资料的上一个状态。
EXTRA_DEVICE
- 远程设备。
EXTRA_STATE
或EXTRA_PREVIOUS_STATE
可以是STATE_DISCONNECTED
中的任何一个,STATE_CONNECTING
,STATE_CONNECTED
,STATE_DISCONNECTING
。
要检查A2DP设备是否正在流式传输,请注册BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED
广播。
来自BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED
的文档:
此意图将有3个额外内容:
EXTRA_STATE
- 个人资料的当前状态。
EXTRA_PREVIOUS_STATE
- 个人资料的上一个状态。
EXTRA_DEVICE
- 远程设备。
EXTRA_STATE
或EXTRA_PREVIOUS_STATE
可以是STATE_PLAYING
中的任何一个,STATE_NOT_PLAYING
。