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. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | * |
| 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 | |
| 77 | /** Default priority for a2dp devices that should allow incoming |
| 78 | * connections */ |
| 79 | public static final int PRIORITY_AUTO = 100; |
| 80 | /** Default priority for a2dp devices that should not allow incoming |
| 81 | * connections */ |
| 82 | public static final int PRIORITY_OFF = 0; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private final IBluetoothA2dp mService; |
| 85 | private final Context mContext; |
| 86 | |
| 87 | /** |
| 88 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 89 | * Bluetooth A2DP service. |
| 90 | * @param c Context |
| 91 | */ |
| 92 | public BluetoothA2dp(Context c) { |
| 93 | mContext = c; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | IBinder b = ServiceManager.getService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE); |
| 96 | if (b == null) { |
| 97 | throw new RuntimeException("Bluetooth A2DP service not available!"); |
| 98 | } |
| 99 | mService = IBluetoothA2dp.Stub.asInterface(b); |
| 100 | } |
| 101 | |
| 102 | /** Initiate a connection to an A2DP sink. |
| 103 | * Listen for SINK_STATE_CHANGED_ACTION to find out when the |
| 104 | * connection is completed. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 105 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 106 | * @return false on immediate error, true otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | * @hide |
| 108 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 109 | public boolean connectSink(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 110 | if (DBG) log("connectSink(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 112 | return mService.connectSink(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 114 | Log.e(TAG, "", e); |
| 115 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** Initiate disconnect from an A2DP sink. |
| 120 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 121 | * disconnect is completed. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 122 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 123 | * @return false on immediate error, true otherwise |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | * @hide |
| 125 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 126 | public boolean disconnectSink(BluetoothDevice device) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 127 | if (DBG) log("disconnectSink(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 129 | return mService.disconnectSink(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 131 | Log.e(TAG, "", e); |
| 132 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Zhu Lan | f9bbe1e | 2009-06-24 10:51:57 +0800 | [diff] [blame^] | 136 | /** Initiate suspend from an A2DP sink. |
| 137 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 138 | * suspend is completed. |
| 139 | * @param device Remote BT device. |
| 140 | * @return false on immediate error, true otherwise |
| 141 | * @hide |
| 142 | */ |
| 143 | public int suspendSink(BluetoothDevice device) { |
| 144 | try { |
| 145 | return mService.suspendSink(device); |
| 146 | } catch (RemoteException e) { |
| 147 | Log.e(TAG, "", e); |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** Initiate resume from an suspended A2DP sink. |
| 153 | * Listen for SINK_STATE_CHANGED_ACTION to find out when |
| 154 | * resume is completed. |
| 155 | * @param device Remote BT device. |
| 156 | * @return false on immediate error, true otherwise |
| 157 | * @hide |
| 158 | */ |
| 159 | public int resumeSink(BluetoothDevice device) { |
| 160 | try { |
| 161 | return mService.resumeSink(device); |
| 162 | } catch (RemoteException e) { |
| 163 | Log.e(TAG, "", e); |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | /** Check if a specified A2DP sink is connected. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 169 | * @param device Remote BT device. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | * @return True if connected (or playing), false otherwise and on error. |
| 171 | * @hide |
| 172 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 173 | public boolean isSinkConnected(BluetoothDevice device) { |
| 174 | if (DBG) log("isSinkConnected(" + device + ")"); |
| 175 | int state = getSinkState(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | return state == STATE_CONNECTED || state == STATE_PLAYING; |
| 177 | } |
| 178 | |
| 179 | /** Check if any A2DP sink is connected. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 180 | * @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] | 181 | * @hide |
| 182 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 183 | public Set<BluetoothDevice> getConnectedSinks() { |
| 184 | if (DBG) log("getConnectedSinks()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 186 | return Collections.unmodifiableSet( |
| 187 | new HashSet<BluetoothDevice>(Arrays.asList(mService.getConnectedSinks()))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 189 | Log.e(TAG, "", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | return null; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** Get the state of an A2DP sink |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 195 | * @param device Remote BT device. |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 196 | * @return State code, one of STATE_ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | * @hide |
| 198 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 199 | public int getSinkState(BluetoothDevice device) { |
| 200 | if (DBG) log("getSinkState(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 202 | return mService.getSinkState(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 204 | Log.e(TAG, "", e); |
| 205 | return BluetoothA2dp.STATE_DISCONNECTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Set priority of a2dp sink. |
| 211 | * Priority is a non-negative integer. By default paired sinks will have |
| 212 | * a priority of PRIORITY_AUTO, and unpaired headset PRIORITY_NONE (0). |
| 213 | * Sinks with priority greater than zero will accept incoming connections |
| 214 | * (if no sink is currently connected). |
| 215 | * Priority for unpaired sink must be PRIORITY_NONE. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 216 | * @param device Paired sink |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * @param priority Integer priority, for example PRIORITY_AUTO or |
| 218 | * PRIORITY_NONE |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 219 | * @return true if priority is set, false on error |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | */ |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 221 | public boolean setSinkPriority(BluetoothDevice device, int priority) { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 222 | if (DBG) log("setSinkPriority(" + device + ", " + priority + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 224 | return mService.setSinkPriority(device, priority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 226 | Log.e(TAG, "", e); |
| 227 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get priority of a2dp sink. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 233 | * @param device Sink |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | * @return non-negative priority, or negative error code on error. |
| 235 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 236 | public int getSinkPriority(BluetoothDevice device) { |
| 237 | if (DBG) log("getSinkPriority(" + device + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 239 | return mService.getSinkPriority(device); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } catch (RemoteException e) { |
Nick Pelly | b24e11b | 2009-09-08 17:40:43 -0700 | [diff] [blame] | 241 | Log.e(TAG, "", e); |
| 242 | return PRIORITY_OFF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | /** Helper for converting a state to a string. |
| 247 | * For debug use only - strings are not internationalized. |
| 248 | * @hide |
| 249 | */ |
| 250 | public static String stateToString(int state) { |
| 251 | switch (state) { |
| 252 | case STATE_DISCONNECTED: |
| 253 | return "disconnected"; |
| 254 | case STATE_CONNECTING: |
| 255 | return "connecting"; |
| 256 | case STATE_CONNECTED: |
| 257 | return "connected"; |
| 258 | case STATE_DISCONNECTING: |
| 259 | return "disconnecting"; |
| 260 | case STATE_PLAYING: |
| 261 | return "playing"; |
| 262 | default: |
| 263 | return "<unknown state " + state + ">"; |
| 264 | } |
| 265 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 266 | |
| 267 | private static void log(String msg) { |
| 268 | Log.d(TAG, msg); |
| 269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |