Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 24 | import android.os.Binder; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * This class provides the public APIs to control the Bluetooth A2DP Sink |
| 34 | * profile. |
| 35 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 36 | * <p>BluetoothA2dpSink is a proxy object for controlling the Bluetooth A2DP Sink |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 37 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 38 | * the BluetoothA2dpSink proxy object. |
| 39 | * |
| 40 | * @hide |
| 41 | */ |
| 42 | public final class BluetoothA2dpSink implements BluetoothProfile { |
| 43 | private static final String TAG = "BluetoothA2dpSink"; |
| 44 | private static final boolean DBG = true; |
| 45 | private static final boolean VDBG = false; |
| 46 | |
| 47 | /** |
| 48 | * Intent used to broadcast the change in connection state of the A2DP Sink |
| 49 | * profile. |
| 50 | * |
| 51 | * <p>This intent will have 3 extras: |
| 52 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 53 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 54 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 55 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 56 | * </ul> |
| 57 | * |
| 58 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 59 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 60 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 61 | * |
| 62 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 63 | * receive. |
| 64 | */ |
| 65 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 66 | "android.bluetooth.a2dp-sink.profile.action.CONNECTION_STATE_CHANGED"; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Intent used to broadcast the change in the Playing state of the A2DP Sink |
| 70 | * profile. |
| 71 | * |
| 72 | * <p>This intent will have 3 extras: |
| 73 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 74 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 75 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 76 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 77 | * </ul> |
| 78 | * |
| 79 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 80 | * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING}, |
| 81 | * |
| 82 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 83 | * receive. |
| 84 | */ |
| 85 | public static final String ACTION_PLAYING_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 86 | "android.bluetooth.a2dp-sink.profile.action.PLAYING_STATE_CHANGED"; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * A2DP sink device is streaming music. This state can be one of |
| 90 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 91 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 92 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 93 | public static final int STATE_PLAYING = 10; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * A2DP sink device is NOT streaming music. This state can be one of |
| 97 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 98 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 99 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 100 | public static final int STATE_NOT_PLAYING = 11; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Intent used to broadcast the change in the Playing state of the A2DP Sink |
| 104 | * profile. |
| 105 | * |
| 106 | * <p>This intent will have 3 extras: |
| 107 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 108 | * <li> {@link #EXTRA_AUDIO_CONFIG} - The audio configuration for the remote device. </li> |
| 109 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 110 | * </ul> |
| 111 | * |
| 112 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 113 | * receive. |
| 114 | */ |
| 115 | public static final String ACTION_AUDIO_CONFIG_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 116 | "android.bluetooth.a2dp-sink.profile.action.AUDIO_CONFIG_CHANGED"; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Extra for the {@link #ACTION_AUDIO_CONFIG_CHANGED} intent. |
| 120 | * |
| 121 | * This extra represents the current audio configuration of the A2DP source device. |
| 122 | * {@see BluetoothAudioConfig} |
| 123 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 124 | public static final String EXTRA_AUDIO_CONFIG = |
| 125 | "android.bluetooth.a2dp-sink.profile.extra.AUDIO_CONFIG"; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 126 | |
| 127 | private Context mContext; |
| 128 | private ServiceListener mServiceListener; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 129 | private volatile IBluetoothA2dpSink mService; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 130 | private BluetoothAdapter mAdapter; |
| 131 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 132 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 133 | new IBluetoothStateChangeCallback.Stub() { |
| 134 | public void onBluetoothStateChange(boolean up) { |
| 135 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 136 | if (!up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 137 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 138 | synchronized (mConnection) { |
| 139 | try { |
| 140 | mService = null; |
| 141 | mContext.unbindService(mConnection); |
| 142 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 143 | Log.e(TAG, "", re); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } else { |
| 147 | synchronized (mConnection) { |
| 148 | try { |
| 149 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 150 | if (VDBG) Log.d(TAG, "Binding service..."); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 151 | doBind(); |
| 152 | } |
| 153 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 154 | Log.e(TAG, "", re); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 163 | * Bluetooth A2DP service. |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 164 | */ |
| 165 | /*package*/ BluetoothA2dpSink(Context context, ServiceListener l) { |
| 166 | mContext = context; |
| 167 | mServiceListener = l; |
| 168 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 169 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 170 | if (mgr != null) { |
| 171 | try { |
| 172 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 173 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 174 | Log.e(TAG, "", e); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | doBind(); |
| 179 | } |
| 180 | |
| 181 | boolean doBind() { |
| 182 | Intent intent = new Intent(IBluetoothA2dpSink.class.getName()); |
| 183 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 184 | intent.setComponent(comp); |
| 185 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 186 | mContext.getUser())) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 187 | Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent); |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /*package*/ void close() { |
| 194 | mServiceListener = null; |
| 195 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 196 | if (mgr != null) { |
| 197 | try { |
| 198 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 199 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 200 | Log.e(TAG, "", e); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | synchronized (mConnection) { |
| 205 | if (mService != null) { |
| 206 | try { |
| 207 | mService = null; |
| 208 | mContext.unbindService(mConnection); |
| 209 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 210 | Log.e(TAG, "", re); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 216 | @Override |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 217 | public void finalize() { |
| 218 | close(); |
| 219 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 220 | |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Initiate connection to a profile of the remote bluetooth device. |
| 223 | * |
| 224 | * <p> Currently, the system supports only 1 connection to the |
| 225 | * A2DP profile. The API will automatically disconnect connected |
| 226 | * devices before connecting. |
| 227 | * |
| 228 | * <p> This API returns false in scenarios like the profile on the |
| 229 | * device is already connected or Bluetooth is not turned on. |
| 230 | * When this API returns true, it is guaranteed that |
| 231 | * connection state intent for the profile will be broadcasted with |
| 232 | * the state. Users can get the connection state of the profile |
| 233 | * from this intent. |
| 234 | * |
| 235 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 236 | * permission. |
| 237 | * |
| 238 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 239 | * @return false on immediate error, true otherwise |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 240 | * @hide |
| 241 | */ |
| 242 | public boolean connect(BluetoothDevice device) { |
| 243 | if (DBG) log("connect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 244 | final IBluetoothA2dpSink service = mService; |
| 245 | if (service != null && isEnabled() && isValidDevice(device)) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 246 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 247 | return service.connect(device); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 248 | } catch (RemoteException e) { |
| 249 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 250 | return false; |
| 251 | } |
| 252 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 253 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Initiate disconnection from a profile |
| 259 | * |
| 260 | * <p> This API will return false in scenarios like the profile on the |
| 261 | * Bluetooth device is not in connected state etc. When this API returns, |
| 262 | * true, it is guaranteed that the connection state change |
| 263 | * intent will be broadcasted with the state. Users can get the |
| 264 | * disconnection state of the profile from this intent. |
| 265 | * |
| 266 | * <p> If the disconnection is initiated by a remote device, the state |
| 267 | * will transition from {@link #STATE_CONNECTED} to |
| 268 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 269 | * host (local) device the state will transition from |
| 270 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 271 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 272 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 273 | * two scenarios. |
| 274 | * |
| 275 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 276 | * permission. |
| 277 | * |
| 278 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 279 | * @return false on immediate error, true otherwise |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 280 | * @hide |
| 281 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 282 | @UnsupportedAppUsage |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 283 | public boolean disconnect(BluetoothDevice device) { |
| 284 | if (DBG) log("disconnect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 285 | final IBluetoothA2dpSink service = mService; |
| 286 | if (service != null && isEnabled() && isValidDevice(device)) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 287 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 288 | return service.disconnect(device); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 289 | } catch (RemoteException e) { |
| 290 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 291 | return false; |
| 292 | } |
| 293 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 294 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * {@inheritDoc} |
| 300 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 301 | @Override |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 302 | public List<BluetoothDevice> getConnectedDevices() { |
| 303 | if (VDBG) log("getConnectedDevices()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 304 | final IBluetoothA2dpSink service = mService; |
| 305 | if (service != null && isEnabled()) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 306 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 307 | return service.getConnectedDevices(); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 308 | } catch (RemoteException e) { |
| 309 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 310 | return new ArrayList<BluetoothDevice>(); |
| 311 | } |
| 312 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 313 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 314 | return new ArrayList<BluetoothDevice>(); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * {@inheritDoc} |
| 319 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 320 | @Override |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 321 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 322 | if (VDBG) log("getDevicesMatchingStates()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 323 | final IBluetoothA2dpSink service = mService; |
| 324 | if (service != null && isEnabled()) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 325 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 326 | return service.getDevicesMatchingConnectionStates(states); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 327 | } catch (RemoteException e) { |
| 328 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 329 | return new ArrayList<BluetoothDevice>(); |
| 330 | } |
| 331 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 332 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 333 | return new ArrayList<BluetoothDevice>(); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * {@inheritDoc} |
| 338 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 339 | @Override |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 340 | public int getConnectionState(BluetoothDevice device) { |
| 341 | if (VDBG) log("getState(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 342 | final IBluetoothA2dpSink service = mService; |
| 343 | if (service != null && isEnabled() && isValidDevice(device)) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 344 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 345 | return service.getConnectionState(device); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 346 | } catch (RemoteException e) { |
| 347 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 348 | return BluetoothProfile.STATE_DISCONNECTED; |
| 349 | } |
| 350 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 351 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 352 | return BluetoothProfile.STATE_DISCONNECTED; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Get the current audio configuration for the A2DP source device, |
| 357 | * or null if the device has no audio configuration |
| 358 | * |
| 359 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 360 | * |
| 361 | * @param device Remote bluetooth device. |
| 362 | * @return audio configuration for the device, or null |
| 363 | * |
| 364 | * {@see BluetoothAudioConfig} |
| 365 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 366 | public BluetoothAudioConfig getAudioConfig(BluetoothDevice device) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 367 | if (VDBG) log("getAudioConfig(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 368 | final IBluetoothA2dpSink service = mService; |
| 369 | if (service != null && isEnabled() && isValidDevice(device)) { |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 370 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 371 | return service.getAudioConfig(device); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 372 | } catch (RemoteException e) { |
| 373 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 374 | return null; |
| 375 | } |
| 376 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 377 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 378 | return null; |
| 379 | } |
| 380 | |
| 381 | /** |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 382 | * Set priority of the profile |
| 383 | * |
| 384 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 385 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 386 | * {@link #PRIORITY_OFF}, |
| 387 | * |
| 388 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 389 | * permission. |
| 390 | * |
| 391 | * @param device Paired bluetooth device |
| 392 | * @param priority |
| 393 | * @return true if priority is set, false on error |
| 394 | * @hide |
| 395 | */ |
| 396 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 397 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 398 | final IBluetoothA2dpSink service = mService; |
| 399 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 400 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 401 | && priority != BluetoothProfile.PRIORITY_ON) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 405 | return service.setPriority(device, priority); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 406 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 407 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 408 | return false; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 411 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 412 | return false; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Get the priority of the profile. |
| 417 | * |
| 418 | * <p> The priority can be any of: |
| 419 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 420 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 421 | * |
| 422 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 423 | * |
| 424 | * @param device Bluetooth device |
| 425 | * @return priority of the device |
| 426 | * @hide |
| 427 | */ |
| 428 | public int getPriority(BluetoothDevice device) { |
| 429 | if (VDBG) log("getPriority(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 430 | final IBluetoothA2dpSink service = mService; |
| 431 | if (service != null && isEnabled() && isValidDevice(device)) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 432 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 433 | return service.getPriority(device); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 434 | } catch (RemoteException e) { |
| 435 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 436 | return BluetoothProfile.PRIORITY_OFF; |
| 437 | } |
| 438 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 439 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 440 | return BluetoothProfile.PRIORITY_OFF; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Check if A2DP profile is streaming music. |
| 445 | * |
| 446 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 447 | * |
| 448 | * @param device BluetoothDevice device |
| 449 | */ |
| 450 | public boolean isA2dpPlaying(BluetoothDevice device) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 451 | final IBluetoothA2dpSink service = mService; |
| 452 | if (service != null && isEnabled() && isValidDevice(device)) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 453 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 454 | return service.isA2dpPlaying(device); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 455 | } catch (RemoteException e) { |
| 456 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 457 | return false; |
| 458 | } |
| 459 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 460 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
| 464 | /** |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 465 | * Helper for converting a state to a string. |
| 466 | * |
| 467 | * For debug use only - strings are not internationalized. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 468 | * |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 469 | * @hide |
| 470 | */ |
| 471 | public static String stateToString(int state) { |
| 472 | switch (state) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 473 | case STATE_DISCONNECTED: |
| 474 | return "disconnected"; |
| 475 | case STATE_CONNECTING: |
| 476 | return "connecting"; |
| 477 | case STATE_CONNECTED: |
| 478 | return "connected"; |
| 479 | case STATE_DISCONNECTING: |
| 480 | return "disconnecting"; |
| 481 | case STATE_PLAYING: |
| 482 | return "playing"; |
| 483 | case STATE_NOT_PLAYING: |
| 484 | return "not playing"; |
| 485 | default: |
| 486 | return "<unknown state " + state + ">"; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | private final ServiceConnection mConnection = new ServiceConnection() { |
| 491 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 492 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 493 | mService = IBluetoothA2dpSink.Stub.asInterface(Binder.allowBlocking(service)); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 494 | if (mServiceListener != null) { |
| 495 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, |
| 496 | BluetoothA2dpSink.this); |
| 497 | } |
| 498 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 499 | |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 500 | public void onServiceDisconnected(ComponentName className) { |
| 501 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 502 | mService = null; |
| 503 | if (mServiceListener != null) { |
| 504 | mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP_SINK); |
| 505 | } |
| 506 | } |
| 507 | }; |
| 508 | |
| 509 | private boolean isEnabled() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 510 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 513 | private static boolean isValidDevice(BluetoothDevice device) { |
| 514 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 518 | Log.d(TAG, msg); |
Mike Lockwood | 2263dd1 | 2014-05-14 09:51:30 -0700 | [diff] [blame] | 519 | } |
| 520 | } |