| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 19 | import android.annotation.RequiresPermission; |
| 20 | import android.annotation.SuppressLint; |
| 21 | import android.bluetooth.annotations.RequiresBluetoothConnectPermission; |
| 22 | import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| Jeff Sharkey | 73458a8 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 24 | import android.os.Binder; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 33 | * This class provides the public APIs to control the Bluetooth AVRCP Controller. It currently |
| 34 | * supports player information, playback support and track metadata. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 35 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 36 | * <p>BluetoothAvrcpController is a proxy object for controlling the Bluetooth AVRCP |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 37 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 38 | * the BluetoothAvrcpController proxy object. |
| 39 | * |
| 40 | * {@hide} |
| 41 | */ |
| 42 | public final class BluetoothAvrcpController implements BluetoothProfile { |
| 43 | private static final String TAG = "BluetoothAvrcpController"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 44 | private static final boolean DBG = false; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 45 | private static final boolean VDBG = false; |
| 46 | |
| 47 | /** |
| 48 | * Intent used to broadcast the change in connection state of the AVRCP Controller |
| 49 | * profile. |
| 50 | * |
| 51 | * <p>This intent will have 3 extras: |
| 52 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 53 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 54 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 55 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 56 | * </ul> |
| 57 | * |
| 58 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 59 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 60 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 61 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 62 | @RequiresLegacyBluetoothPermission |
| 63 | @RequiresBluetoothConnectPermission |
| 64 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 65 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 66 | "android.bluetooth.avrcp-controller.profile.action.CONNECTION_STATE_CHANGED"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 69 | * Intent used to broadcast the change in player application setting state on AVRCP AG. |
| 70 | * |
| 71 | * <p>This intent will have the following extras: |
| 72 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 73 | * <li> {@link #EXTRA_PLAYER_SETTING} - {@link BluetoothAvrcpPlayerSettings} containing the |
| 74 | * most recent player setting. </li> |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 75 | * </ul> |
| 76 | */ |
| 77 | public static final String ACTION_PLAYER_SETTING = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 78 | "android.bluetooth.avrcp-controller.profile.action.PLAYER_SETTING"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 79 | |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 80 | public static final String EXTRA_PLAYER_SETTING = |
| 81 | "android.bluetooth.avrcp-controller.profile.extra.PLAYER_SETTING"; |
| 82 | |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 83 | private BluetoothAdapter mAdapter; |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 84 | private final BluetoothProfileConnector<IBluetoothAvrcpController> mProfileConnector = |
| 85 | new BluetoothProfileConnector(this, BluetoothProfile.AVRCP_CONTROLLER, |
| 86 | "BluetoothAvrcpController", IBluetoothAvrcpController.class.getName()) { |
| 87 | @Override |
| 88 | public IBluetoothAvrcpController getServiceInterface(IBinder service) { |
| 89 | return IBluetoothAvrcpController.Stub.asInterface( |
| 90 | Binder.allowBlocking(service)); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 91 | } |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 92 | }; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Create a BluetoothAvrcpController proxy object for interacting with the local |
| 96 | * Bluetooth AVRCP service. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 97 | */ |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 98 | /*package*/ BluetoothAvrcpController(Context context, ServiceListener listener) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 99 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 100 | mProfileConnector.connect(context, listener); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /*package*/ void close() { |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 104 | mProfileConnector.disconnect(); |
| 105 | } |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 106 | |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 107 | private IBluetoothAvrcpController getService() { |
| 108 | return mProfileConnector.getService(); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 111 | @Override |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 112 | public void finalize() { |
| 113 | close(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * {@inheritDoc} |
| 118 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 119 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 120 | @RequiresBluetoothConnectPermission |
| 121 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 122 | public List<BluetoothDevice> getConnectedDevices() { |
| 123 | if (VDBG) log("getConnectedDevices()"); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 124 | final IBluetoothAvrcpController service = |
| 125 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 126 | if (service != null && isEnabled()) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 127 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 128 | return service.getConnectedDevices(); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 129 | } catch (RemoteException e) { |
| 130 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 131 | return new ArrayList<BluetoothDevice>(); |
| 132 | } |
| 133 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 134 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 135 | return new ArrayList<BluetoothDevice>(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * {@inheritDoc} |
| 140 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 141 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 142 | @RequiresBluetoothConnectPermission |
| 143 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 144 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 145 | if (VDBG) log("getDevicesMatchingStates()"); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 146 | final IBluetoothAvrcpController service = |
| 147 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 148 | if (service != null && isEnabled()) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 149 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 150 | return service.getDevicesMatchingConnectionStates(states); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 151 | } catch (RemoteException e) { |
| 152 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 153 | return new ArrayList<BluetoothDevice>(); |
| 154 | } |
| 155 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 156 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 157 | return new ArrayList<BluetoothDevice>(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * {@inheritDoc} |
| 162 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 163 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 164 | @RequiresBluetoothConnectPermission |
| 165 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 166 | public int getConnectionState(BluetoothDevice device) { |
| 167 | if (VDBG) log("getState(" + device + ")"); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 168 | final IBluetoothAvrcpController service = |
| 169 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 170 | if (service != null && isEnabled() && isValidDevice(device)) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 171 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 172 | return service.getConnectionState(device); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 173 | } catch (RemoteException e) { |
| 174 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 175 | return BluetoothProfile.STATE_DISCONNECTED; |
| 176 | } |
| 177 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 178 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 179 | return BluetoothProfile.STATE_DISCONNECTED; |
| 180 | } |
| 181 | |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Gets the player application settings. |
| 184 | * |
| 185 | * @return the {@link BluetoothAvrcpPlayerSettings} or {@link null} if there is an error. |
| 186 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 187 | @RequiresBluetoothConnectPermission |
| 188 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 189 | public BluetoothAvrcpPlayerSettings getPlayerSettings(BluetoothDevice device) { |
| 190 | if (DBG) Log.d(TAG, "getPlayerSettings"); |
| 191 | BluetoothAvrcpPlayerSettings settings = null; |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 192 | final IBluetoothAvrcpController service = |
| 193 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 194 | if (service != null && isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 195 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 196 | settings = service.getPlayerSettings(device); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 197 | } catch (RemoteException e) { |
| 198 | Log.e(TAG, "Error talking to BT service in getMetadata() " + e); |
| 199 | return null; |
| 200 | } |
| 201 | } |
| 202 | return settings; |
| 203 | } |
| 204 | |
| 205 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 206 | * Sets the player app setting for current player. |
| 207 | * returns true in case setting is supported by remote, false otherwise |
| 208 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 209 | @RequiresBluetoothConnectPermission |
| 210 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 211 | public boolean setPlayerApplicationSetting(BluetoothAvrcpPlayerSettings plAppSetting) { |
| 212 | if (DBG) Log.d(TAG, "setPlayerApplicationSetting"); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 213 | final IBluetoothAvrcpController service = |
| 214 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 215 | if (service != null && isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 216 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 217 | return service.setPlayerApplicationSetting(plAppSetting); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 218 | } catch (RemoteException e) { |
| 219 | Log.e(TAG, "Error talking to BT service in setPlayerApplicationSetting() " + e); |
| 220 | return false; |
| 221 | } |
| 222 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 223 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 227 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 228 | * Send Group Navigation Command to Remote. |
| 229 | * possible keycode values: next_grp, previous_grp defined above |
| 230 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame^] | 231 | @RequiresBluetoothConnectPermission |
| 232 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 233 | public void sendGroupNavigationCmd(BluetoothDevice device, int keyCode, int keyState) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 234 | Log.d(TAG, "sendGroupNavigationCmd dev = " + device + " key " + keyCode + " State = " |
| 235 | + keyState); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 236 | final IBluetoothAvrcpController service = |
| 237 | getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 238 | if (service != null && isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 239 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 240 | service.sendGroupNavigationCmd(device, keyCode, keyState); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 241 | return; |
| 242 | } catch (RemoteException e) { |
| 243 | Log.e(TAG, "Error talking to BT service in sendGroupNavigationCmd()", e); |
| 244 | return; |
| 245 | } |
| 246 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 247 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 250 | private boolean isEnabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 251 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 254 | private static boolean isValidDevice(BluetoothDevice device) { |
| 255 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | private static void log(String msg) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 259 | Log.d(TAG, msg); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 260 | } |
| 261 | } |