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