diff options
| author | Jack He <siyuanh@google.com> | 2017-06-28 08:32:54 +0000 |
|---|---|---|
| committer | android-build-merger <android-build-merger@google.com> | 2017-06-28 08:32:54 +0000 |
| commit | 0abc2e7cdb4bff1ef05e94ba0a131755b9644ecc (patch) | |
| tree | 3e69cd9feef0b3a8a56b94739399e25a8f618924 /framework/java/android/bluetooth/BluetoothDevice.java | |
| parent | e9a61426672ba836ed8244e0b626affbb28d1678 (diff) | |
| parent | 02b46c13ad25eb9fafa29a58cd7a0d782d12dbfc (diff) | |
Merge "Add APIs to get remote device's battery level (1/2)"
am: 02b46c13ad
Change-Id: Ic3253ef666632aaaffe8e1547dc563cd3bca9e37
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothDevice.java')
| -rw-r--r-- | framework/java/android/bluetooth/BluetoothDevice.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index 7ff37d2932..27b802e59c 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/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 + * <p>Always contains the extra fields {@link #EXTRA_DEVICE} and {@link + * #EXTRA_BATTERY_LEVEL}. + * <p>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 + * <p>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. * <p>This is an asynchronous call, it will return immediately. Register * for {@link #ACTION_BOND_STATE_CHANGED} intents to be notified when |
