diff options
| author | 2018-01-03 12:13:26 -0800 | |
|---|---|---|
| committer | 2018-01-05 14:51:40 -0800 | |
| commit | 05f4bc40743654ea501b18d70ee162a08b81b35c (patch) | |
| tree | b5021558b1c6c1ae2564222c48869fb0a286cc13 | |
| parent | 4029fa6039a3305b093d8db0a24fb8e4dedd351a (diff) | |
HFP: Add APIs for set and get active device (1/3)
1. Call BluetoothHeadset.setActiveDevice(BluetoothDevice device) to set
a connected HFP/HSP device as active.
2. Listen for BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED intent
that will contain the latest active device (in EXTRA_DEVICE field).
If the active device could not be changed, the EXTRA_DEVICE
field could be null.
3. If setActiveDevice() is NOT in-progress, BluetoothA2dp.getActiveDevice()
can be used. If setActiveDevice() is in-progress, the result is undefined.
4. BluetoothHeadset.setActiveDevice() could be called by some other parts of
the system, so interested parties should always listen for
BluetoothHeadset.ACTION_ACTIVE_DEVICE_CHANGED intents and prepared
for active device updates.
Bug: 68951996
Test: manual
Change-Id: I22ca639a04fed7bf17df59c405ddeda90dafb8ff
| -rw-r--r-- | core/java/android/bluetooth/BluetoothHeadset.java | 88 | ||||
| -rw-r--r-- | core/res/AndroidManifest.xml | 2 |
2 files changed, 90 insertions, 0 deletions
diff --git a/core/java/android/bluetooth/BluetoothHeadset.java b/core/java/android/bluetooth/BluetoothHeadset.java index 838d3153d54c..55a6b4c6b4d4 100644 --- a/core/java/android/bluetooth/BluetoothHeadset.java +++ b/core/java/android/bluetooth/BluetoothHeadset.java @@ -16,6 +16,7 @@ package android.bluetooth; +import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; @@ -93,6 +94,23 @@ public final class BluetoothHeadset implements BluetoothProfile { public static final String ACTION_AUDIO_STATE_CHANGED = "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"; + /** + * Intent used to broadcast the selection of a connected device as active. + * + * <p>This intent will have one extra: + * <ul> + * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can + * be null if no device is active. </li> + * </ul> + * + * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to + * receive. + * + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_ACTIVE_DEVICE_CHANGED = + "android.bluetooth.headset.profile.action.ACTIVE_DEVICE_CHANGED"; /** * Intent used to broadcast that the headset has posted a @@ -983,6 +1001,76 @@ public final class BluetoothHeadset implements BluetoothProfile { } /** + * Select a connected device as active. + * + * The active device selection is per profile. An active device's + * purpose is profile-specific. For example, in HFP and HSP profiles, + * it is the device used for phone call audio. If a remote device is not + * connected, it cannot be selected as active. + * + * <p> This API returns false in scenarios like the profile on the + * device is not connected or Bluetooth is not turned on. + * When this API returns true, it is guaranteed that the + * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted + * with the active device. + * + * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} + * permission. + * + * @param device Remote Bluetooth Device, could be null if phone call audio should not be + * streamed to a headset + * @return false on immediate error, true otherwise + * @hide + */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) + public boolean setActiveDevice(@Nullable BluetoothDevice device) { + if (DBG) { + Log.d(TAG, "setActiveDevice: " + device); + } + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && (device == null || isValidDevice(device))) { + try { + return service.setActiveDevice(device); + } catch (RemoteException e) { + Log.e(TAG, Log.getStackTraceString(new Throwable())); + } + } + if (service == null) { + Log.w(TAG, "Proxy not attached to service"); + } + return false; + } + + /** + * Get the connected device that is active. + * + * <p>Requires {@link android.Manifest.permission#BLUETOOTH} + * permission. + * + * @return the connected device that is active or null if no device + * is active. + * @hide + */ + @RequiresPermission(android.Manifest.permission.BLUETOOTH) + public BluetoothDevice getActiveDevice() { + if (VDBG) { + Log.d(TAG, "getActiveDevice"); + } + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { + try { + return service.getActiveDevice(); + } catch (RemoteException e) { + Log.e(TAG, Log.getStackTraceString(new Throwable())); + } + } + if (service == null) { + Log.w(TAG, "Proxy not attached to service"); + } + return null; + } + + /** * check if in-band ringing is supported for this platform. * * @return true if in-band ringing is supported false if in-band ringing is not supported diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index e33c822dfc7d..4b2ae06eff2f 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -159,6 +159,8 @@ <protected-broadcast android:name="android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED" /> <protected-broadcast + android:name="android.bluetooth.headset.profile.action.ACTIVE_DEVICE_CHANGED" /> + <protected-broadcast android:name="android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED" /> <protected-broadcast android:name="android.bluetooth.headsetclient.profile.action.AUDIO_STATE_CHANGED" /> |