diff --git a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java index 1b3118c323edb5ece97bc4847f1a446d70283f2a..57ff97bae3dd77b6b438cd9496216df16b3b92c1 100644 --- a/android/app/src/com/android/bluetooth/a2dp/A2dpService.java +++ b/android/app/src/com/android/bluetooth/a2dp/A2dpService.java @@ -1069,7 +1069,7 @@ public class A2dpService extends ProfileService { // Make sure volume has been store before device been remove from active. if (mFactory.getAvrcpTargetService() != null) { - mFactory.getAvrcpTargetService().volumeDeviceSwitched(device); + mFactory.getAvrcpTargetService().handleA2dpActiveDeviceChanged(device); } synchronized (mStateMachines) { mActiveDevice = device; @@ -1270,6 +1270,9 @@ public class A2dpService extends ProfileService { removeStateMachine(device); } } + if (mFactory.getAvrcpTargetService() != null) { + mFactory.getAvrcpTargetService().handleA2dpConnectionStateChanged(device, toState); + } mAdapterService .getActiveDeviceManager() .a2dpConnectionStateChanged(device, fromState, toState); diff --git a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java index ae753a28ba8230372394344c34d22acbf7bea510..30751ca69bbf08d6b34de0b0f531b42f3bf3e254 100644 --- a/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java +++ b/android/app/src/com/android/bluetooth/avrcp/AvrcpTargetService.java @@ -17,7 +17,6 @@ package com.android.bluetooth.avrcp; import android.annotation.NonNull; -import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothUtils; @@ -140,25 +139,7 @@ public class AvrcpTargetService extends ProfileService { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (action.equals(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) { - if (mNativeInterface == null) return; - - // Update all the playback status info for each connected device - mNativeInterface.sendMediaUpdate(false, true, false); - } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) { - if (mNativeInterface == null) return; - - BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - if (device == null) return; - - int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); - if (state == BluetoothProfile.STATE_DISCONNECTED) { - // If there is no connection, disconnectDevice() will do nothing - if (mNativeInterface.disconnectDevice(device.getAddress())) { - Log.d(TAG, "request to disconnect device " + device); - } - } - } else if (action.equals(AudioManager.ACTION_VOLUME_CHANGED)) { + if (action.equals(AudioManager.ACTION_VOLUME_CHANGED)) { int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); if (streamType == AudioManager.STREAM_MUSIC) { int volume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); @@ -252,8 +233,6 @@ public class AvrcpTargetService extends ProfileService { mReceiver = new AvrcpBroadcastReceiver(); IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); - filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED); - filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); filter.addAction(AudioManager.ACTION_VOLUME_CHANGED); registerReceiver(mReceiver, filter); @@ -337,18 +316,6 @@ public class AvrcpTargetService extends ProfileService { mVolumeManager.deviceDisconnected(device); } - /** - * Signal to the service that the current audio out device has changed and to inform - * the audio service whether the new device supports absolute volume. If it does, also - * set the absolute volume level on the remote device. - */ - public void volumeDeviceSwitched(BluetoothDevice device) { - if (DEBUG) { - Log.d(TAG, "volumeDeviceSwitched: device=" + device); - } - mVolumeManager.volumeDeviceSwitched(device); - } - /** * Remove the stored volume for a device. */ @@ -368,6 +335,35 @@ public class AvrcpTargetService extends ProfileService { return mVolumeManager.getVolume(device, mVolumeManager.getNewDeviceVolume()); } + /** + * Handle when A2DP connection state changes. + * + * <p>If the A2DP connection disconnects, we request AVRCP to disconnect device as well. + */ + public void handleA2dpConnectionStateChanged(BluetoothDevice device, int newState) { + if (device == null || mNativeInterface == null) return; + if (newState == BluetoothProfile.STATE_DISCONNECTED) { + // If there is no connection, disconnectDevice() will do nothing + if (mNativeInterface.disconnectDevice(device.getAddress())) { + Log.d(TAG, "request to disconnect device " + device); + } + } + } + /** + * Handle when Active Device changes in A2DP. + * + * <p>Signal to the service that the current audio out device has changed and to inform the + * audio service whether the new device supports absolute volume. If it does, also set the + * absolute volume level on the remote device. + */ + public void handleA2dpActiveDeviceChanged(BluetoothDevice device) { + mVolumeManager.volumeDeviceSwitched(device); + if (mNativeInterface != null) { + // Update all the playback status info for each connected device + mNativeInterface.sendMediaUpdate(false, true, false); + } + } + // TODO (apanicke): Add checks to rejectlist Absolute Volume devices if they behave poorly. void setVolume(int avrcpVolume) { BluetoothDevice activeDevice = getA2dpActiveDevice();