The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame^] | 20 | import android.annotation.Nullable; |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 24 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.ServiceConnection; |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 28 | import android.media.AudioManager; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 29 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.IBinder; |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 31 | import android.os.ParcelUuid; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 39 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 43 | * This class provides the public APIs to control the Bluetooth A2DP |
| 44 | * profile. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 46 | * <p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 47 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 48 | * the BluetoothA2dp proxy object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 50 | * <p> Android only supports one connected Bluetooth A2dp device at a time. |
| 51 | * Each method is protected with its appropriate permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 53 | public final class BluetoothA2dp implements BluetoothProfile { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | private static final String TAG = "BluetoothA2dp"; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 55 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 56 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Intent used to broadcast the change in connection state of the A2DP |
| 60 | * profile. |
| 61 | * |
| 62 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 63 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 64 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 65 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 66 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 67 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 68 | * |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 69 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 70 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 71 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 72 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 73 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 74 | * receive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | */ |
| 76 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 77 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 78 | "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Intent used to broadcast the change in the Playing state of the A2DP |
| 82 | * profile. |
| 83 | * |
| 84 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 85 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 86 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 87 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 88 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 89 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 90 | * |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 91 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 92 | * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING}, |
| 93 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 94 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 95 | * receive. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 96 | */ |
| 97 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 98 | public static final String ACTION_PLAYING_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 99 | "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
Hemant Gupta | 3b76a4b | 2014-02-14 19:53:31 +0530 | [diff] [blame] | 101 | /** @hide */ |
| 102 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 103 | public static final String ACTION_AVRCP_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 104 | "android.bluetooth.a2dp.profile.action.AVRCP_CONNECTION_STATE_CHANGED"; |
Hemant Gupta | 3b76a4b | 2014-02-14 19:53:31 +0530 | [diff] [blame] | 105 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 106 | /** |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame^] | 107 | * Intent used to broadcast the selection of a connected device as active. |
| 108 | * |
| 109 | * <p>This intent will have one extra: |
| 110 | * <ul> |
| 111 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can |
| 112 | * be null if no device is active. </li> |
| 113 | * </ul> |
| 114 | * |
| 115 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 116 | * receive. |
| 117 | * |
| 118 | * @hide |
| 119 | */ |
| 120 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 121 | public static final String ACTION_ACTIVE_DEVICE_CHANGED = |
| 122 | "android.bluetooth.a2dp.profile.action.ACTIVE_DEVICE_CHANGED"; |
| 123 | |
| 124 | /** |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 125 | * Intent used to broadcast the change in the Audio Codec state of the |
| 126 | * A2DP Source profile. |
| 127 | * |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 128 | * <p>This intent will have 2 extras: |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 129 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 130 | * <li> {@link BluetoothCodecStatus#EXTRA_CODEC_STATUS} - The codec status. </li> |
| 131 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device if the device is currently |
| 132 | * connected, otherwise it is not included.</li> |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 133 | * </ul> |
| 134 | * |
| 135 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 136 | * receive. |
| 137 | * |
| 138 | * @hide |
| 139 | */ |
| 140 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 141 | public static final String ACTION_CODEC_CONFIG_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 142 | "android.bluetooth.a2dp.profile.action.CODEC_CONFIG_CHANGED"; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 145 | * A2DP sink device is streaming music. This state can be one of |
| 146 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 147 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 148 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 149 | public static final int STATE_PLAYING = 10; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 150 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 151 | /** |
| 152 | * A2DP sink device is NOT streaming music. This state can be one of |
| 153 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 154 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 155 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 156 | public static final int STATE_NOT_PLAYING = 11; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 157 | |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 158 | /** |
| 159 | * We don't have a stored preference for whether or not the given A2DP sink device supports |
| 160 | * optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 161 | * |
| 162 | * @hide |
| 163 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 164 | public static final int OPTIONAL_CODECS_SUPPORT_UNKNOWN = -1; |
| 165 | |
| 166 | /** |
| 167 | * The given A2DP sink device does not support optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 168 | * |
| 169 | * @hide |
| 170 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 171 | public static final int OPTIONAL_CODECS_NOT_SUPPORTED = 0; |
| 172 | |
| 173 | /** |
| 174 | * The given A2DP sink device does support optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 175 | * |
| 176 | * @hide |
| 177 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 178 | public static final int OPTIONAL_CODECS_SUPPORTED = 1; |
| 179 | |
| 180 | /** |
| 181 | * We don't have a stored preference for whether optional codecs should be enabled or disabled |
| 182 | * for the given A2DP device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 183 | * |
| 184 | * @hide |
| 185 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 186 | public static final int OPTIONAL_CODECS_PREF_UNKNOWN = -1; |
| 187 | |
| 188 | /** |
| 189 | * Optional codecs should be disabled for the given A2DP device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 190 | * |
| 191 | * @hide |
| 192 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 193 | public static final int OPTIONAL_CODECS_PREF_DISABLED = 0; |
| 194 | |
| 195 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 196 | * Optional codecs should be enabled for the given A2DP device. |
| 197 | * |
| 198 | * @hide |
| 199 | */ |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 200 | public static final int OPTIONAL_CODECS_PREF_ENABLED = 1; |
| 201 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 202 | private Context mContext; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 203 | private ServiceListener mServiceListener; |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 204 | private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 205 | @GuardedBy("mServiceLock") |
| 206 | private IBluetoothA2dp mService; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 207 | private BluetoothAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 209 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 210 | new IBluetoothStateChangeCallback.Stub() { |
| 211 | public void onBluetoothStateChange(boolean up) { |
| 212 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 213 | if (!up) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 214 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| 215 | try { |
| 216 | mServiceLock.writeLock().lock(); |
| 217 | mService = null; |
| 218 | mContext.unbindService(mConnection); |
| 219 | } catch (Exception re) { |
| 220 | Log.e(TAG, "", re); |
| 221 | } finally { |
| 222 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 223 | } |
| 224 | } else { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 225 | try { |
| 226 | mServiceLock.readLock().lock(); |
| 227 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 228 | if (VDBG) Log.d(TAG, "Binding service..."); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 229 | doBind(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 230 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 231 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 232 | Log.e(TAG, "", re); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 233 | } finally { |
| 234 | mServiceLock.readLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 242 | * Bluetooth A2DP service. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | */ |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 244 | /*package*/ BluetoothA2dp(Context context, ServiceListener l) { |
| 245 | mContext = context; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 246 | mServiceListener = l; |
| 247 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 248 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 249 | if (mgr != null) { |
| 250 | try { |
| 251 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 252 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 253 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 257 | doBind(); |
| 258 | } |
| 259 | |
| 260 | boolean doBind() { |
| 261 | Intent intent = new Intent(IBluetoothA2dp.class.getName()); |
| 262 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 263 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 264 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 265 | android.os.Process.myUserHandle())) { |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 266 | Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent); |
| 267 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 269 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 272 | /*package*/ void close() { |
| 273 | mServiceListener = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 274 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 275 | if (mgr != null) { |
| 276 | try { |
| 277 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 278 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 279 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 283 | try { |
| 284 | mServiceLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 285 | if (mService != null) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 286 | mService = null; |
| 287 | mContext.unbindService(mConnection); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 288 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 289 | } catch (Exception re) { |
| 290 | Log.e(TAG, "", re); |
| 291 | } finally { |
| 292 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 293 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 296 | @Override |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 297 | public void finalize() { |
Mathias Jeppsson | 2d2d8c2 | 2013-08-06 11:41:55 +0200 | [diff] [blame] | 298 | // The empty finalize needs to be kept or the |
| 299 | // cts signature tests would fail. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 300 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 301 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 302 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 303 | * Initiate connection to a profile of the remote bluetooth device. |
| 304 | * |
| 305 | * <p> Currently, the system supports only 1 connection to the |
| 306 | * A2DP profile. The API will automatically disconnect connected |
| 307 | * devices before connecting. |
| 308 | * |
| 309 | * <p> This API returns false in scenarios like the profile on the |
| 310 | * device is already connected or Bluetooth is not turned on. |
| 311 | * When this API returns true, it is guaranteed that |
| 312 | * connection state intent for the profile will be broadcasted with |
| 313 | * the state. Users can get the connection state of the profile |
| 314 | * from this intent. |
| 315 | * |
| 316 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 317 | * permission. |
| 318 | * |
| 319 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 320 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 321 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 323 | public boolean connect(BluetoothDevice device) { |
| 324 | if (DBG) log("connect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 325 | try { |
| 326 | mServiceLock.readLock().lock(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 327 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 328 | return mService.connect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 329 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 330 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 331 | return false; |
| 332 | } catch (RemoteException e) { |
| 333 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 334 | return false; |
| 335 | } finally { |
| 336 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 340 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 341 | * Initiate disconnection from a profile |
| 342 | * |
| 343 | * <p> This API will return false in scenarios like the profile on the |
| 344 | * Bluetooth device is not in connected state etc. When this API returns, |
| 345 | * true, it is guaranteed that the connection state change |
| 346 | * intent will be broadcasted with the state. Users can get the |
| 347 | * disconnection state of the profile from this intent. |
| 348 | * |
| 349 | * <p> If the disconnection is initiated by a remote device, the state |
| 350 | * will transition from {@link #STATE_CONNECTED} to |
| 351 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 352 | * host (local) device the state will transition from |
| 353 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 354 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 355 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 356 | * two scenarios. |
| 357 | * |
| 358 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 359 | * permission. |
| 360 | * |
| 361 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 362 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 363 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 365 | public boolean disconnect(BluetoothDevice device) { |
| 366 | if (DBG) log("disconnect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 367 | try { |
| 368 | mServiceLock.readLock().lock(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 369 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 370 | return mService.disconnect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 371 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 372 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 373 | return false; |
| 374 | } catch (RemoteException e) { |
| 375 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 376 | return false; |
| 377 | } finally { |
| 378 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 382 | /** |
| 383 | * {@inheritDoc} |
| 384 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 385 | @Override |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 386 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 387 | if (VDBG) log("getConnectedDevices()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 388 | try { |
| 389 | mServiceLock.readLock().lock(); |
| 390 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 391 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 392 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 393 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 394 | return new ArrayList<BluetoothDevice>(); |
| 395 | } catch (RemoteException e) { |
| 396 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 397 | return new ArrayList<BluetoothDevice>(); |
| 398 | } finally { |
| 399 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 400 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
| 404 | * {@inheritDoc} |
| 405 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 406 | @Override |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 407 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 408 | if (VDBG) log("getDevicesMatchingStates()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 409 | try { |
| 410 | mServiceLock.readLock().lock(); |
| 411 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 412 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 413 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 414 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 415 | return new ArrayList<BluetoothDevice>(); |
| 416 | } catch (RemoteException e) { |
| 417 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 418 | return new ArrayList<BluetoothDevice>(); |
| 419 | } finally { |
| 420 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 421 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
| 425 | * {@inheritDoc} |
| 426 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 427 | @Override |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 428 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 429 | if (VDBG) log("getState(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 430 | try { |
| 431 | mServiceLock.readLock().lock(); |
| 432 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 433 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 434 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 435 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 436 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 437 | return BluetoothProfile.STATE_DISCONNECTED; |
| 438 | } catch (RemoteException e) { |
| 439 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 440 | return BluetoothProfile.STATE_DISCONNECTED; |
| 441 | } finally { |
| 442 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 443 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame^] | 447 | * Select a connected device as active. |
| 448 | * |
| 449 | * The active device selection is per profile. An active device's |
| 450 | * purpose is profile-specific. For example, A2DP audio streaming |
| 451 | * is to the active A2DP Sink device. If a remote device is not |
| 452 | * connected, it cannot be selected as active. |
| 453 | * |
| 454 | * <p> This API returns false in scenarios like the profile on the |
| 455 | * device is not connected or Bluetooth is not turned on. |
| 456 | * When this API returns true, it is guaranteed that the |
| 457 | * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted |
| 458 | * with the active device. |
| 459 | * |
| 460 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 461 | * permission. |
| 462 | * |
| 463 | * @param device the remote Bluetooth device. Could be null to clear |
| 464 | * the active device and stop streaming audio to a Bluetooth device. |
| 465 | * @return false on immediate error, true otherwise |
| 466 | * @hide |
| 467 | */ |
| 468 | public boolean setActiveDevice(@Nullable BluetoothDevice device) { |
| 469 | if (DBG) log("setActiveDevice(" + device + ")"); |
| 470 | try { |
| 471 | mServiceLock.readLock().lock(); |
| 472 | if (mService != null && isEnabled() |
| 473 | && ((device == null) || isValidDevice(device))) { |
| 474 | return mService.setActiveDevice(device); |
| 475 | } |
| 476 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 477 | return false; |
| 478 | } catch (RemoteException e) { |
| 479 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 480 | return false; |
| 481 | } finally { |
| 482 | mServiceLock.readLock().unlock(); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Get the connected device that is active. |
| 488 | * |
| 489 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 490 | * permission. |
| 491 | * |
| 492 | * @return the connected device that is active or null if no device |
| 493 | * is active |
| 494 | * @hide |
| 495 | */ |
| 496 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 497 | @Nullable |
| 498 | public BluetoothDevice getActiveDevice() { |
| 499 | if (VDBG) log("getActiveDevice()"); |
| 500 | try { |
| 501 | mServiceLock.readLock().lock(); |
| 502 | if (mService != null && isEnabled()) { |
| 503 | return mService.getActiveDevice(); |
| 504 | } |
| 505 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 506 | return null; |
| 507 | } catch (RemoteException e) { |
| 508 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 509 | return null; |
| 510 | } finally { |
| 511 | mServiceLock.readLock().unlock(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 516 | * Set priority of the profile |
| 517 | * |
| 518 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 519 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 520 | * {@link #PRIORITY_OFF}, |
| 521 | * |
| 522 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 523 | * permission. |
| 524 | * |
| 525 | * @param device Paired bluetooth device |
| 526 | * @param priority |
| 527 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 528 | * @hide |
| 529 | */ |
| 530 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 531 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 532 | try { |
| 533 | mServiceLock.readLock().lock(); |
| 534 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 535 | && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 536 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 537 | && priority != BluetoothProfile.PRIORITY_ON) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 540 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 541 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 542 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 543 | return false; |
| 544 | } catch (RemoteException e) { |
| 545 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 546 | return false; |
| 547 | } finally { |
| 548 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 549 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 553 | * Get the priority of the profile. |
| 554 | * |
| 555 | * <p> The priority can be any of: |
| 556 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 557 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 558 | * |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 559 | * @param device Bluetooth device |
| 560 | * @return priority of the device |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 561 | * @hide |
| 562 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 563 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 564 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 565 | if (VDBG) log("getPriority(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 566 | try { |
| 567 | mServiceLock.readLock().lock(); |
| 568 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 569 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 570 | return mService.getPriority(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 571 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 572 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 573 | return BluetoothProfile.PRIORITY_OFF; |
| 574 | } catch (RemoteException e) { |
| 575 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 576 | return BluetoothProfile.PRIORITY_OFF; |
| 577 | } finally { |
| 578 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 579 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 583 | * Checks if Avrcp device supports the absolute volume feature. |
| 584 | * |
| 585 | * @return true if device supports absolute volume |
| 586 | * @hide |
| 587 | */ |
| 588 | public boolean isAvrcpAbsoluteVolumeSupported() { |
| 589 | if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 590 | try { |
| 591 | mServiceLock.readLock().lock(); |
| 592 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 593 | return mService.isAvrcpAbsoluteVolumeSupported(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 594 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 595 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 596 | return false; |
| 597 | } catch (RemoteException e) { |
| 598 | Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e); |
| 599 | return false; |
| 600 | } finally { |
| 601 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 602 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 606 | * Tells remote device to adjust volume. Only if absolute volume is |
| 607 | * supported. Uses the following values: |
| 608 | * <ul> |
| 609 | * <li>{@link AudioManager#ADJUST_LOWER}</li> |
| 610 | * <li>{@link AudioManager#ADJUST_RAISE}</li> |
| 611 | * <li>{@link AudioManager#ADJUST_MUTE}</li> |
| 612 | * <li>{@link AudioManager#ADJUST_UNMUTE}</li> |
| 613 | * </ul> |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 614 | * |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 615 | * @param direction One of the supported adjust values. |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 616 | * @hide |
| 617 | */ |
| 618 | public void adjustAvrcpAbsoluteVolume(int direction) { |
| 619 | if (DBG) Log.d(TAG, "adjustAvrcpAbsoluteVolume"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 620 | try { |
| 621 | mServiceLock.readLock().lock(); |
| 622 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 623 | mService.adjustAvrcpAbsoluteVolume(direction); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 624 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 625 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 626 | } catch (RemoteException e) { |
| 627 | Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e); |
| 628 | } finally { |
| 629 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 630 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Tells remote device to set an absolute volume. Only if absolute volume is supported |
| 635 | * |
| 636 | * @param volume Absolute volume to be set on AVRCP side |
| 637 | * @hide |
| 638 | */ |
| 639 | public void setAvrcpAbsoluteVolume(int volume) { |
| 640 | if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 641 | try { |
| 642 | mServiceLock.readLock().lock(); |
| 643 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 644 | mService.setAvrcpAbsoluteVolume(volume); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 645 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 646 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 647 | } catch (RemoteException e) { |
| 648 | Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e); |
| 649 | } finally { |
| 650 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 651 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 655 | * Check if A2DP profile is streaming music. |
| 656 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 657 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 658 | * |
| 659 | * @param device BluetoothDevice device |
| 660 | */ |
| 661 | public boolean isA2dpPlaying(BluetoothDevice device) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 662 | try { |
| 663 | mServiceLock.readLock().lock(); |
| 664 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 665 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 666 | return mService.isA2dpPlaying(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 667 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 668 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 669 | return false; |
| 670 | } catch (RemoteException e) { |
| 671 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 672 | return false; |
| 673 | } finally { |
| 674 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 675 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 679 | * This function checks if the remote device is an AVCRP |
| 680 | * target and thus whether we should send volume keys |
| 681 | * changes or not. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 682 | * |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 683 | * @hide |
| 684 | */ |
| 685 | public boolean shouldSendVolumeKeys(BluetoothDevice device) { |
| 686 | if (isEnabled() && isValidDevice(device)) { |
| 687 | ParcelUuid[] uuids = device.getUuids(); |
| 688 | if (uuids == null) return false; |
| 689 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 690 | for (ParcelUuid uuid : uuids) { |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 691 | if (BluetoothUuid.isAvrcpTarget(uuid)) { |
| 692 | return true; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 699 | /** |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 700 | * Gets the current codec status (configuration and capability). |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 701 | * |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 702 | * @return the current codec status |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 703 | * @hide |
| 704 | */ |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 705 | public BluetoothCodecStatus getCodecStatus() { |
| 706 | if (DBG) Log.d(TAG, "getCodecStatus"); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 707 | try { |
| 708 | mServiceLock.readLock().lock(); |
| 709 | if (mService != null && isEnabled()) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 710 | return mService.getCodecStatus(); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 711 | } |
| 712 | if (mService == null) { |
| 713 | Log.w(TAG, "Proxy not attached to service"); |
| 714 | } |
| 715 | return null; |
| 716 | } catch (RemoteException e) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 717 | Log.e(TAG, "Error talking to BT service in getCodecStatus()", e); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 718 | return null; |
| 719 | } finally { |
| 720 | mServiceLock.readLock().unlock(); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Sets the codec configuration preference. |
| 726 | * |
| 727 | * @param codecConfig the codec configuration preference |
| 728 | * @hide |
| 729 | */ |
| 730 | public void setCodecConfigPreference(BluetoothCodecConfig codecConfig) { |
| 731 | if (DBG) Log.d(TAG, "setCodecConfigPreference"); |
| 732 | try { |
| 733 | mServiceLock.readLock().lock(); |
| 734 | if (mService != null && isEnabled()) { |
| 735 | mService.setCodecConfigPreference(codecConfig); |
| 736 | } |
| 737 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 738 | return; |
| 739 | } catch (RemoteException e) { |
| 740 | Log.e(TAG, "Error talking to BT service in setCodecConfigPreference()", e); |
| 741 | return; |
| 742 | } finally { |
| 743 | mServiceLock.readLock().unlock(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 748 | * Enables the optional codecs. |
| 749 | * |
| 750 | * @hide |
| 751 | */ |
| 752 | public void enableOptionalCodecs() { |
| 753 | if (DBG) Log.d(TAG, "enableOptionalCodecs"); |
| 754 | enableDisableOptionalCodecs(true); |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Disables the optional codecs. |
| 759 | * |
| 760 | * @hide |
| 761 | */ |
| 762 | public void disableOptionalCodecs() { |
| 763 | if (DBG) Log.d(TAG, "disableOptionalCodecs"); |
| 764 | enableDisableOptionalCodecs(false); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Enables or disables the optional codecs. |
| 769 | * |
| 770 | * @param enable if true, enable the optional codecs, other disable them |
| 771 | */ |
| 772 | private void enableDisableOptionalCodecs(boolean enable) { |
| 773 | try { |
| 774 | mServiceLock.readLock().lock(); |
| 775 | if (mService != null && isEnabled()) { |
| 776 | if (enable) { |
| 777 | mService.enableOptionalCodecs(); |
| 778 | } else { |
| 779 | mService.disableOptionalCodecs(); |
| 780 | } |
| 781 | } |
| 782 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 783 | return; |
| 784 | } catch (RemoteException e) { |
| 785 | Log.e(TAG, "Error talking to BT service in enableDisableOptionalCodecs()", e); |
| 786 | return; |
| 787 | } finally { |
| 788 | mServiceLock.readLock().unlock(); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /** |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 793 | * Returns whether this device supports optional codecs. |
| 794 | * |
| 795 | * @param device The device to check |
| 796 | * @return one of OPTIONAL_CODECS_SUPPORT_UNKNOWN, OPTIONAL_CODECS_NOT_SUPPORTED, or |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 797 | * OPTIONAL_CODECS_SUPPORTED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 798 | * @hide |
| 799 | */ |
| 800 | public int supportsOptionalCodecs(BluetoothDevice device) { |
| 801 | try { |
| 802 | mServiceLock.readLock().lock(); |
| 803 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 804 | return mService.supportsOptionalCodecs(device); |
| 805 | } |
| 806 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 807 | return OPTIONAL_CODECS_SUPPORT_UNKNOWN; |
| 808 | } catch (RemoteException e) { |
| 809 | Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e); |
| 810 | return OPTIONAL_CODECS_SUPPORT_UNKNOWN; |
| 811 | } finally { |
| 812 | mServiceLock.readLock().unlock(); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Returns whether this device should have optional codecs enabled. |
| 818 | * |
| 819 | * @param device The device in question. |
| 820 | * @return one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 821 | * OPTIONAL_CODECS_PREF_DISABLED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 822 | * @hide |
| 823 | */ |
| 824 | public int getOptionalCodecsEnabled(BluetoothDevice device) { |
| 825 | try { |
| 826 | mServiceLock.readLock().lock(); |
| 827 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 828 | return mService.getOptionalCodecsEnabled(device); |
| 829 | } |
| 830 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 831 | return OPTIONAL_CODECS_PREF_UNKNOWN; |
| 832 | } catch (RemoteException e) { |
| 833 | Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e); |
| 834 | return OPTIONAL_CODECS_PREF_UNKNOWN; |
| 835 | } finally { |
| 836 | mServiceLock.readLock().unlock(); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Sets a persistent preference for whether a given device should have optional codecs enabled. |
| 842 | * |
| 843 | * @param device The device to set this preference for. |
| 844 | * @param value Whether the optional codecs should be enabled for this device. This should be |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 845 | * one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or |
| 846 | * OPTIONAL_CODECS_PREF_DISABLED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 847 | * @hide |
| 848 | */ |
| 849 | public void setOptionalCodecsEnabled(BluetoothDevice device, int value) { |
| 850 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 851 | if (value != BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN |
| 852 | && value != BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED |
| 853 | && value != BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) { |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 854 | Log.e(TAG, "Invalid value passed to setOptionalCodecsEnabled: " + value); |
| 855 | return; |
| 856 | } |
| 857 | mServiceLock.readLock().lock(); |
| 858 | if (mService != null && isEnabled() |
| 859 | && isValidDevice(device)) { |
| 860 | mService.setOptionalCodecsEnabled(device, value); |
| 861 | } |
| 862 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 863 | return; |
| 864 | } catch (RemoteException e) { |
| 865 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 866 | return; |
| 867 | } finally { |
| 868 | mServiceLock.readLock().unlock(); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 873 | * Helper for converting a state to a string. |
| 874 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | * For debug use only - strings are not internationalized. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 876 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | * @hide |
| 878 | */ |
| 879 | public static String stateToString(int state) { |
| 880 | switch (state) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 881 | case STATE_DISCONNECTED: |
| 882 | return "disconnected"; |
| 883 | case STATE_CONNECTING: |
| 884 | return "connecting"; |
| 885 | case STATE_CONNECTED: |
| 886 | return "connected"; |
| 887 | case STATE_DISCONNECTING: |
| 888 | return "disconnecting"; |
| 889 | case STATE_PLAYING: |
| 890 | return "playing"; |
| 891 | case STATE_NOT_PLAYING: |
| 892 | return "not playing"; |
| 893 | default: |
| 894 | return "<unknown state " + state + ">"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } |
| 896 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 897 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 898 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 899 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 900 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 901 | try { |
| 902 | mServiceLock.writeLock().lock(); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 903 | mService = IBluetoothA2dp.Stub.asInterface(Binder.allowBlocking(service)); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 904 | } finally { |
| 905 | mServiceLock.writeLock().unlock(); |
| 906 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 907 | |
| 908 | if (mServiceListener != null) { |
| 909 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this); |
| 910 | } |
| 911 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 912 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 913 | public void onServiceDisconnected(ComponentName className) { |
| 914 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 915 | try { |
| 916 | mServiceLock.writeLock().lock(); |
| 917 | mService = null; |
| 918 | } finally { |
| 919 | mServiceLock.writeLock().unlock(); |
| 920 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 921 | if (mServiceListener != null) { |
| 922 | mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP); |
| 923 | } |
| 924 | } |
| 925 | }; |
| 926 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 927 | private boolean isEnabled() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 928 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 929 | return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | private boolean isValidDevice(BluetoothDevice device) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 933 | if (device == null) return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 934 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 935 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 936 | return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 937 | } |
| 938 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 939 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 940 | Log.d(TAG, msg); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |