diff --git a/services/core/java/com/android/server/am/BatteryExternalStatsWorker.java b/services/core/java/com/android/server/am/BatteryExternalStatsWorker.java index b3b74a109083..c5ee25910671 100644 --- a/services/core/java/com/android/server/am/BatteryExternalStatsWorker.java +++ b/services/core/java/com/android/server/am/BatteryExternalStatsWorker.java @@ -556,8 +556,31 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync { // We were asked to fetch Bluetooth data. final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter != null) { - bluetoothReceiver = new SynchronousResultReceiver("bluetooth"); - adapter.requestControllerActivityEnergyInfo(bluetoothReceiver); + SynchronousResultReceiver resultReceiver = + new SynchronousResultReceiver("bluetooth"); + adapter.requestControllerActivityEnergyInfo( + Runnable::run, + new BluetoothAdapter.OnBluetoothActivityEnergyInfoCallback() { + @Override + public void onBluetoothActivityEnergyInfoAvailable( + BluetoothActivityEnergyInfo info) { + Bundle bundle = new Bundle(); + bundle.putParcelable( + BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, info); + resultReceiver.send(0, bundle); + } + + @Override + public void onBluetoothActivityEnergyInfoError(int errorCode) { + Slog.w(TAG, "error reading Bluetooth stats: " + errorCode); + Bundle bundle = new Bundle(); + bundle.putParcelable( + BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, null); + resultReceiver.send(0, bundle); + } + } + ); + bluetoothReceiver = resultReceiver; } } diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index 99c675d4b22f..48d464c52df5 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -1618,7 +1618,28 @@ public class StatsPullAtomService extends SystemService { if (adapter != null) { SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver("bluetooth"); - adapter.requestControllerActivityEnergyInfo(bluetoothReceiver); + adapter.requestControllerActivityEnergyInfo( + Runnable::run, + new BluetoothAdapter.OnBluetoothActivityEnergyInfoCallback() { + @Override + public void onBluetoothActivityEnergyInfoAvailable( + BluetoothActivityEnergyInfo info) { + Bundle bundle = new Bundle(); + bundle.putParcelable( + BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, info); + bluetoothReceiver.send(0, bundle); + } + + @Override + public void onBluetoothActivityEnergyInfoError(int errorCode) { + Slog.w(TAG, "error reading Bluetooth stats: " + errorCode); + Bundle bundle = new Bundle(); + bundle.putParcelable( + BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, null); + bluetoothReceiver.send(0, bundle); + } + } + ); return awaitControllerInfo(bluetoothReceiver); } else { Slog.e(TAG, "Failed to get bluetooth adapter!");