From f6b59c80da416b7b069cb16e8ede83064ebf7919 Mon Sep 17 00:00:00 2001 From: Stanley Tng Date: Mon, 17 Apr 2017 22:35:45 -0700 Subject: DO NOT MERGE ANYWHERE Allow the Bluetooth MAC address to be updated asynchronously (2/3) There are intermittent issues where either the returned Bluetooth MAC address to Java framework is uninitialized or this address update arrives too late. This fix will do 2 things: (1) Returns error when MAC address is unavailable in the native code. (2) Updates the MAC address later by adding a new broadcast event. Test: Check address for these cases: factory reset, system reboot, and Bluetooth re-enable. Bug: 36709382 Merged-In: I09720193e38fdf9139e1bb146f8e1847e2b65b1a (cherry picked from commit ad4d1d8e28618546953e75d4983335631feb6f2a) Change-Id: Ifae3adf6e2aad1f0811c03d3114d1bd0452e7c23 --- core/java/android/bluetooth/BluetoothAdapter.java | 24 ++++++++++++++++++++++ core/res/AndroidManifest.xml | 1 + .../android/server/BluetoothManagerService.java | 11 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 542b06b13605..e1a2a2f45a83 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -447,6 +447,30 @@ public final class BluetoothAdapter { public static final String ACTION_BLE_STATE_CHANGED = "android.bluetooth.adapter.action.BLE_STATE_CHANGED"; + /** + * Intent used to broadcast the change in the Bluetooth address + * of the local Bluetooth adapter. + *

Always contains the extra field {@link + * #EXTRA_BLUETOOTH_ADDRESS} containing the Bluetooth address. + * + * Note: only system level processes are allowed to send this + * defined broadcast. + * + * @hide + */ + public static final String ACTION_BLUETOOTH_ADDRESS_CHANGED = + "android.bluetooth.adapter.action.BLUETOOTH_ADDRESS_CHANGED"; + + /** + * Used as a String extra field in {@link + * #ACTION_BLUETOOTH_ADDRESS_CHANGED} intent to store the local + * Bluetooth address. + * + * @hide + */ + public static final String EXTRA_BLUETOOTH_ADDRESS = + "android.bluetooth.adapter.extra.BLUETOOTH_ADDRESS"; + /** * Broadcast Action: The notifys Bluetooth ACL connected event. This will be * by BLE Always on enabled application to know the ACL_CONNECTED event diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index c1c436da5938..3fc7ab76ef5f 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -121,6 +121,7 @@ + diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java index d767c462c6db..97437c9579d4 100644 --- a/services/core/java/com/android/server/BluetoothManagerService.java +++ b/services/core/java/com/android/server/BluetoothManagerService.java @@ -187,6 +187,14 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (newName != null) { storeNameAndAddress(newName, null); } + } else if (BluetoothAdapter.ACTION_BLUETOOTH_ADDRESS_CHANGED.equals(action)) { + String newAddress = intent.getStringExtra(BluetoothAdapter.EXTRA_BLUETOOTH_ADDRESS); + if (newAddress != null) { + if (DBG) Slog.d(TAG, "Bluetooth Adapter address changed to " + newAddress); + storeNameAndAddress(null, newAddress); + } else { + if (DBG) Slog.e(TAG, "No Bluetooth Adapter address parameter found"); + } } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { synchronized(mReceiver) { if (isBluetoothPersistedStateOn()) { @@ -275,6 +283,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub { registerForAirplaneMode(filter); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); mContext.registerReceiver(mReceiver, filter); + filter = new IntentFilter(BluetoothAdapter.ACTION_BLUETOOTH_ADDRESS_CHANGED); + filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); + mContext.registerReceiver(mReceiver, filter); loadStoredNameAndAddress(); if (isBluetoothPersistedStateOn()) { if (DBG) Slog.d(TAG, "Startup: Bluetooth persisted state is ON."); -- cgit v1.2.3-59-g8ed1b