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 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.os.IBinder; |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 23 | import android.os.ParcelUuid; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.server.BluetoothA2dpService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 34 | * This class provides the public APIs to control the Bluetooth A2DP |
| 35 | * profile. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 37 | *<p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP |
| 38 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 39 | * the BluetoothA2dp proxy object. |
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> Android only supports one connected Bluetooth A2dp device at a time. |
| 42 | * Each method is protected with its appropriate permission. |
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 | public final class BluetoothA2dp implements BluetoothProfile { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | private static final String TAG = "BluetoothA2dp"; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 46 | private static final boolean DBG = false; |
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 | /** |
| 49 | * Intent used to broadcast the change in connection state of the A2DP |
| 50 | * profile. |
| 51 | * |
| 52 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 53 | * <ul> |
| 54 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 55 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 56 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 57 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 58 | * |
| 59 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 60 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 61 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 62 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 63 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 64 | * receive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | */ |
| 66 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 67 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 68 | "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Intent used to broadcast the change in the Playing state of the A2DP |
| 72 | * profile. |
| 73 | * |
| 74 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 75 | * <ul> |
| 76 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 77 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 78 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. <li/> |
| 79 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 80 | * |
| 81 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 82 | * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING}, |
| 83 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 84 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 85 | * receive. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 86 | */ |
| 87 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 88 | public static final String ACTION_PLAYING_STATE_CHANGED = |
| 89 | "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 91 | /** |
| 92 | * A2DP sink device is streaming music. This state can be one of |
| 93 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 94 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 95 | */ |
| 96 | public static final int STATE_PLAYING = 10; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 97 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 98 | /** |
| 99 | * A2DP sink device is NOT streaming music. This state can be one of |
| 100 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 101 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 102 | */ |
| 103 | public static final int STATE_NOT_PLAYING = 11; |
| 104 | |
| 105 | private ServiceListener mServiceListener; |
| 106 | private IBluetoothA2dp mService; |
| 107 | private BluetoothAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 111 | * Bluetooth A2DP service. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 112 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 114 | /*package*/ BluetoothA2dp(Context mContext, ServiceListener l) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | IBinder b = ServiceManager.getService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 116 | mServiceListener = l; |
| 117 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Michael Chan | 081f58a | 2009-09-30 17:59:40 -0700 | [diff] [blame] | 118 | if (b != null) { |
| 119 | mService = IBluetoothA2dp.Stub.asInterface(b); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 120 | if (mServiceListener != null) { |
| 121 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, this); |
| 122 | } |
Michael Chan | 081f58a | 2009-09-30 17:59:40 -0700 | [diff] [blame] | 123 | } else { |
| 124 | Log.w(TAG, "Bluetooth A2DP service not available!"); |
| 125 | |
| 126 | // Instead of throwing an exception which prevents people from going |
| 127 | // into Wireless settings in the emulator. Let it crash later when it is actually used. |
| 128 | mService = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 132 | /** |
| 133 | * {@inheritDoc} |
| 134 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 136 | public boolean connect(BluetoothDevice device) { |
| 137 | if (DBG) log("connect(" + device + ")"); |
| 138 | if (mService != null && isEnabled() && |
| 139 | isValidDevice(device)) { |
| 140 | try { |
| 141 | return mService.connect(device); |
| 142 | } catch (RemoteException e) { |
| 143 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 144 | return false; |
| 145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 147 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 148 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 151 | /** |
| 152 | * {@inheritDoc} |
| 153 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 155 | public boolean disconnect(BluetoothDevice device) { |
| 156 | if (DBG) log("disconnect(" + device + ")"); |
| 157 | if (mService != null && isEnabled() && |
| 158 | isValidDevice(device)) { |
| 159 | try { |
| 160 | return mService.disconnect(device); |
| 161 | } catch (RemoteException e) { |
| 162 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 163 | return false; |
| 164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 166 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 167 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 170 | /** |
| 171 | * {@inheritDoc} |
| 172 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 173 | public List<BluetoothDevice> getConnectedDevices() { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 174 | if (DBG) log("getConnectedDevices()"); |
| 175 | if (mService != null && isEnabled()) { |
| 176 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 177 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 178 | } catch (RemoteException e) { |
| 179 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 180 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 184 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * {@inheritDoc} |
| 189 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 190 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 191 | if (DBG) log("getDevicesMatchingStates()"); |
| 192 | if (mService != null && isEnabled()) { |
| 193 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 194 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 195 | } catch (RemoteException e) { |
| 196 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 197 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 201 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * {@inheritDoc} |
| 206 | */ |
| 207 | public int getConnectionState(BluetoothDevice device) { |
| 208 | if (DBG) log("getState(" + device + ")"); |
| 209 | if (mService != null && isEnabled() |
| 210 | && isValidDevice(device)) { |
| 211 | try { |
| 212 | return mService.getConnectionState(device); |
| 213 | } catch (RemoteException e) { |
| 214 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 215 | return BluetoothProfile.STATE_DISCONNECTED; |
| 216 | } |
| 217 | } |
| 218 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 219 | return BluetoothProfile.STATE_DISCONNECTED; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * {@inheritDoc} |
| 224 | * @hide |
| 225 | */ |
| 226 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 227 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| 228 | if (mService != null && isEnabled() |
| 229 | && isValidDevice(device)) { |
| 230 | if (priority != BluetoothProfile.PRIORITY_OFF && |
| 231 | priority != BluetoothProfile.PRIORITY_ON) { |
| 232 | return false; |
| 233 | } |
| 234 | try { |
| 235 | return mService.setPriority(device, priority); |
| 236 | } catch (RemoteException e) { |
| 237 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * {@inheritDoc} |
| 247 | * @hide |
| 248 | */ |
| 249 | public int getPriority(BluetoothDevice device) { |
| 250 | if (DBG) log("getPriority(" + device + ")"); |
| 251 | if (mService != null && isEnabled() |
| 252 | && isValidDevice(device)) { |
| 253 | try { |
| 254 | return mService.getPriority(device); |
| 255 | } catch (RemoteException e) { |
| 256 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 257 | return BluetoothProfile.PRIORITY_OFF; |
| 258 | } |
| 259 | } |
| 260 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 261 | return BluetoothProfile.PRIORITY_OFF; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Check if A2DP profile is streaming music. |
| 266 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 267 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 268 | * |
| 269 | * @param device BluetoothDevice device |
| 270 | */ |
| 271 | public boolean isA2dpPlaying(BluetoothDevice device) { |
| 272 | if (mService != null && isEnabled() |
| 273 | && isValidDevice(device)) { |
| 274 | try { |
| 275 | return mService.isA2dpPlaying(device); |
| 276 | } catch (RemoteException e) { |
| 277 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Initiate suspend from an A2DP sink. |
| 287 | * |
| 288 | * <p> This API will return false in scenarios like the A2DP |
| 289 | * device is not in connected state etc. When this API returns, |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 290 | * true, it is guaranteed that {@link #ACTION_CONNECTION_STATE_CHANGED} |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 291 | * intent will be broadcasted with the state. Users can get the |
| 292 | * state of the A2DP device from this intent. |
| 293 | * |
| 294 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 295 | * permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 296 | * |
| 297 | * @param device Remote A2DP sink |
| 298 | * @return false on immediate error, |
| 299 | * true otherwise |
| 300 | * @hide |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 301 | */ |
Nick Pelly | e766eae | 2009-09-30 12:06:28 -0700 | [diff] [blame] | 302 | public boolean suspendSink(BluetoothDevice device) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 303 | if (mService != null && isEnabled() |
| 304 | && isValidDevice(device)) { |
| 305 | try { |
| 306 | return mService.suspendSink(device); |
| 307 | } catch (RemoteException e) { |
| 308 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 309 | return false; |
| 310 | } |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 311 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 312 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 313 | return false; |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 314 | } |
| 315 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Initiate resume from a suspended A2DP sink. |
| 318 | * |
| 319 | * <p> This API will return false in scenarios like the A2DP |
| 320 | * device is not in suspended state etc. When this API returns, |
| 321 | * true, it is guaranteed that {@link #ACTION_SINK_STATE_CHANGED} |
| 322 | * intent will be broadcasted with the state. Users can get the |
| 323 | * state of the A2DP device from this intent. |
| 324 | * |
| 325 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 326 | * |
| 327 | * @param device Remote A2DP sink |
| 328 | * @return false on immediate error, |
| 329 | * true otherwise |
| 330 | * @hide |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 331 | */ |
Nick Pelly | e766eae | 2009-09-30 12:06:28 -0700 | [diff] [blame] | 332 | public boolean resumeSink(BluetoothDevice device) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 333 | if (mService != null && isEnabled() |
| 334 | && isValidDevice(device)) { |
| 335 | try { |
| 336 | return mService.resumeSink(device); |
| 337 | } catch (RemoteException e) { |
| 338 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 339 | return false; |
| 340 | } |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 341 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 342 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 343 | return false; |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 344 | } |
| 345 | |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 346 | /** |
| 347 | * This function checks if the remote device is an AVCRP |
| 348 | * target and thus whether we should send volume keys |
| 349 | * changes or not. |
| 350 | * @hide |
| 351 | */ |
| 352 | public boolean shouldSendVolumeKeys(BluetoothDevice device) { |
| 353 | if (isEnabled() && isValidDevice(device)) { |
| 354 | ParcelUuid[] uuids = device.getUuids(); |
| 355 | if (uuids == null) return false; |
| 356 | |
| 357 | for (ParcelUuid uuid: uuids) { |
| 358 | if (BluetoothUuid.isAvrcpTarget(uuid)) { |
| 359 | return true; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 366 | /** |
| 367 | * Helper for converting a state to a string. |
| 368 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | * For debug use only - strings are not internationalized. |
| 370 | * @hide |
| 371 | */ |
| 372 | public static String stateToString(int state) { |
| 373 | switch (state) { |
| 374 | case STATE_DISCONNECTED: |
| 375 | return "disconnected"; |
| 376 | case STATE_CONNECTING: |
| 377 | return "connecting"; |
| 378 | case STATE_CONNECTED: |
| 379 | return "connected"; |
| 380 | case STATE_DISCONNECTING: |
| 381 | return "disconnecting"; |
| 382 | case STATE_PLAYING: |
| 383 | return "playing"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 384 | case STATE_NOT_PLAYING: |
| 385 | return "not playing"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | default: |
| 387 | return "<unknown state " + state + ">"; |
| 388 | } |
| 389 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 390 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 391 | private boolean isEnabled() { |
| 392 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | private boolean isValidDevice(BluetoothDevice device) { |
| 397 | if (device == null) return false; |
| 398 | |
| 399 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 400 | return false; |
| 401 | } |
| 402 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 403 | private static void log(String msg) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 404 | Log.d(TAG, msg); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |