Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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.Manifest; |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
| 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.ServiceConnection; |
| 29 | import android.os.Binder; |
| 30 | import android.os.IBinder; |
| 31 | import android.os.RemoteException; |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
| 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 40 | |
| 41 | /** |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 42 | * This class provides the public APIs to control the Hearing Aid profile. |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 43 | * |
| 44 | * <p>BluetoothHearingAid is a proxy object for controlling the Bluetooth Hearing Aid |
| 45 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 46 | * the BluetoothHearingAid proxy object. |
| 47 | * |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 48 | * <p> Android only supports one set of connected Bluetooth Hearing Aid device at a time. Each |
| 49 | * method is protected with its appropriate permission. |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 50 | */ |
| 51 | public final class BluetoothHearingAid implements BluetoothProfile { |
| 52 | private static final String TAG = "BluetoothHearingAid"; |
Stanley Tng | 420a0eb | 2018-11-15 10:22:07 -0800 | [diff] [blame] | 53 | private static final boolean DBG = true; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 54 | private static final boolean VDBG = false; |
| 55 | |
| 56 | /** |
| 57 | * Intent used to broadcast the change in connection state of the Hearing Aid |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 58 | * profile. Please note that in the binaural case, there will be two different LE devices for |
| 59 | * the left and right side and each device will have their own connection state changes.S |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 60 | * |
| 61 | * <p>This intent will have 3 extras: |
| 62 | * <ul> |
| 63 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 64 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 65 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 66 | * </ul> |
| 67 | * |
| 68 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 69 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 70 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 71 | * |
| 72 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 73 | * receive. |
| 74 | */ |
| 75 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 76 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 77 | "android.bluetooth.hearingaid.profile.action.CONNECTION_STATE_CHANGED"; |
| 78 | |
| 79 | /** |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 80 | * Intent used to broadcast the selection of a connected device as active. |
| 81 | * |
| 82 | * <p>This intent will have one extra: |
| 83 | * <ul> |
| 84 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can |
| 85 | * be null if no device is active. </li> |
| 86 | * </ul> |
| 87 | * |
| 88 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 89 | * receive. |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 90 | * |
| 91 | * @hide |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 92 | */ |
| 93 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 94 | @UnsupportedAppUsage |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 95 | public static final String ACTION_ACTIVE_DEVICE_CHANGED = |
| 96 | "android.bluetooth.hearingaid.profile.action.ACTIVE_DEVICE_CHANGED"; |
| 97 | |
| 98 | /** |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 99 | * This device represents Left Hearing Aid. |
| 100 | * |
| 101 | * @hide |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 102 | */ |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 103 | public static final int SIDE_LEFT = IBluetoothHearingAid.SIDE_LEFT; |
| 104 | |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 105 | /** |
| 106 | * This device represents Right Hearing Aid. |
| 107 | * |
| 108 | * @hide |
| 109 | */ |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 110 | public static final int SIDE_RIGHT = IBluetoothHearingAid.SIDE_RIGHT; |
| 111 | |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 112 | /** |
| 113 | * This device is Monaural. |
| 114 | * |
| 115 | * @hide |
| 116 | */ |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 117 | public static final int MODE_MONAURAL = IBluetoothHearingAid.MODE_MONAURAL; |
| 118 | |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 119 | /** |
| 120 | * This device is Binaural (should receive only left or right audio). |
| 121 | * |
| 122 | * @hide |
| 123 | */ |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 124 | public static final int MODE_BINAURAL = IBluetoothHearingAid.MODE_BINAURAL; |
| 125 | |
Stanley Tng | 1f5ea66 | 2018-11-15 17:11:36 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Indicates the HiSyncID could not be read and is unavailable. |
| 128 | * |
| 129 | * @hide |
| 130 | */ |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 131 | public static final long HI_SYNC_ID_INVALID = IBluetoothHearingAid.HI_SYNC_ID_INVALID; |
| 132 | |
| 133 | private Context mContext; |
| 134 | private ServiceListener mServiceListener; |
| 135 | private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); |
| 136 | @GuardedBy("mServiceLock") |
| 137 | private IBluetoothHearingAid mService; |
| 138 | private BluetoothAdapter mAdapter; |
| 139 | |
| 140 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 141 | new IBluetoothStateChangeCallback.Stub() { |
| 142 | public void onBluetoothStateChange(boolean up) { |
| 143 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 144 | if (!up) { |
| 145 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| 146 | try { |
| 147 | mServiceLock.writeLock().lock(); |
| 148 | mService = null; |
| 149 | mContext.unbindService(mConnection); |
| 150 | } catch (Exception re) { |
| 151 | Log.e(TAG, "", re); |
| 152 | } finally { |
| 153 | mServiceLock.writeLock().unlock(); |
| 154 | } |
| 155 | } else { |
| 156 | try { |
| 157 | mServiceLock.readLock().lock(); |
| 158 | if (mService == null) { |
| 159 | if (VDBG) Log.d(TAG, "Binding service..."); |
| 160 | doBind(); |
| 161 | } |
| 162 | } catch (Exception re) { |
| 163 | Log.e(TAG, "", re); |
| 164 | } finally { |
| 165 | mServiceLock.readLock().unlock(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * Create a BluetoothHearingAid proxy object for interacting with the local |
| 173 | * Bluetooth Hearing Aid service. |
| 174 | */ |
| 175 | /*package*/ BluetoothHearingAid(Context context, ServiceListener l) { |
| 176 | mContext = context; |
| 177 | mServiceListener = l; |
| 178 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 179 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 180 | if (mgr != null) { |
| 181 | try { |
| 182 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 183 | } catch (RemoteException e) { |
| 184 | Log.e(TAG, "", e); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | doBind(); |
| 189 | } |
| 190 | |
| 191 | void doBind() { |
| 192 | Intent intent = new Intent(IBluetoothHearingAid.class.getName()); |
| 193 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 194 | intent.setComponent(comp); |
| 195 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 196 | UserHandle.CURRENT_OR_SELF)) { |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 197 | Log.e(TAG, "Could not bind to Bluetooth Hearing Aid Service with " + intent); |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /*package*/ void close() { |
| 203 | mServiceListener = null; |
| 204 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 205 | if (mgr != null) { |
| 206 | try { |
| 207 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 208 | } catch (Exception e) { |
| 209 | Log.e(TAG, "", e); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | try { |
| 214 | mServiceLock.writeLock().lock(); |
| 215 | if (mService != null) { |
| 216 | mService = null; |
| 217 | mContext.unbindService(mConnection); |
| 218 | } |
| 219 | } catch (Exception re) { |
| 220 | Log.e(TAG, "", re); |
| 221 | } finally { |
| 222 | mServiceLock.writeLock().unlock(); |
| 223 | } |
| 224 | } |
| 225 | |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Initiate connection to a profile of the remote bluetooth device. |
| 228 | * |
| 229 | * <p> This API returns false in scenarios like the profile on the |
| 230 | * device is already connected or Bluetooth is not turned on. |
| 231 | * When this API returns true, it is guaranteed that |
| 232 | * connection state intent for the profile will be broadcasted with |
| 233 | * the state. Users can get the connection state of the profile |
| 234 | * from this intent. |
| 235 | * |
| 236 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 237 | * permission. |
| 238 | * |
| 239 | * @param device Remote Bluetooth Device |
| 240 | * @return false on immediate error, true otherwise |
| 241 | * @hide |
| 242 | */ |
| 243 | public boolean connect(BluetoothDevice device) { |
| 244 | if (DBG) log("connect(" + device + ")"); |
| 245 | try { |
| 246 | mServiceLock.readLock().lock(); |
| 247 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 248 | return mService.connect(device); |
| 249 | } |
| 250 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 251 | return false; |
| 252 | } catch (RemoteException e) { |
| 253 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 254 | return false; |
| 255 | } finally { |
| 256 | mServiceLock.readLock().unlock(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Initiate disconnection from a profile |
| 262 | * |
| 263 | * <p> This API will return false in scenarios like the profile on the |
| 264 | * Bluetooth device is not in connected state etc. When this API returns, |
| 265 | * true, it is guaranteed that the connection state change |
| 266 | * intent will be broadcasted with the state. Users can get the |
| 267 | * disconnection state of the profile from this intent. |
| 268 | * |
| 269 | * <p> If the disconnection is initiated by a remote device, the state |
| 270 | * will transition from {@link #STATE_CONNECTED} to |
| 271 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 272 | * host (local) device the state will transition from |
| 273 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 274 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 275 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 276 | * two scenarios. |
| 277 | * |
| 278 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 279 | * permission. |
| 280 | * |
| 281 | * @param device Remote Bluetooth Device |
| 282 | * @return false on immediate error, true otherwise |
| 283 | * @hide |
| 284 | */ |
| 285 | public boolean disconnect(BluetoothDevice device) { |
| 286 | if (DBG) log("disconnect(" + device + ")"); |
| 287 | try { |
| 288 | mServiceLock.readLock().lock(); |
| 289 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 290 | return mService.disconnect(device); |
| 291 | } |
| 292 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 293 | return false; |
| 294 | } catch (RemoteException e) { |
| 295 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 296 | return false; |
| 297 | } finally { |
| 298 | mServiceLock.readLock().unlock(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * {@inheritDoc} |
| 304 | */ |
| 305 | @Override |
| 306 | public List<BluetoothDevice> getConnectedDevices() { |
| 307 | if (VDBG) log("getConnectedDevices()"); |
| 308 | try { |
| 309 | mServiceLock.readLock().lock(); |
| 310 | if (mService != null && isEnabled()) { |
| 311 | return mService.getConnectedDevices(); |
| 312 | } |
| 313 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 314 | return new ArrayList<BluetoothDevice>(); |
| 315 | } catch (RemoteException e) { |
| 316 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 317 | return new ArrayList<BluetoothDevice>(); |
| 318 | } finally { |
| 319 | mServiceLock.readLock().unlock(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * {@inheritDoc} |
| 325 | */ |
| 326 | @Override |
| 327 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 328 | if (VDBG) log("getDevicesMatchingStates()"); |
| 329 | try { |
| 330 | mServiceLock.readLock().lock(); |
| 331 | if (mService != null && isEnabled()) { |
| 332 | return mService.getDevicesMatchingConnectionStates(states); |
| 333 | } |
| 334 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 335 | return new ArrayList<BluetoothDevice>(); |
| 336 | } catch (RemoteException e) { |
| 337 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 338 | return new ArrayList<BluetoothDevice>(); |
| 339 | } finally { |
| 340 | mServiceLock.readLock().unlock(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * {@inheritDoc} |
| 346 | */ |
| 347 | @Override |
| 348 | public int getConnectionState(BluetoothDevice device) { |
| 349 | if (VDBG) log("getState(" + device + ")"); |
| 350 | try { |
| 351 | mServiceLock.readLock().lock(); |
| 352 | if (mService != null && isEnabled() |
| 353 | && isValidDevice(device)) { |
| 354 | return mService.getConnectionState(device); |
| 355 | } |
| 356 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 357 | return BluetoothProfile.STATE_DISCONNECTED; |
| 358 | } catch (RemoteException e) { |
| 359 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 360 | return BluetoothProfile.STATE_DISCONNECTED; |
| 361 | } finally { |
| 362 | mServiceLock.readLock().unlock(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 367 | * Select a connected device as active. |
| 368 | * |
| 369 | * The active device selection is per profile. An active device's |
| 370 | * purpose is profile-specific. For example, Hearing Aid audio |
| 371 | * streaming is to the active Hearing Aid device. If a remote device |
| 372 | * is not connected, it cannot be selected as active. |
| 373 | * |
| 374 | * <p> This API returns false in scenarios like the profile on the |
| 375 | * device is not connected or Bluetooth is not turned on. |
| 376 | * When this API returns true, it is guaranteed that the |
| 377 | * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted |
| 378 | * with the active device. |
| 379 | * |
| 380 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 381 | * permission. |
| 382 | * |
| 383 | * @param device the remote Bluetooth device. Could be null to clear |
| 384 | * the active device and stop streaming audio to a Bluetooth device. |
| 385 | * @return false on immediate error, true otherwise |
| 386 | * @hide |
| 387 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 388 | @UnsupportedAppUsage |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 389 | public boolean setActiveDevice(@Nullable BluetoothDevice device) { |
| 390 | if (DBG) log("setActiveDevice(" + device + ")"); |
| 391 | try { |
| 392 | mServiceLock.readLock().lock(); |
| 393 | if (mService != null && isEnabled() |
| 394 | && ((device == null) || isValidDevice(device))) { |
| 395 | mService.setActiveDevice(device); |
| 396 | return true; |
| 397 | } |
| 398 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 399 | return false; |
| 400 | } catch (RemoteException e) { |
| 401 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 402 | return false; |
| 403 | } finally { |
| 404 | mServiceLock.readLock().unlock(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 409 | * Get the connected physical Hearing Aid devices that are active |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 410 | * |
| 411 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 412 | * permission. |
| 413 | * |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 414 | * @return the list of active devices. The first element is the left active |
| 415 | * device; the second element is the right active device. If either or both side |
| 416 | * is not active, it will be null on that position. Returns empty list on error. |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 417 | * @hide |
| 418 | */ |
| 419 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 420 | @UnsupportedAppUsage |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 421 | public List<BluetoothDevice> getActiveDevices() { |
| 422 | if (VDBG) log("getActiveDevices()"); |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 423 | try { |
| 424 | mServiceLock.readLock().lock(); |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 425 | if (mService != null && isEnabled()) { |
| 426 | return mService.getActiveDevices(); |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 427 | } |
| 428 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 429 | return new ArrayList<>(); |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 430 | } catch (RemoteException e) { |
| 431 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Hansong Zhang | 8d799f8 | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 432 | return new ArrayList<>(); |
Hansong Zhang | 7ca303c | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 433 | } finally { |
| 434 | mServiceLock.readLock().unlock(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 439 | * Set priority of the profile |
| 440 | * |
| 441 | * <p> The device should already be paired. |
| 442 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
| 443 | * {@link #PRIORITY_OFF}, |
| 444 | * |
| 445 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 446 | * permission. |
| 447 | * |
| 448 | * @param device Paired bluetooth device |
| 449 | * @param priority |
| 450 | * @return true if priority is set, false on error |
| 451 | * @hide |
| 452 | */ |
| 453 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 454 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| 455 | try { |
| 456 | mServiceLock.readLock().lock(); |
| 457 | if (mService != null && isEnabled() |
| 458 | && isValidDevice(device)) { |
| 459 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 460 | && priority != BluetoothProfile.PRIORITY_ON) { |
| 461 | return false; |
| 462 | } |
| 463 | return mService.setPriority(device, priority); |
| 464 | } |
| 465 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 466 | return false; |
| 467 | } catch (RemoteException e) { |
| 468 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 469 | return false; |
| 470 | } finally { |
| 471 | mServiceLock.readLock().unlock(); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Get the priority of the profile. |
| 477 | * |
| 478 | * <p> The priority can be any of: |
| 479 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 480 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 481 | * |
| 482 | * @param device Bluetooth device |
| 483 | * @return priority of the device |
| 484 | * @hide |
| 485 | */ |
| 486 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 487 | public int getPriority(BluetoothDevice device) { |
| 488 | if (VDBG) log("getPriority(" + device + ")"); |
| 489 | try { |
| 490 | mServiceLock.readLock().lock(); |
| 491 | if (mService != null && isEnabled() |
| 492 | && isValidDevice(device)) { |
| 493 | return mService.getPriority(device); |
| 494 | } |
| 495 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 496 | return BluetoothProfile.PRIORITY_OFF; |
| 497 | } catch (RemoteException e) { |
| 498 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 499 | return BluetoothProfile.PRIORITY_OFF; |
| 500 | } finally { |
| 501 | mServiceLock.readLock().unlock(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Helper for converting a state to a string. |
| 507 | * |
| 508 | * For debug use only - strings are not internationalized. |
| 509 | * |
| 510 | * @hide |
| 511 | */ |
| 512 | public static String stateToString(int state) { |
| 513 | switch (state) { |
| 514 | case STATE_DISCONNECTED: |
| 515 | return "disconnected"; |
| 516 | case STATE_CONNECTING: |
| 517 | return "connecting"; |
| 518 | case STATE_CONNECTED: |
| 519 | return "connected"; |
| 520 | case STATE_DISCONNECTING: |
| 521 | return "disconnecting"; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 522 | default: |
| 523 | return "<unknown state " + state + ">"; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Get the volume of the device. |
| 529 | * |
| 530 | * <p> The volume is between -128 dB (mute) to 0 dB. |
| 531 | * |
| 532 | * @return volume of the hearing aid device. |
| 533 | * @hide |
| 534 | */ |
| 535 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 536 | public int getVolume() { |
| 537 | if (VDBG) { |
| 538 | log("getVolume()"); |
| 539 | } |
| 540 | try { |
| 541 | mServiceLock.readLock().lock(); |
| 542 | if (mService != null && isEnabled()) { |
| 543 | return mService.getVolume(); |
| 544 | } |
| 545 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 546 | return 0; |
| 547 | } catch (RemoteException e) { |
| 548 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 549 | return 0; |
| 550 | } finally { |
| 551 | mServiceLock.readLock().unlock(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Tells remote device to adjust volume. Uses the following values: |
| 557 | * <ul> |
| 558 | * <li>{@link AudioManager#ADJUST_LOWER}</li> |
| 559 | * <li>{@link AudioManager#ADJUST_RAISE}</li> |
| 560 | * <li>{@link AudioManager#ADJUST_MUTE}</li> |
| 561 | * <li>{@link AudioManager#ADJUST_UNMUTE}</li> |
| 562 | * </ul> |
| 563 | * |
| 564 | * @param direction One of the supported adjust values. |
| 565 | * @hide |
| 566 | */ |
| 567 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 568 | public void adjustVolume(int direction) { |
| 569 | if (DBG) log("adjustVolume(" + direction + ")"); |
| 570 | |
| 571 | try { |
| 572 | mServiceLock.readLock().lock(); |
| 573 | |
| 574 | if (mService == null) { |
| 575 | Log.w(TAG, "Proxy not attached to service"); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | if (!isEnabled()) return; |
| 580 | |
| 581 | mService.adjustVolume(direction); |
| 582 | } catch (RemoteException e) { |
| 583 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 584 | } finally { |
| 585 | mServiceLock.readLock().unlock(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Tells remote device to set an absolute volume. |
| 591 | * |
| 592 | * @param volume Absolute volume to be set on remote |
| 593 | * @hide |
| 594 | */ |
| 595 | public void setVolume(int volume) { |
| 596 | if (DBG) Log.d(TAG, "setVolume(" + volume + ")"); |
| 597 | |
| 598 | try { |
| 599 | mServiceLock.readLock().lock(); |
| 600 | if (mService == null) { |
| 601 | Log.w(TAG, "Proxy not attached to service"); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | if (!isEnabled()) return; |
| 606 | |
| 607 | mService.setVolume(volume); |
| 608 | } catch (RemoteException e) { |
| 609 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 610 | } finally { |
| 611 | mServiceLock.readLock().unlock(); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Get the CustomerId of the device. |
| 617 | * |
| 618 | * @param device Bluetooth device |
| 619 | * @return the CustomerId of the device |
| 620 | * @hide |
| 621 | */ |
| 622 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 623 | public long getHiSyncId(BluetoothDevice device) { |
| 624 | if (VDBG) { |
| 625 | log("getCustomerId(" + device + ")"); |
| 626 | } |
| 627 | try { |
| 628 | mServiceLock.readLock().lock(); |
| 629 | if (mService == null) { |
| 630 | Log.w(TAG, "Proxy not attached to service"); |
| 631 | return HI_SYNC_ID_INVALID; |
| 632 | } |
| 633 | |
| 634 | if (!isEnabled() || !isValidDevice(device)) return HI_SYNC_ID_INVALID; |
| 635 | |
| 636 | return mService.getHiSyncId(device); |
| 637 | } catch (RemoteException e) { |
| 638 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 639 | return HI_SYNC_ID_INVALID; |
| 640 | } finally { |
| 641 | mServiceLock.readLock().unlock(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Get the side of the device. |
| 647 | * |
| 648 | * @param device Bluetooth device. |
| 649 | * @return SIDE_LEFT or SIDE_RIGHT |
| 650 | * @hide |
| 651 | */ |
| 652 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 653 | public int getDeviceSide(BluetoothDevice device) { |
| 654 | if (VDBG) { |
| 655 | log("getDeviceSide(" + device + ")"); |
| 656 | } |
| 657 | try { |
| 658 | mServiceLock.readLock().lock(); |
| 659 | if (mService != null && isEnabled() |
| 660 | && isValidDevice(device)) { |
| 661 | return mService.getDeviceSide(device); |
| 662 | } |
| 663 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 664 | return SIDE_LEFT; |
| 665 | } catch (RemoteException e) { |
| 666 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 667 | return SIDE_LEFT; |
| 668 | } finally { |
| 669 | mServiceLock.readLock().unlock(); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Get the mode of the device. |
| 675 | * |
| 676 | * @param device Bluetooth device |
| 677 | * @return MODE_MONAURAL or MODE_BINAURAL |
| 678 | * @hide |
| 679 | */ |
| 680 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 681 | public int getDeviceMode(BluetoothDevice device) { |
| 682 | if (VDBG) { |
| 683 | log("getDeviceMode(" + device + ")"); |
| 684 | } |
| 685 | try { |
| 686 | mServiceLock.readLock().lock(); |
| 687 | if (mService != null && isEnabled() |
| 688 | && isValidDevice(device)) { |
| 689 | return mService.getDeviceMode(device); |
| 690 | } |
| 691 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 692 | return MODE_MONAURAL; |
| 693 | } catch (RemoteException e) { |
| 694 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 695 | return MODE_MONAURAL; |
| 696 | } finally { |
| 697 | mServiceLock.readLock().unlock(); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | private final ServiceConnection mConnection = new ServiceConnection() { |
| 702 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 703 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| 704 | try { |
| 705 | mServiceLock.writeLock().lock(); |
| 706 | mService = IBluetoothHearingAid.Stub.asInterface(Binder.allowBlocking(service)); |
| 707 | } finally { |
| 708 | mServiceLock.writeLock().unlock(); |
| 709 | } |
| 710 | |
| 711 | if (mServiceListener != null) { |
| 712 | mServiceListener.onServiceConnected(BluetoothProfile.HEARING_AID, |
| 713 | BluetoothHearingAid.this); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | public void onServiceDisconnected(ComponentName className) { |
| 718 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 719 | try { |
| 720 | mServiceLock.writeLock().lock(); |
| 721 | mService = null; |
| 722 | } finally { |
| 723 | mServiceLock.writeLock().unlock(); |
| 724 | } |
| 725 | if (mServiceListener != null) { |
| 726 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEARING_AID); |
| 727 | } |
| 728 | } |
| 729 | }; |
| 730 | |
| 731 | private boolean isEnabled() { |
| 732 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | private boolean isValidDevice(BluetoothDevice device) { |
| 737 | if (device == null) return false; |
| 738 | |
| 739 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | private static void log(String msg) { |
| 744 | Log.d(TAG, msg); |
| 745 | } |
| 746 | } |