From fc10fdf30bfd7740d97dce6b73a9f4746161257a Mon Sep 17 00:00:00 2001 From: Haijie Hong Date: Tue, 18 Mar 2025 18:56:24 +0800 Subject: [PATCH] Check bond state when bluetooth key is missing Bug: 403847818 Test: local tested Flag: EXEMPT minor fix Change-Id: I3f310b5758ebecd7591d76988b351d3c6a7dfc67 --- .../settings/bluetooth/BluetoothKeyMissingReceiver.java | 9 +++++++++ .../bluetooth/BluetoothKeyMissingReceiverTest.java | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java b/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java index e7e0b4a100e..cfe9c056d39 100644 --- a/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java +++ b/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiver.java @@ -55,9 +55,18 @@ public final class BluetoothKeyMissingReceiver extends BroadcastReceiver { } BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + if (device == null) { + return; + } PowerManager powerManager = context.getSystemService(PowerManager.class); if (TextUtils.equals(action, BluetoothDevice.ACTION_KEY_MISSING)) { Log.d(TAG, "Receive ACTION_KEY_MISSING"); + if (device.getBondState() == BluetoothDevice.BOND_NONE) { + Log.d( + TAG, + "Device " + device.getAnonymizedAddress() + " is already unbonded, skip."); + return; + } Integer keyMissingCount = BluetoothUtils.getKeyMissingCount(device); if (keyMissingCount != null && keyMissingCount != 1) { Log.d(TAG, "Key missing count is " + keyMissingCount + ", skip."); diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java index a183d8d7e68..42d7105c6f3 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothKeyMissingReceiverTest.java @@ -127,6 +127,7 @@ public class BluetoothKeyMissingReceiverTest { public void broadcastReceiver_background_showNotification() { Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING)); when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice); + when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent); bluetoothKeyMissingReceiver.onReceive(mContext, intent); @@ -141,6 +142,7 @@ public class BluetoothKeyMissingReceiverTest { when(mLocalBtManager.isForegroundActivity()).thenReturn(true); Intent intent = spy(new Intent(BluetoothDevice.ACTION_KEY_MISSING)); when(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(mBluetoothDevice); + when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); BluetoothKeyMissingReceiver bluetoothKeyMissingReceiver = getReceiver(intent); bluetoothKeyMissingReceiver.onReceive(mContext, intent);