Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 539b11e3 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Deliver correct address type to LeScanner

Bug: 339289658
Bug: 342127181
Flag: com.android.bluetooth.flags.le_scan_use_address_type
Test: atest TransitionalScanHelperTest
Change-Id: I6318b37a0cf285aafa25b65f56a8b9d71084de9c
parent 64fd9c43
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.bluetooth.le_scan;

import android.annotation.Nullable;
import android.bluetooth.BluetoothDevice;

public class AdvtFilterOnFoundOnLostInfo {
    private int mClientIf;
@@ -100,6 +101,7 @@ public class AdvtFilterOnFoundOnLostInfo {
        return mAddress;
    }

    @BluetoothDevice.AddressType
    public int getAddressType() {
        return mAddrType;
    }
+14 −3
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ public class TransitionalScanHelper {
    private static final int NUM_SCAN_EVENTS_KEPT = 20;

    // onFoundLost related constants
    private static final int ADVT_STATE_ONFOUND = 0;
    @VisibleForTesting static final int ADVT_STATE_ONFOUND = 0;
    private static final int ADVT_STATE_ONLOST = 1;

    private static final int ET_LEGACY_MASK = 0x10;
@@ -956,17 +956,28 @@ public class TransitionalScanHelper {
                        + trackingInfo.getClientIf()
                        + " address = "
                        + trackingInfo.getAddress()
                        + " addressType = "
                        + trackingInfo.getAddressType()
                        + " adv_state = "
                        + trackingInfo.getAdvState());

        @SuppressWarnings("NonCanonicalType")
        ScannerMap.App app = mScannerMap.getById(trackingInfo.getClientIf());
        if (app == null || (app.callback == null && app.info == null)) {
            Log.e(TAG, "app or callback is null");
            return;
        }

        BluetoothDevice device =
        BluetoothDevice device;
        if (Flags.leScanUseAddressType()) {
            device =
                    BluetoothAdapter.getDefaultAdapter()
                            .getRemoteLeDevice(
                                    trackingInfo.getAddress(), trackingInfo.getAddressType());
        } else {
            device =
                    BluetoothAdapter.getDefaultAdapter().getRemoteDevice(trackingInfo.getAddress());
        }
        int advertiserState = trackingInfo.getAdvState();
        ScanResult result =
                new ScanResult(
+63 −1
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import android.os.Binder;
import android.os.RemoteException;
import android.os.WorkSource;
import android.os.test.TestLooper;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.InstrumentationRegistry;
@@ -55,6 +56,7 @@ import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.CompanionManager;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.gatt.ContextMap;
import com.android.bluetooth.gatt.GattNativeInterface;
import com.android.bluetooth.gatt.GattObjectsFactory;

@@ -63,6 +65,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -82,7 +85,6 @@ public class TransitionalScanHelperTest {
    private TransitionalScanHelper mScanHelper;
    @Mock private TransitionalScanHelper.ScannerMap mScannerMap;

    @SuppressWarnings("NonCanonicalType")
    @Mock
    private TransitionalScanHelper.ScannerMap.App mApp;

@@ -396,4 +398,64 @@ public class TransitionalScanHelperTest {
                        BluetoothProfile.STATE_CONNECTING,
                        BluetoothProfile.STATE_CONNECTED);
    }

    @Test
    @EnableFlags(Flags.FLAG_LE_SCAN_USE_ADDRESS_TYPE)
    public void onTrackAdvFoundLost() throws Exception {
        int scannerId = 1;
        int advPktLen = 1;
        byte[] advPkt = new byte[] {0x02};
        int scanRspLen = 3;
        byte[] scanRsp = new byte[] {0x04};
        int filtIndex = 5;

        int advState = TransitionalScanHelper.ADVT_STATE_ONFOUND;
        int advInfoPresent = 7;
        String address = "00:11:22:33:FF:EE";
        int addrType = BluetoothDevice.ADDRESS_TYPE_RANDOM;
        int txPower = 9;
        int rssiValue = 10;
        int timeStamp = 11;

        ScanClient scanClient = new ScanClient(scannerId);
        scanClient.hasNetworkSettingsPermission = true;
        scanClient.settings =
                new ScanSettings.Builder()
                        .setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH)
                        .setLegacy(false)
                        .build();
        Set<ScanClient> scanClientSet = Collections.singleton(scanClient);

        ContextMap.App app = mock(ContextMap.App.class);
        IScannerCallback callback = mock(IScannerCallback.class);

        app.callback = callback;
        app.info = mock(TransitionalScanHelper.PendingIntentInfo.class);

        doReturn(app).when(mScannerMap).getById(scannerId);
        doReturn(scanClientSet).when(mScanManager).getRegularScanQueue();

        AdvtFilterOnFoundOnLostInfo advtFilterOnFoundOnLostInfo =
                new AdvtFilterOnFoundOnLostInfo(
                        scannerId,
                        advPktLen,
                        advPkt,
                        scanRspLen,
                        scanRsp,
                        filtIndex,
                        advState,
                        advInfoPresent,
                        address,
                        addrType,
                        txPower,
                        rssiValue,
                        timeStamp);

        mScanHelper.onTrackAdvFoundLost(advtFilterOnFoundOnLostInfo);
        ArgumentCaptor<ScanResult> result = ArgumentCaptor.forClass(ScanResult.class);
        verify(callback).onFoundOrLost(eq(true), result.capture());
        assertThat(result.getValue().getDevice()).isNotNull();
        assertThat(result.getValue().getDevice().getAddress()).isEqualTo(address);
        assertThat(result.getValue().getDevice().getAddressType()).isEqualTo(addrType);
    }
}