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