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; |
| 21 | import android.server.BluetoothA2dpService; |
| 22 | import android.content.Context; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.IBinder; |
| 26 | import android.util.Log; |
| 27 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | import java.util.Collections; |
| 30 | import java.util.Set; |
| 31 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Public API for controlling the Bluetooth A2DP Profile Service. |
| 35 | * |
| 36 | * BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP |
| 37 | * Service via IPC. |
| 38 | * |
| 39 | * Creating a BluetoothA2dp object will initiate a binding with the |
| 40 | * BluetoothHeadset service. Users of this object should call close() when they |
| 41 | * are finished, so that this proxy object can unbind from the service. |
| 42 | * |
| 43 | * Currently the BluetoothA2dp service runs in the system server and this |
| 44 | * proxy object will be immediately bound to the service on construction. |
Jaikumar Ganesh | 721361f | 2009-11-20 15:21:47 -0800 | [diff] [blame] | 45 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | * Currently this class provides methods to connect to A2DP audio sinks. |
| 47 | * |
| 48 | * @hide |
| 49 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 50 | public final class BluetoothA2dp { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | private static final String TAG = "BluetoothA2dp"; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 52 | private static final boolean DBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 54 | /** int extra for ACTION_SINK_STATE_CHANGED */ |
| 55 | public static final String EXTRA_SINK_STATE = |
| 56 | "android.bluetooth.a2dp.extra.SINK_STATE"; |
| 57 | /** int extra for ACTION_SINK_STATE_CHANGED */ |
| 58 | public static final String EXTRA_PREVIOUS_SINK_STATE = |
| 59 | "android.bluetooth.a2dp.extra.PREVIOUS_SINK_STATE"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | /** Indicates the state of an A2DP audio sink has changed. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 62 | * This intent will always contain EXTRA_SINK_STATE, |
| 63 | * EXTRA_PREVIOUS_SINK_STATE and BluetoothDevice.EXTRA_DEVICE |
| 64 | * extras. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | */ |
| 66 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 67 | public static final String ACTION_SINK_STATE_CHANGED = |
| 68 | "android.bluetooth.a2dp.action.SINK_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | public static final int STATE_DISCONNECTED = 0; |
| 71 | public static final int STATE_CONNECTING = 1; |
| 72 | public static final int STATE_CONNECTED = 2; |
| 73 | public static final int STATE_DISCONNECTING = 3; |
| 74 | /** Playing implies connected */ |
| 75 | public static final int STATE_PLAYING = 4; |
| 76 | |
Jaikumar Ganesh | 721361f | 2009-11-20 15:21:47 -0800 | [diff] [blame] | 77 | /** Default priority for a2dp devices that we try to auto-connect |
| 78 | * and allow incoming connections */ |
| 79 | public static final int PRIORITY_AUTO_CONNECT = 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | /** Default priority for a2dp devices that should allow incoming |
| 81 | * connections */ |
Jaikumar Ganesh | 721361f | 2009-11-20 15:21:47 -0800 | [diff] [blame] | 82 | public static final int PRIORITY_ON = 100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** Default priority for a2dp devices that should not allow incoming |
| 84 | * connections */ |
| 85 | public static final int PRIORITY_OFF = 0; |
Jaikumar Ganesh | 551ed72 | 2009-12-11 12:00:31 -0800 | [diff] [blame] | 86 | /** Default priority when not set or when the device is unpaired */ |
| 87 | public static final int PRIORITY_UNDEFINED = -1; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | private final IBluetoothA2dp mService; |
| 90 | private final Context mContext; |
| 91 | |
| 92 | /** |
| 93 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 94 | * Bluetooth A2DP service. |
| 95 | * @param c Context |
| 96 | */ |
| 97 | public BluetoothA2dp(Context c) { |
| 98 | mContext = c; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | IBinder b = ServiceManager.getService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE); |
Michael Chan | 081f58a | 2009-09-30 17:59:40 -0700 | [diff] [blame] | 101 | if (b != null) { |
| 102 | mService = IBluetoothA2dp.Stub.asInterface(b); |
| 103 | } else { |
| 104 | Log.w(TAG, "Bluetooth A2DP service not available!"); |
| 105 | |
| 106 | // Instead of throwing an exception which prevents people from going |
| 107 | // into Wireless settings in the emulator. Let it crash later when it is actually used. |
| 108 | mService = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** Initiate a connection to an A2DP sink. |
| 113 | * Listen for SINK_STATE_CHANGED_ACTION to find out when the |
| 114 | * connection is completed. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 115 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 116 | * @return false on immediate error, true otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | * @hide |
| 118 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 119 | public boolean connectSink(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 120 | if (DBG) log("connectSink(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 122 | return mService.connectSink(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 124 | Log.e(TAG, "", e); |
| 125 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** Initiate disconnect from an A2DP sink. |
| 130 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 131 | * disconnect is completed. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 132 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 133 | * @return false on immediate error, true otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | * @hide |
| 135 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 136 | public boolean disconnectSink(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 137 | if (DBG) log("disconnectSink(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 139 | return mService.disconnectSink(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 141 | Log.e(TAG, "", e); |
| 142 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 146 | /** Initiate suspend from an A2DP sink. |
| 147 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 148 | * suspend is completed. |
| 149 | * @param device Remote BT device. |
| 150 | * @return false on immediate error, true otherwise |
| 151 | * @hide |
| 152 | */ |
Nick Pelly | e766eae | 2009-09-30 12:06:28 -0700 | [diff] [blame] | 153 | public boolean suspendSink(BluetoothDevice device) { |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 154 | try { |
| 155 | return mService.suspendSink(device); |
| 156 | } catch (RemoteException e) { |
| 157 | Log.e(TAG, "", e); |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** Initiate resume from an suspended A2DP sink. |
| 163 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 164 | * resume is completed. |
| 165 | * @param device Remote BT device. |
| 166 | * @return false on immediate error, true otherwise |
| 167 | * @hide |
| 168 | */ |
Nick Pelly | e766eae | 2009-09-30 12:06:28 -0700 | [diff] [blame] | 169 | public boolean resumeSink(BluetoothDevice device) { |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame] | 170 | try { |
| 171 | return mService.resumeSink(device); |
| 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, "", e); |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | /** Check if a specified A2DP sink is connected. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 179 | * @param device Remote BT device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | * @return True if connected (or playing), false otherwise and on error. |
| 181 | * @hide |
| 182 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 183 | public boolean isSinkConnected(BluetoothDevice device) { |
| 184 | if (DBG) log("isSinkConnected(" + device + ")"); |
| 185 | int state = getSinkState(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | return state == STATE_CONNECTED || state == STATE_PLAYING; |
| 187 | } |
| 188 | |
| 189 | /** Check if any A2DP sink is connected. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 190 | * @return a unmodifiable set of connected A2DP sinks, or null on error. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | * @hide |
| 192 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 193 | public Set<BluetoothDevice> getConnectedSinks() { |
| 194 | if (DBG) log("getConnectedSinks()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 196 | return Collections.unmodifiableSet( |
| 197 | new HashSet<BluetoothDevice>(Arrays.asList(mService.getConnectedSinks()))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 199 | Log.e(TAG, "", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | return null; |
| 201 | } |
| 202 | } |
| 203 | |
Jaikumar Ganesh | b16c4f7 | 2009-12-04 15:10:54 -0800 | [diff] [blame] | 204 | /** Check if any A2DP sink is in Non Disconnected state |
| 205 | * i.e playing, connected, connecting, disconnecting. |
| 206 | * @return a unmodifiable set of connected A2DP sinks, or null on error. |
| 207 | * @hide |
| 208 | */ |
| 209 | public Set<BluetoothDevice> getNonDisconnectedSinks() { |
| 210 | if (DBG) log("getNonDisconnectedSinks()"); |
| 211 | try { |
| 212 | return Collections.unmodifiableSet( |
| 213 | new HashSet<BluetoothDevice>(Arrays.asList(mService.getNonDisconnectedSinks()))); |
| 214 | } catch (RemoteException e) { |
| 215 | Log.e(TAG, "", e); |
| 216 | return null; |
| 217 | } |
| 218 | } |
| 219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | /** Get the state of an A2DP sink |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 221 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 222 | * @return State code, one of STATE_ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | * @hide |
| 224 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 225 | public int getSinkState(BluetoothDevice device) { |
| 226 | if (DBG) log("getSinkState(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 228 | return mService.getSinkState(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 230 | Log.e(TAG, "", e); |
| 231 | return BluetoothA2dp.STATE_DISCONNECTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Set priority of a2dp sink. |
| 237 | * Priority is a non-negative integer. By default paired sinks will have |
| 238 | * a priority of PRIORITY_AUTO, and unpaired headset PRIORITY_NONE (0). |
| 239 | * Sinks with priority greater than zero will accept incoming connections |
| 240 | * (if no sink is currently connected). |
| 241 | * Priority for unpaired sink must be PRIORITY_NONE. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 242 | * @param device Paired sink |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | * @param priority Integer priority, for example PRIORITY_AUTO or |
| 244 | * PRIORITY_NONE |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 245 | * @return true if priority is set, false on error |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 247 | public boolean setSinkPriority(BluetoothDevice device, int priority) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 248 | if (DBG) log("setSinkPriority(" + device + ", " + priority + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 250 | return mService.setSinkPriority(device, priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 252 | Log.e(TAG, "", e); |
| 253 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get priority of a2dp sink. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 259 | * @param device Sink |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | * @return non-negative priority, or negative error code on error. |
| 261 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 262 | public int getSinkPriority(BluetoothDevice device) { |
| 263 | if (DBG) log("getSinkPriority(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 265 | return mService.getSinkPriority(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 267 | Log.e(TAG, "", e); |
| 268 | return PRIORITY_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | /** Helper for converting a state to a string. |
| 273 | * For debug use only - strings are not internationalized. |
| 274 | * @hide |
| 275 | */ |
| 276 | public static String stateToString(int state) { |
| 277 | switch (state) { |
| 278 | case STATE_DISCONNECTED: |
| 279 | return "disconnected"; |
| 280 | case STATE_CONNECTING: |
| 281 | return "connecting"; |
| 282 | case STATE_CONNECTED: |
| 283 | return "connected"; |
| 284 | case STATE_DISCONNECTING: |
| 285 | return "disconnecting"; |
| 286 | case STATE_PLAYING: |
| 287 | return "playing"; |
| 288 | default: |
| 289 | return "<unknown state " + state + ">"; |
| 290 | } |
| 291 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 292 | |
| 293 | private static void log(String msg) { |
| 294 | Log.d(TAG, msg); |
| 295 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |