在A2DP Sink应用程序中添加以下代码,以避免在播放音乐时重新启动或停止Sink:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mBluetoothA2dp.registerCallback(new BluetoothA2dp.Callback() {
@Override
public void onBluetoothA2dpSinkStateChanged(BluetoothDevice device, int newState) {
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// Handle disconnected state
}
}
});
} else {
mBluetoothA2dp.registerSinkStateListener(mContext, new BluetoothA2dpSinkStateListener() {
@Override
public void onSinkStateChanged(BluetoothDevice device, int prevState, int newState) {
if (newState == BluetoothA2dp.STATE_DISCONNECTED) {
// Handle disconnected state
}
}
});
}
这将注册一个回调或监听器,以在A2DP Sink设备的状态更改为已断开连接时通知您。您可以根据需要添加适当的代码来处理此状态更改。