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