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