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 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 111 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 112 | new IBluetoothStateChangeCallback.Stub() { |
| 113 | public void onBluetoothStateChange(boolean up) { |
| 114 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 115 | if (!up) { |
| 116 | if (DBG) Log.d(TAG,"Unbinding service..."); |
| 117 | synchronized (mConnection) { |
| 118 | try { |
| 119 | mService = null; |
| 120 | mContext.unbindService(mConnection); |
| 121 | } catch (Exception re) { |
| 122 | Log.e(TAG,"",re); |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | synchronized (mConnection) { |
| 127 | try { |
| 128 | if (mService == null) { |
| 129 | if (DBG) Log.d(TAG,"Binding service..."); |
| 130 | if (!mContext.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) { |
| 131 | Log.e(TAG, "Could not bind to Bluetooth A2DP Service"); |
| 132 | } |
| 133 | } |
| 134 | } catch (Exception re) { |
| 135 | Log.e(TAG,"",re); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 143 | * Bluetooth A2DP service. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 144 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | */ |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 146 | /*package*/ BluetoothA2dp(Context context, ServiceListener l) { |
| 147 | mContext = context; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 148 | mServiceListener = l; |
| 149 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 150 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 151 | if (mgr != null) { |
| 152 | try { |
| 153 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 154 | } catch (RemoteException e) { |
| 155 | Log.e(TAG,"",e); |
| 156 | } |
| 157 | } |
| 158 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 159 | if (!context.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) { |
| 160 | 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] | 161 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 164 | /*package*/ void close() { |
| 165 | mServiceListener = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 166 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 167 | if (mgr != null) { |
| 168 | try { |
| 169 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 170 | } catch (Exception e) { |
| 171 | Log.e(TAG,"",e); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | synchronized (mConnection) { |
| 176 | if (mService != null) { |
| 177 | try { |
| 178 | mService = null; |
| 179 | mContext.unbindService(mConnection); |
| 180 | } catch (Exception re) { |
| 181 | Log.e(TAG,"",re); |
| 182 | } |
| 183 | } |
| 184 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 185 | } |
| 186 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 187 | public void finalize() { |
| 188 | close(); |
| 189 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 190 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 191 | * Initiate connection to a profile of the remote bluetooth device. |
| 192 | * |
| 193 | * <p> Currently, the system supports only 1 connection to the |
| 194 | * A2DP profile. The API will automatically disconnect connected |
| 195 | * devices before connecting. |
| 196 | * |
| 197 | * <p> This API returns false in scenarios like the profile on the |
| 198 | * device is already connected or Bluetooth is not turned on. |
| 199 | * When this API returns true, it is guaranteed that |
| 200 | * connection state intent for the profile will be broadcasted with |
| 201 | * the state. Users can get the connection state of the profile |
| 202 | * from this intent. |
| 203 | * |
| 204 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 205 | * permission. |
| 206 | * |
| 207 | * @param device Remote Bluetooth Device |
| 208 | * @return false on immediate error, |
| 209 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 210 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 212 | public boolean connect(BluetoothDevice device) { |
| 213 | if (DBG) log("connect(" + device + ")"); |
| 214 | if (mService != null && isEnabled() && |
| 215 | isValidDevice(device)) { |
| 216 | try { |
| 217 | return mService.connect(device); |
| 218 | } catch (RemoteException e) { |
| 219 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 220 | return false; |
| 221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 223 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 224 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 227 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 228 | * Initiate disconnection from a profile |
| 229 | * |
| 230 | * <p> This API will return false in scenarios like the profile on the |
| 231 | * Bluetooth device is not in connected state etc. When this API returns, |
| 232 | * true, it is guaranteed that the connection state change |
| 233 | * intent will be broadcasted with the state. Users can get the |
| 234 | * disconnection state of the profile from this intent. |
| 235 | * |
| 236 | * <p> If the disconnection is initiated by a remote device, the state |
| 237 | * will transition from {@link #STATE_CONNECTED} to |
| 238 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 239 | * host (local) device the state will transition from |
| 240 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 241 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 242 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 243 | * two scenarios. |
| 244 | * |
| 245 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 246 | * permission. |
| 247 | * |
| 248 | * @param device Remote Bluetooth Device |
| 249 | * @return false on immediate error, |
| 250 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 251 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 253 | public boolean disconnect(BluetoothDevice device) { |
| 254 | if (DBG) log("disconnect(" + device + ")"); |
| 255 | if (mService != null && isEnabled() && |
| 256 | isValidDevice(device)) { |
| 257 | try { |
| 258 | return mService.disconnect(device); |
| 259 | } catch (RemoteException e) { |
| 260 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 261 | return false; |
| 262 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 264 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 265 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 268 | /** |
| 269 | * {@inheritDoc} |
| 270 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 271 | public List<BluetoothDevice> getConnectedDevices() { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 272 | if (DBG) log("getConnectedDevices()"); |
| 273 | if (mService != null && isEnabled()) { |
| 274 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 275 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 276 | } catch (RemoteException e) { |
| 277 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 278 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 282 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * {@inheritDoc} |
| 287 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 288 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 289 | if (DBG) log("getDevicesMatchingStates()"); |
| 290 | if (mService != null && isEnabled()) { |
| 291 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 292 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 293 | } catch (RemoteException e) { |
| 294 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 295 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 299 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * {@inheritDoc} |
| 304 | */ |
| 305 | public int getConnectionState(BluetoothDevice device) { |
| 306 | if (DBG) log("getState(" + device + ")"); |
| 307 | if (mService != null && isEnabled() |
| 308 | && isValidDevice(device)) { |
| 309 | try { |
| 310 | return mService.getConnectionState(device); |
| 311 | } catch (RemoteException e) { |
| 312 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 313 | return BluetoothProfile.STATE_DISCONNECTED; |
| 314 | } |
| 315 | } |
| 316 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 317 | return BluetoothProfile.STATE_DISCONNECTED; |
| 318 | } |
| 319 | |
| 320 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 321 | * Set priority of the profile |
| 322 | * |
| 323 | * <p> The device should already be paired. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 324 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 325 | * {@link #PRIORITY_OFF}, |
| 326 | * |
| 327 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 328 | * permission. |
| 329 | * |
| 330 | * @param device Paired bluetooth device |
| 331 | * @param priority |
| 332 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 333 | * @hide |
| 334 | */ |
| 335 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 336 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| 337 | if (mService != null && isEnabled() |
| 338 | && isValidDevice(device)) { |
| 339 | if (priority != BluetoothProfile.PRIORITY_OFF && |
Ganesh Ganapathi Batta | 6f6c5451 | 2012-07-31 16:08:17 -0700 | [diff] [blame^] | 340 | priority != BluetoothProfile.PRIORITY_ON){ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | try { |
| 344 | return mService.setPriority(device, priority); |
| 345 | } catch (RemoteException e) { |
| 346 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 355 | * Get the priority of the profile. |
| 356 | * |
| 357 | * <p> The priority can be any of: |
| 358 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 359 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 360 | * |
| 361 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 362 | * |
| 363 | * @param device Bluetooth device |
| 364 | * @return priority of the device |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 365 | * @hide |
| 366 | */ |
| 367 | public int getPriority(BluetoothDevice device) { |
| 368 | if (DBG) log("getPriority(" + device + ")"); |
| 369 | if (mService != null && isEnabled() |
| 370 | && isValidDevice(device)) { |
| 371 | try { |
| 372 | return mService.getPriority(device); |
| 373 | } catch (RemoteException e) { |
| 374 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 375 | return BluetoothProfile.PRIORITY_OFF; |
| 376 | } |
| 377 | } |
| 378 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 379 | return BluetoothProfile.PRIORITY_OFF; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Check if A2DP profile is streaming music. |
| 384 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 385 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 386 | * |
| 387 | * @param device BluetoothDevice device |
| 388 | */ |
| 389 | public boolean isA2dpPlaying(BluetoothDevice device) { |
| 390 | if (mService != null && isEnabled() |
| 391 | && isValidDevice(device)) { |
| 392 | try { |
| 393 | return mService.isA2dpPlaying(device); |
| 394 | } catch (RemoteException e) { |
| 395 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | /** |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 404 | * This function checks if the remote device is an AVCRP |
| 405 | * target and thus whether we should send volume keys |
| 406 | * changes or not. |
| 407 | * @hide |
| 408 | */ |
| 409 | public boolean shouldSendVolumeKeys(BluetoothDevice device) { |
| 410 | if (isEnabled() && isValidDevice(device)) { |
| 411 | ParcelUuid[] uuids = device.getUuids(); |
| 412 | if (uuids == null) return false; |
| 413 | |
| 414 | for (ParcelUuid uuid: uuids) { |
| 415 | if (BluetoothUuid.isAvrcpTarget(uuid)) { |
| 416 | return true; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 423 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 424 | * Helper for converting a state to a string. |
| 425 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | * For debug use only - strings are not internationalized. |
| 427 | * @hide |
| 428 | */ |
| 429 | public static String stateToString(int state) { |
| 430 | switch (state) { |
| 431 | case STATE_DISCONNECTED: |
| 432 | return "disconnected"; |
| 433 | case STATE_CONNECTING: |
| 434 | return "connecting"; |
| 435 | case STATE_CONNECTED: |
| 436 | return "connected"; |
| 437 | case STATE_DISCONNECTING: |
| 438 | return "disconnecting"; |
| 439 | case STATE_PLAYING: |
| 440 | return "playing"; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 441 | case STATE_NOT_PLAYING: |
| 442 | return "not playing"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | default: |
| 444 | return "<unknown state " + state + ">"; |
| 445 | } |
| 446 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 447 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 448 | private ServiceConnection mConnection = new ServiceConnection() { |
| 449 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 450 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| 451 | mService = IBluetoothA2dp.Stub.asInterface(service); |
| 452 | |
| 453 | if (mServiceListener != null) { |
| 454 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this); |
| 455 | } |
| 456 | } |
| 457 | public void onServiceDisconnected(ComponentName className) { |
| 458 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 459 | mService = null; |
| 460 | if (mServiceListener != null) { |
| 461 | mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP); |
| 462 | } |
| 463 | } |
| 464 | }; |
| 465 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 466 | private boolean isEnabled() { |
| 467 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | private boolean isValidDevice(BluetoothDevice device) { |
| 472 | if (device == null) return false; |
| 473 | |
| 474 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 475 | return false; |
| 476 | } |
| 477 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 478 | private static void log(String msg) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 479 | Log.d(TAG, msg); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |