Filter out non-discoverable scan result in hearing device pairing page

Flag: EXEMPT bugfix
Bug: 381027984
Test: atest HearingDevicePairingFragmentTest
Test: check with real device
Change-Id: I7ab70d5508d71472cbd2d0959bb749599d039058
This commit is contained in:
Angela Wang
2025-02-24 09:22:06 +00:00
parent 7a9277ca8f
commit 5097cbe1ca
2 changed files with 24 additions and 13 deletions

View File

@@ -75,6 +75,8 @@ public class HearingDevicePairingFragment extends RestrictedDashboardFragment im
private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY = private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
"persist.bluetooth.showdeviceswithoutnames"; "persist.bluetooth.showdeviceswithoutnames";
private static final String KEY_AVAILABLE_HEARING_DEVICES = "available_hearing_devices"; private static final String KEY_AVAILABLE_HEARING_DEVICES = "available_hearing_devices";
// Flags data type from CSS 1.3 Flags
private static final int BT_DISCOVERABLE_MASK = 0x03;
LocalBluetoothManager mLocalManager; LocalBluetoothManager mLocalManager;
@Nullable @Nullable
@@ -322,7 +324,7 @@ public class HearingDevicePairingFragment extends RestrictedDashboardFragment im
}; };
void handleLeScanResult(ScanResult result) { void handleLeScanResult(ScanResult result) {
if (mCachedDeviceManager == null) { if (mCachedDeviceManager == null || !isDeviceDiscoverable(result)) {
return; return;
} }
final BluetoothDevice device = result.getDevice(); final BluetoothDevice device = result.getDevice();
@@ -505,4 +507,13 @@ public class HearingDevicePairingFragment extends RestrictedDashboardFragment im
Toast.makeText(getContext(), R.string.connected_device_bluetooth_turned_on_toast, Toast.makeText(getContext(), R.string.connected_device_bluetooth_turned_on_toast,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
boolean isDeviceDiscoverable(ScanResult result) {
final ScanRecord scanRecord = result.getScanRecord();
if (scanRecord == null) {
return false;
}
final int flags = scanRecord.getAdvertiseFlags();
return (flags & BT_DISCOVERABLE_MASK) != 0;
}
} }

View File

@@ -156,9 +156,7 @@ public class HearingDevicePairingFragmentTest {
@Test @Test
public void handleLeScanResult_markDeviceAsHearingAid() { public void handleLeScanResult_markDeviceAsHearingAid() {
ScanResult scanResult = mock(ScanResult.class); ScanResult scanResult = createMockScanResult();
doReturn(mDevice).when(scanResult).getDevice();
doReturn(mCachedDevice).when(mCachedDeviceManager).findDevice(mDevice);
mFragment.handleLeScanResult(scanResult); mFragment.handleLeScanResult(scanResult);
@@ -167,9 +165,7 @@ public class HearingDevicePairingFragmentTest {
@Test @Test
public void handleLeScanResult_isAndroidCompatible_addDevice() { public void handleLeScanResult_isAndroidCompatible_addDevice() {
ScanResult scanResult = mock(ScanResult.class); ScanResult scanResult = createMockScanResult();
doReturn(mDevice).when(scanResult).getDevice();
doReturn(mCachedDevice).when(mCachedDeviceManager).findDevice(mDevice);
doReturn(true).when(mFragment).isAndroidCompatibleHearingAid(scanResult); doReturn(true).when(mFragment).isAndroidCompatibleHearingAid(scanResult);
mFragment.handleLeScanResult(scanResult); mFragment.handleLeScanResult(scanResult);
@@ -179,9 +175,7 @@ public class HearingDevicePairingFragmentTest {
@Test @Test
public void handleLeScanResult_isNotAndroidCompatible_discoverServices() { public void handleLeScanResult_isNotAndroidCompatible_discoverServices() {
ScanResult scanResult = mock(ScanResult.class); ScanResult scanResult = createMockScanResult();
doReturn(mDevice).when(scanResult).getDevice();
doReturn(mCachedDevice).when(mCachedDeviceManager).findDevice(mDevice);
doReturn(false).when(mFragment).isAndroidCompatibleHearingAid(scanResult); doReturn(false).when(mFragment).isAndroidCompatibleHearingAid(scanResult);
mFragment.handleLeScanResult(scanResult); mFragment.handleLeScanResult(scanResult);
@@ -191,9 +185,7 @@ public class HearingDevicePairingFragmentTest {
@Test @Test
public void handleLeScanResult_alreadyBonded_doNothing() { public void handleLeScanResult_alreadyBonded_doNothing() {
ScanResult scanResult = mock(ScanResult.class); ScanResult scanResult = createMockScanResult();
doReturn(mDevice).when(scanResult).getDevice();
doReturn(mCachedDevice).when(mCachedDeviceManager).findDevice(mDevice);
doReturn(BluetoothDevice.BOND_BONDED).when(mCachedDevice).getBondState(); doReturn(BluetoothDevice.BOND_BONDED).when(mCachedDevice).getBondState();
mFragment.handleLeScanResult(scanResult); mFragment.handleLeScanResult(scanResult);
@@ -292,6 +284,14 @@ public class HearingDevicePairingFragmentTest {
assertThat(isCompatible).isFalse(); assertThat(isCompatible).isFalse();
} }
private ScanResult createMockScanResult() {
ScanResult scanResult = mock(ScanResult.class);
doReturn(mDevice).when(scanResult).getDevice();
doReturn(mCachedDevice).when(mCachedDeviceManager).findDevice(mDevice);
doReturn(true).when(mFragment).isDeviceDiscoverable(scanResult);
return scanResult;
}
private ScanResult createAshaScanResult() { private ScanResult createAshaScanResult() {
ScanResult scanResult = mock(ScanResult.class); ScanResult scanResult = mock(ScanResult.class);
ScanRecord scanRecord = mock(ScanRecord.class); ScanRecord scanRecord = mock(ScanRecord.class);