From 2b07bbafa261c27409cd7ca8c586521f54babd96 Mon Sep 17 00:00:00 2001 From: Jack He Date: Fri, 16 Jun 2017 19:43:58 -0700 Subject: [PATCH] Add APIs to get remote device's battery level (1/2) * Add BluetoothDevice.getBatteryLevel() API to retreive battery level information of remote device * Add BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED intent to notify user that remote device's battery level has changed Bug: 35874078 Test: make, pair with devices and use them Change-Id: I41051ee25383f5f3a1e505aef6f8c526385f58bd Merged-In: I41051ee25383f5f3a1e505aef6f8c526385f58bd (cherry picked from commit 1d312bfa78c25e0e1d6ea25b2c027e2efdd5a418) --- core/java/android/bluetooth/BluetoothDevice.java | 49 +++++++++++++++++++++++ core/java/android/bluetooth/BluetoothHeadset.java | 11 +++-- core/java/android/bluetooth/IBluetooth.aidl | 1 + core/res/AndroidManifest.xml | 1 + 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index 7ff37d29323e..27b802e59c58 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -203,6 +203,34 @@ public final class BluetoothDevice implements Parcelable { "android.bluetooth.device.action.BOND_STATE_CHANGED"; /** + * Broadcast Action: Indicates the battery level of a remote device has + * been retrieved for the first time, or changed since the last retrieval + *

Always contains the extra fields {@link #EXTRA_DEVICE} and {@link + * #EXTRA_BATTERY_LEVEL}. + *

Requires {@link android.Manifest.permission#BLUETOOTH} to receive. + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_BATTERY_LEVEL_CHANGED = + "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"; + + /** + * Used as an Integer extra field in {@link #ACTION_BATTERY_LEVEL_CHANGED} + * intent. It contains the most recently retrieved battery level information + * ranging from 0% to 100% for a remote device, {@link #BATTERY_LEVEL_UNKNOWN} + * when the valid is unknown or there is an error + * @hide + */ + public static final String EXTRA_BATTERY_LEVEL = + "android.bluetooth.device.extra.BATTERY_LEVEL"; + + /** + * Used as the unknown value for {@link #EXTRA_BATTERY_LEVEL} and {@link #getBatteryLevel()} + * @hide + */ + public static final int BATTERY_LEVEL_UNKNOWN = -1; + + /** * Used as a Parcelable {@link BluetoothDevice} extra field in every intent * broadcast by this class. It contains the {@link BluetoothDevice} that * the intent applies to. @@ -861,6 +889,27 @@ public final class BluetoothDevice implements Parcelable { } /** + * Get the most recent identified battery level of this Bluetooth device + *

Requires {@link android.Manifest.permission#BLUETOOTH} + * + * @return Battery level in percents from 0 to 100, or {@link #BATTERY_LEVEL_UNKNOWN} if + * Bluetooth is disabled, or device is disconnected, or does not have any battery + * reporting service, or return value is invalid + * @hide + */ + @RequiresPermission(Manifest.permission.BLUETOOTH) + public int getBatteryLevel() { + if (sService == null) { + Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); + return BATTERY_LEVEL_UNKNOWN; + } + try { + return sService.getBatteryLevel(this); + } catch (RemoteException e) {Log.e(TAG, "", e);} + return BATTERY_LEVEL_UNKNOWN; + } + + /** * Start the bonding (pairing) process with the remote device. *

This is an asynchronous call, it will return immediately. Register * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when diff --git a/core/java/android/bluetooth/BluetoothHeadset.java b/core/java/android/bluetooth/BluetoothHeadset.java index 57b954f40c6b..5b8d81d81efb 100644 --- a/core/java/android/bluetooth/BluetoothHeadset.java +++ b/core/java/android/bluetooth/BluetoothHeadset.java @@ -227,14 +227,13 @@ public final class BluetoothHeadset implements BluetoothProfile { * *

This intent will have 3 extras: *

- *

{@link #EXTRA_IND_ID} is defined by Bluetooth SIG and each of the indicators are + *

{@link #EXTRA_HF_INDICATORS_IND_ID} is defined by Bluetooth SIG and each of the indicators are * given an assigned number. Below shows the assigned number of Indicator added so far * - Enhanced Safety - 1 *

Requires {@link android.Manifest.permission#BLUETOOTH} permission to diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl index 43c5ae4407c9..1d7cfc900e4c 100644 --- a/core/java/android/bluetooth/IBluetooth.aidl +++ b/core/java/android/bluetooth/IBluetooth.aidl @@ -75,6 +75,7 @@ interface IBluetooth ParcelUuid[] getRemoteUuids(in BluetoothDevice device); boolean fetchRemoteUuids(in BluetoothDevice device); boolean sdpSearch(in BluetoothDevice device, in ParcelUuid uuid); + int getBatteryLevel(in BluetoothDevice device); boolean setPin(in BluetoothDevice device, boolean accept, int len, in byte[] pinCode); boolean setPasskey(in BluetoothDevice device, boolean accept, int len, in byte[] diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index b72f8c5567c3..be27048c1232 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -146,6 +146,7 @@ +