FastPairProvider Advertising initiation

Update FastPairProvider to disable all advertising if the scan mode
changes to SCAN_MODE_CONNECTABLE_DISCOVERABLE but the adapter is not in
a discovering state.

Bug: 196130405
Test: atest com.android.car.bluetooth
Change-Id: Iae8ca1d4bb02c6d5ecebf342f3f80a49fe7a25be
Merged-In: I873dccefbe337e268c3a97a96c9f58f421ccca13
diff --git a/service/src/com/android/car/FastPairProvider.java b/service/src/com/android/car/FastPairProvider.java
index dc63058..90529d4 100644
--- a/service/src/com/android/car/FastPairProvider.java
+++ b/service/src/com/android/car/FastPairProvider.java
@@ -18,6 +18,7 @@
 
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -48,6 +49,7 @@
     private final Context mContext;
     private boolean mStarted;
     private int mScanMode;
+    private BluetoothAdapter mBluetoothAdapter;
     private FastPairAdvertiser mFastPairModelAdvertiser;
     private FastPairAdvertiser mFastPairAccountAdvertiser;
     private FastPairGattServer mFastPairGattServer;
@@ -93,7 +95,11 @@
                         Slog.d(TAG, "NewScanMode = " + mScanMode);
                     }
                     if (mScanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
-                        advertiseModelId();
+                        if (mBluetoothAdapter.isDiscovering()) {
+                            advertiseModelId();
+                        } else {
+                            stopAdvertising();
+                        }
                     } else if (mScanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE
                             && mFastPairGattServer != null
                             && !mFastPairGattServer.isConnected()) {
@@ -130,6 +136,7 @@
         mModelId = res.getInteger(R.integer.fastPairModelId);
         mAntiSpoofKey = res.getString(R.string.fastPairAntiSpoofKey);
         mAutomaticAcceptance = res.getBoolean(R.bool.fastPairAutomaticAcceptance);
+        mBluetoothAdapter = mContext.getSystemService(BluetoothManager.class).getAdapter();
     }
 
     /**
@@ -161,6 +168,20 @@
         }
     }
 
+    void stopAdvertising() {
+        mFastPairAdvertiserHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                if (mFastPairAccountAdvertiser != null) {
+                    mFastPairAccountAdvertiser.stopAdvertising();
+                }
+                if (mFastPairModelAdvertiser != null) {
+                    mFastPairModelAdvertiser.stopAdvertising();
+                }
+            }
+        });
+    }
+
     void advertiseModelId() {
         mFastPairAdvertiserHandler.post(new Runnable() {
             @Override
@@ -195,7 +216,6 @@
                 mFastPairAccountAdvertiser.advertiseAccountKeys();
             }
         });
-
     }
 
     void startGatt() {
diff --git a/tests/carservice_unit_test/src/com/android/car/BluetoothFastPairTest.java b/tests/carservice_unit_test/src/com/android/car/BluetoothFastPairTest.java
index d7a8f3b..16bfa55 100644
--- a/tests/carservice_unit_test/src/com/android/car/BluetoothFastPairTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/BluetoothFastPairTest.java
@@ -24,6 +24,7 @@
 import static org.mockito.Mockito.anyInt;
 import static org.mockito.Mockito.anyString;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -120,6 +121,7 @@
     static final int TEST_PIN_NUMBER = 66051;
     static final int TEST_MODEL_ID = 4386;
     static final byte[] TEST_MODEL_ID_BYTES = {0x00, 0x11, 0x22};
+    static final int ASYNC_CALL_TIMEOUT_MILLIS = 200;
     byte[] mAdvertisementExpectedResults = new byte[]{0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
             0x11, 0x00};
     @Mock
@@ -440,6 +442,22 @@
                 .isEqualTo(TEST_MODEL_ID_BYTES);
     }
 
+    @Test
+    public void testStopAdvertisements() {
+        mTestFastPairProvider.start();
+        when(mMockBluetoothAdapter.isDiscovering()).thenReturn(true);
+        Intent scanMode = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
+        scanMode.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE,
+                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+        mTestFastPairProvider.mDiscoveryModeChanged.onReceive(mMockContext, scanMode);
+
+        when(mMockBluetoothAdapter.isDiscovering()).thenReturn(false);
+        scanMode.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE,
+                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
+        mTestFastPairProvider.mDiscoveryModeChanged.onReceive(mMockContext, scanMode);
+        verify(mMockLeAdvertiser, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).stopAdvertisingSet(any());
+    }
+
     void sendPairingKey(int pairingKey) {
         Intent pairingRequest = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
         pairingRequest.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pairingKey);