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 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 45 | *<p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP |
| 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> |
| 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> |
| 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 = |
| 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> |
| 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> |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 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 = |
| 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 = |
| 103 | "android.bluetooth.a2dp.profile.action.AVRCP_CONNECTION_STATE_CHANGED"; |
| 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> |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 111 | * <li> {@link BluetoothCodecStatus#EXTRA_CODEC_STATUS} - The codec status. </li> |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 112 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device if the device is currently |
| 113 | * connected, otherwise it is not included.</li> |
| 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 = |
| 123 | "android.bluetooth.a2dp.profile.action.CODEC_CONFIG_CHANGED"; |
| 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 | */ |
| 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 | */ |
| 137 | public static final int STATE_NOT_PLAYING = 11; |
| 138 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 139 | private Context mContext; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 140 | private ServiceListener mServiceListener; |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 141 | private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); |
| 142 | @GuardedBy("mServiceLock") private IBluetoothA2dp mService; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 143 | private BluetoothAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 145 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 146 | new IBluetoothStateChangeCallback.Stub() { |
| 147 | public void onBluetoothStateChange(boolean up) { |
| 148 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 149 | if (!up) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 150 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| 151 | try { |
| 152 | mServiceLock.writeLock().lock(); |
| 153 | mService = null; |
| 154 | mContext.unbindService(mConnection); |
| 155 | } catch (Exception re) { |
| 156 | Log.e(TAG, "", re); |
| 157 | } finally { |
| 158 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 159 | } |
| 160 | } else { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 161 | try { |
| 162 | mServiceLock.readLock().lock(); |
| 163 | if (mService == null) { |
| 164 | if (VDBG) Log.d(TAG,"Binding service..."); |
| 165 | doBind(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 166 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 167 | } catch (Exception re) { |
| 168 | Log.e(TAG,"",re); |
| 169 | } finally { |
| 170 | mServiceLock.readLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | /** |
| 176 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 177 | * Bluetooth A2DP service. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 178 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | */ |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 180 | /*package*/ BluetoothA2dp(Context context, ServiceListener l) { |
| 181 | mContext = context; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 182 | mServiceListener = l; |
| 183 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 184 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 185 | if (mgr != null) { |
| 186 | try { |
| 187 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 188 | } catch (RemoteException e) { |
| 189 | Log.e(TAG,"",e); |
| 190 | } |
| 191 | } |
| 192 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 193 | doBind(); |
| 194 | } |
| 195 | |
| 196 | boolean doBind() { |
| 197 | Intent intent = new Intent(IBluetoothA2dp.class.getName()); |
| 198 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 199 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 200 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 201 | android.os.Process.myUserHandle())) { |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 202 | Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent); |
| 203 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 205 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 208 | /*package*/ void close() { |
| 209 | mServiceListener = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 210 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 211 | if (mgr != null) { |
| 212 | try { |
| 213 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 214 | } catch (Exception e) { |
| 215 | Log.e(TAG,"",e); |
| 216 | } |
| 217 | } |
| 218 | |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 219 | try { |
| 220 | mServiceLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 221 | if (mService != null) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 222 | mService = null; |
| 223 | mContext.unbindService(mConnection); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 224 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 225 | } catch (Exception re) { |
| 226 | Log.e(TAG, "", re); |
| 227 | } finally { |
| 228 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 229 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 230 | } |
| 231 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 232 | public void finalize() { |
Mathias Jeppsson | 2d2d8c2 | 2013-08-06 11:41:55 +0200 | [diff] [blame] | 233 | // The empty finalize needs to be kept or the |
| 234 | // cts signature tests would fail. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 235 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 236 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 237 | * Initiate connection to a profile of the remote bluetooth device. |
| 238 | * |
| 239 | * <p> Currently, the system supports only 1 connection to the |
| 240 | * A2DP profile. The API will automatically disconnect connected |
| 241 | * devices before connecting. |
| 242 | * |
| 243 | * <p> This API returns false in scenarios like the profile on the |
| 244 | * device is already connected or Bluetooth is not turned on. |
| 245 | * When this API returns true, it is guaranteed that |
| 246 | * connection state intent for the profile will be broadcasted with |
| 247 | * the state. Users can get the connection state of the profile |
| 248 | * from this intent. |
| 249 | * |
| 250 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 251 | * permission. |
| 252 | * |
| 253 | * @param device Remote Bluetooth Device |
| 254 | * @return false on immediate error, |
| 255 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 256 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 258 | public boolean connect(BluetoothDevice device) { |
| 259 | if (DBG) log("connect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 260 | try { |
| 261 | mServiceLock.readLock().lock(); |
| 262 | if (mService != null && isEnabled() && |
| 263 | isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 264 | return mService.connect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 265 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 266 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 267 | return false; |
| 268 | } catch (RemoteException e) { |
| 269 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 270 | return false; |
| 271 | } finally { |
| 272 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 276 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 277 | * Initiate disconnection from a profile |
| 278 | * |
| 279 | * <p> This API will return false in scenarios like the profile on the |
| 280 | * Bluetooth device is not in connected state etc. When this API returns, |
| 281 | * true, it is guaranteed that the connection state change |
| 282 | * intent will be broadcasted with the state. Users can get the |
| 283 | * disconnection state of the profile from this intent. |
| 284 | * |
| 285 | * <p> If the disconnection is initiated by a remote device, the state |
| 286 | * will transition from {@link #STATE_CONNECTED} to |
| 287 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 288 | * host (local) device the state will transition from |
| 289 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 290 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 291 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 292 | * two scenarios. |
| 293 | * |
| 294 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 295 | * permission. |
| 296 | * |
| 297 | * @param device Remote Bluetooth Device |
| 298 | * @return false on immediate error, |
| 299 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 300 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 302 | public boolean disconnect(BluetoothDevice device) { |
| 303 | if (DBG) log("disconnect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 304 | try { |
| 305 | mServiceLock.readLock().lock(); |
| 306 | if (mService != null && isEnabled() && |
| 307 | isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 308 | return mService.disconnect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 309 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 310 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 311 | return false; |
| 312 | } catch (RemoteException e) { |
| 313 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 314 | return false; |
| 315 | } finally { |
| 316 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 320 | /** |
| 321 | * {@inheritDoc} |
| 322 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 323 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 324 | if (VDBG) log("getConnectedDevices()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 325 | try { |
| 326 | mServiceLock.readLock().lock(); |
| 327 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 328 | return mService.getConnectedDevices(); |
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 new ArrayList<BluetoothDevice>(); |
| 332 | } catch (RemoteException e) { |
| 333 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 334 | return new ArrayList<BluetoothDevice>(); |
| 335 | } finally { |
| 336 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 337 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
| 341 | * {@inheritDoc} |
| 342 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 343 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 344 | if (VDBG) log("getDevicesMatchingStates()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 345 | try { |
| 346 | mServiceLock.readLock().lock(); |
| 347 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 348 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 349 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 350 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 351 | return new ArrayList<BluetoothDevice>(); |
| 352 | } catch (RemoteException e) { |
| 353 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 354 | return new ArrayList<BluetoothDevice>(); |
| 355 | } finally { |
| 356 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 357 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * {@inheritDoc} |
| 362 | */ |
| 363 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 364 | if (VDBG) log("getState(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 365 | try { |
| 366 | mServiceLock.readLock().lock(); |
| 367 | if (mService != null && isEnabled() |
| 368 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 369 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 370 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 371 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 372 | return BluetoothProfile.STATE_DISCONNECTED; |
| 373 | } catch (RemoteException e) { |
| 374 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 375 | return BluetoothProfile.STATE_DISCONNECTED; |
| 376 | } finally { |
| 377 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 378 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 382 | * Set priority of the profile |
| 383 | * |
| 384 | * <p> The device should already be paired. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 385 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 386 | * {@link #PRIORITY_OFF}, |
| 387 | * |
| 388 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 389 | * permission. |
| 390 | * |
| 391 | * @param device Paired bluetooth device |
| 392 | * @param priority |
| 393 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 394 | * @hide |
| 395 | */ |
| 396 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 397 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 398 | try { |
| 399 | mServiceLock.readLock().lock(); |
| 400 | if (mService != null && isEnabled() |
| 401 | && isValidDevice(device)) { |
| 402 | if (priority != BluetoothProfile.PRIORITY_OFF && |
| 403 | priority != BluetoothProfile.PRIORITY_ON) { |
| 404 | return false; |
| 405 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 406 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 407 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 408 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 409 | return false; |
| 410 | } catch (RemoteException e) { |
| 411 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 412 | return false; |
| 413 | } finally { |
| 414 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 415 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 419 | * Get the priority of the profile. |
| 420 | * |
| 421 | * <p> The priority can be any of: |
| 422 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 423 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 424 | * |
| 425 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 426 | * |
| 427 | * @param device Bluetooth device |
| 428 | * @return priority of the device |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 429 | * @hide |
| 430 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 431 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 432 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 433 | if (VDBG) log("getPriority(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 434 | try { |
| 435 | mServiceLock.readLock().lock(); |
| 436 | if (mService != null && isEnabled() |
| 437 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 438 | return mService.getPriority(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 439 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 440 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 441 | return BluetoothProfile.PRIORITY_OFF; |
| 442 | } catch (RemoteException e) { |
| 443 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 444 | return BluetoothProfile.PRIORITY_OFF; |
| 445 | } finally { |
| 446 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 447 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 451 | * Checks if Avrcp device supports the absolute volume feature. |
| 452 | * |
| 453 | * @return true if device supports absolute volume |
| 454 | * @hide |
| 455 | */ |
| 456 | public boolean isAvrcpAbsoluteVolumeSupported() { |
| 457 | if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 458 | try { |
| 459 | mServiceLock.readLock().lock(); |
| 460 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 461 | return mService.isAvrcpAbsoluteVolumeSupported(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 462 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 463 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 464 | return false; |
| 465 | } catch (RemoteException e) { |
| 466 | Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e); |
| 467 | return false; |
| 468 | } finally { |
| 469 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 470 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /** |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 474 | * Tells remote device to adjust volume. Only if absolute volume is |
| 475 | * supported. Uses the following values: |
| 476 | * <ul> |
| 477 | * <li>{@link AudioManager#ADJUST_LOWER}</li> |
| 478 | * <li>{@link AudioManager#ADJUST_RAISE}</li> |
| 479 | * <li>{@link AudioManager#ADJUST_MUTE}</li> |
| 480 | * <li>{@link AudioManager#ADJUST_UNMUTE}</li> |
| 481 | * </ul> |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 482 | * |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 483 | * @param direction One of the supported adjust values. |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 484 | * @hide |
| 485 | */ |
| 486 | public void adjustAvrcpAbsoluteVolume(int direction) { |
| 487 | if (DBG) Log.d(TAG, "adjustAvrcpAbsoluteVolume"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 488 | try { |
| 489 | mServiceLock.readLock().lock(); |
| 490 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 491 | mService.adjustAvrcpAbsoluteVolume(direction); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 492 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 493 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 494 | } catch (RemoteException e) { |
| 495 | Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e); |
| 496 | } finally { |
| 497 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 498 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Tells remote device to set an absolute volume. Only if absolute volume is supported |
| 503 | * |
| 504 | * @param volume Absolute volume to be set on AVRCP side |
| 505 | * @hide |
| 506 | */ |
| 507 | public void setAvrcpAbsoluteVolume(int volume) { |
| 508 | if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 509 | try { |
| 510 | mServiceLock.readLock().lock(); |
| 511 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 512 | mService.setAvrcpAbsoluteVolume(volume); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 513 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 514 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 515 | } catch (RemoteException e) { |
| 516 | Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e); |
| 517 | } finally { |
| 518 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 519 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 523 | * Check if A2DP profile is streaming music. |
| 524 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 525 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 526 | * |
| 527 | * @param device BluetoothDevice device |
| 528 | */ |
| 529 | public boolean isA2dpPlaying(BluetoothDevice device) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 530 | try { |
| 531 | mServiceLock.readLock().lock(); |
| 532 | if (mService != null && isEnabled() |
| 533 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 534 | return mService.isA2dpPlaying(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 535 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 536 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 537 | return false; |
| 538 | } catch (RemoteException e) { |
| 539 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 540 | return false; |
| 541 | } finally { |
| 542 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 543 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /** |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 547 | * This function checks if the remote device is an AVCRP |
| 548 | * target and thus whether we should send volume keys |
| 549 | * changes or not. |
| 550 | * @hide |
| 551 | */ |
| 552 | public boolean shouldSendVolumeKeys(BluetoothDevice device) { |
| 553 | if (isEnabled() && isValidDevice(device)) { |
| 554 | ParcelUuid[] uuids = device.getUuids(); |
| 555 | if (uuids == null) return false; |
| 556 | |
| 557 | for (ParcelUuid uuid: uuids) { |
| 558 | if (BluetoothUuid.isAvrcpTarget(uuid)) { |
| 559 | return true; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 566 | /** |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 567 | * Gets the current codec status (configuration and capability). |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 568 | * |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 569 | * @return the current codec status |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 570 | * @hide |
| 571 | */ |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 572 | public BluetoothCodecStatus getCodecStatus() { |
| 573 | if (DBG) Log.d(TAG, "getCodecStatus"); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 574 | try { |
| 575 | mServiceLock.readLock().lock(); |
| 576 | if (mService != null && isEnabled()) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 577 | return mService.getCodecStatus(); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 578 | } |
| 579 | if (mService == null) { |
| 580 | Log.w(TAG, "Proxy not attached to service"); |
| 581 | } |
| 582 | return null; |
| 583 | } catch (RemoteException e) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 584 | Log.e(TAG, "Error talking to BT service in getCodecStatus()", e); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 585 | return null; |
| 586 | } finally { |
| 587 | mServiceLock.readLock().unlock(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Sets the codec configuration preference. |
| 593 | * |
| 594 | * @param codecConfig the codec configuration preference |
| 595 | * @hide |
| 596 | */ |
| 597 | public void setCodecConfigPreference(BluetoothCodecConfig codecConfig) { |
| 598 | if (DBG) Log.d(TAG, "setCodecConfigPreference"); |
| 599 | try { |
| 600 | mServiceLock.readLock().lock(); |
| 601 | if (mService != null && isEnabled()) { |
| 602 | mService.setCodecConfigPreference(codecConfig); |
| 603 | } |
| 604 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 605 | return; |
| 606 | } catch (RemoteException e) { |
| 607 | Log.e(TAG, "Error talking to BT service in setCodecConfigPreference()", e); |
| 608 | return; |
| 609 | } finally { |
| 610 | mServiceLock.readLock().unlock(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 615 | * Helper for converting a state to a string. |
| 616 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | * For debug use only - strings are not internationalized. |
| 618 | * @hide |
| 619 | */ |
| 620 | public static String stateToString(int state) { |
| 621 | switch (state) { |
| 622 | case STATE_DISCONNECTED: |
| 623 | return "disconnected"; |
| 624 | case STATE_CONNECTING: |
| 625 | return "connecting"; |
| 626 | case STATE_CONNECTED: |
| 627 | return "connected"; |
| 628 | case STATE_DISCONNECTING: |
| 629 | return "disconnecting"; |
| 630 | case STATE_PLAYING: |
| 631 | return "playing"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 632 | case STATE_NOT_PLAYING: |
| 633 | return "not playing"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | default: |
| 635 | return "<unknown state " + state + ">"; |
| 636 | } |
| 637 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 638 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 639 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 640 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 641 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 642 | try { |
| 643 | mServiceLock.writeLock().lock(); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 644 | mService = IBluetoothA2dp.Stub.asInterface(Binder.allowBlocking(service)); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 645 | } finally { |
| 646 | mServiceLock.writeLock().unlock(); |
| 647 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 648 | |
| 649 | if (mServiceListener != null) { |
| 650 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this); |
| 651 | } |
| 652 | } |
| 653 | public void onServiceDisconnected(ComponentName className) { |
| 654 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 655 | try { |
| 656 | mServiceLock.writeLock().lock(); |
| 657 | mService = null; |
| 658 | } finally { |
| 659 | mServiceLock.writeLock().unlock(); |
| 660 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 661 | if (mServiceListener != null) { |
| 662 | mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP); |
| 663 | } |
| 664 | } |
| 665 | }; |
| 666 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 667 | private boolean isEnabled() { |
| 668 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | private boolean isValidDevice(BluetoothDevice device) { |
| 673 | if (device == null) return false; |
| 674 | |
| 675 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 676 | return false; |
| 677 | } |
| 678 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 679 | private static void log(String msg) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 680 | Log.d(TAG, msg); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | } |