Fix Bluetooth isConnected tracking when Wifi is disabled

We need to track whether Bluetooth is connected
in all states, not just when Wifi is enabled.
Thus, move the logic out of ClientModeImpl and
into WifiGlobals.

Bug: 160752427
Test: atest FrameworksWifiTests
Change-Id: I3e080027344446019730182dba4835c66a01714a
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java
index 0e55449..bb9283c 100644
--- a/service/java/com/android/server/wifi/ClientModeImpl.java
+++ b/service/java/com/android/server/wifi/ClientModeImpl.java
@@ -32,7 +32,6 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityManager;
-import android.bluetooth.BluetoothAdapter;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -288,29 +287,9 @@
     */
     private int mOperationalMode = DISABLED_MODE;
 
-    private ClientModeManager.Listener mClientModeCallback = null;
-
-    private boolean mBluetoothConnectionActive = false;
-
     private PowerManager.WakeLock mSuspendWakeLock;
 
     /**
-     * Interval in milliseconds between receiving a disconnect event
-     * while connected to a good AP, and handling the disconnect proper
-     */
-    private static final int LINK_FLAPPING_DEBOUNCE_MSEC = 4000;
-
-    /**
-     * Delay between supplicant restarts upon failure to establish connection
-     */
-    private static final int SUPPLICANT_RESTART_INTERVAL_MSECS = 5000;
-
-    /**
-     * Number of times we attempt to restart supplicant
-     */
-    private static final int SUPPLICANT_RESTART_TRIES = 5;
-
-    /**
      * Value to set in wpa_supplicant "bssid" field when we don't want to restrict connection to
      * a specific AP.
      */
@@ -322,9 +301,6 @@
      */
     private LinkProperties mLinkProperties;
 
-    /* Tracks sequence number on a periodic scan message */
-    private int mPeriodicScanToken = 0;
-
     private final Object mDhcpResultsParcelableLock = new Object();
     @NonNull
     private DhcpResultsParcelable mDhcpResultsParcelable = new DhcpResultsParcelable();
@@ -415,10 +391,9 @@
 
     /* The base for wifi message types */
     static final int BASE = Protocol.BASE_WIFI;
-    /* BT state change, e.g., on or off */
-    static final int CMD_BLUETOOTH_ADAPTER_STATE_CHANGE                 = BASE + 31;
-    /* BT connection state change, e.g., connected or disconnected */
-    static final int CMD_BLUETOOTH_ADAPTER_CONNECTION_STATE_CHANGE      = BASE + 32;
+
+    /* BT connection state changed, e.g., connected/disconnected */
+    static final int CMD_BLUETOOTH_CONNECTION_STATE_CHANGE              = BASE + 31;
 
     /* Supplicant commands after driver start*/
     /* Set operational mode. CONNECT, SCAN ONLY, SCAN_ONLY with Wi-Fi off mode */
@@ -1645,20 +1620,9 @@
         sendMessage(CMD_ENABLE_TDLS, enabler, 0, remoteMacAddress);
     }
 
-    /**
-     * Send a message indicating bluetooth adapter state changed, e.g., turn on or ff
-     */
-    public void sendBluetoothAdapterStateChange(int state) {
-        sendMessage(CMD_BLUETOOTH_ADAPTER_STATE_CHANGE, state, 0);
-    }
-
-    /**
-     * Send a message indicating bluetooth adapter connection state changed, e.g., connected
-     * or disconnected. Note that turning off BT after pairing success keeps connection state in
-     * connected state.
-     */
-    public void sendBluetoothAdapterConnectionStateChange(int state) {
-        sendMessage(CMD_BLUETOOTH_ADAPTER_CONNECTION_STATE_CHANGE, state, 0);
+    /** Send a message indicating bluetooth connection state changed, e.g. connected/disconnected */
+    public void onBluetoothConnectionStateChanged() {
+        sendMessage(CMD_BLUETOOTH_CONNECTION_STATE_CHANGE);
     }
 
     /**
@@ -2560,7 +2524,7 @@
     }
 
     void handlePreDhcpSetup() {
-        if (!mBluetoothConnectionActive) {
+        if (!mWifiGlobals.isBluetoothConnected()) {
             /*
              * There are problems setting the Wi-Fi driver's power
              * mode to active when bluetooth coexistence mode is
@@ -3132,23 +3096,6 @@
             boolean handleStatus = HANDLED;
 
             switch (message.what) {
-                case CMD_BLUETOOTH_ADAPTER_STATE_CHANGE: {
-                    // If BT was connected and then turned off, there is no CONNECTION_STATE_CHANGE
-                    // message. So we need to rely on STATE_CHANGE message to detect on->off
-                    // transition and update mBluetoothConnectionActive status correctly.
-                    mBluetoothConnectionActive = mBluetoothConnectionActive
-                            && message.arg1 != BluetoothAdapter.STATE_OFF;
-                    mWifiConnectivityManager.setBluetoothConnected(mBluetoothConnectionActive);
-                    break;
-                }
-                case CMD_BLUETOOTH_ADAPTER_CONNECTION_STATE_CHANGE: {
-                    // Transition to a non-disconnected state does correctly
-                    // indicate BT is connected or being connected.
-                    mBluetoothConnectionActive =
-                            message.arg1 != BluetoothAdapter.STATE_DISCONNECTED;
-                    mWifiConnectivityManager.setBluetoothConnected(mBluetoothConnectionActive);
-                    break;
-                }
                 case CMD_ENABLE_RSSI_POLL: {
                     mEnableRssiPolling = (message.arg1 == 1);
                     break;
@@ -3161,6 +3108,7 @@
                     }
                     break;
                 }
+                case CMD_BLUETOOTH_CONNECTION_STATE_CHANGE:
                 case WifiMonitor.NETWORK_CONNECTION_EVENT:
                 case WifiMonitor.NETWORK_DISCONNECTION_EVENT:
                 case WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT:
@@ -3333,12 +3281,11 @@
         mMboOceController.enable();
         mWifiDataStall.enablePhoneStateListener();
 
-        /**
-         * Enable bluetooth coexistence scan mode when bluetooth connection is active.
-         * When this mode is on, some of the low-level scan parameters used by the
-         * driver are changed to reduce interference with bluetooth
-         */
-        mWifiNative.setBluetoothCoexistenceScanMode(mInterfaceName, mBluetoothConnectionActive);
+        // Enable bluetooth coexistence scan mode when bluetooth connection is active.
+        // When this mode is on, some of the low-level scan parameters used by the
+        // driver are changed to reduce interference with bluetooth
+        mWifiNative.setBluetoothCoexistenceScanMode(
+                mInterfaceName, mWifiGlobals.isBluetoothConnected());
         sendNetworkChangeBroadcast(DetailedState.DISCONNECTED);
 
         // Disable legacy multicast filtering, which on some chipsets defaults to enabled.
@@ -3719,25 +3666,9 @@
                     }
                     break;
                 }
-                case CMD_BLUETOOTH_ADAPTER_STATE_CHANGE: {
-                    // If BT was connected and then turned off, there is no CONNECTION_STATE_CHANGE
-                    // message. So we need to rely on STATE_CHANGE message to detect on->off
-                    // transition and update mBluetoothConnectionActive status correctly.
-                    mBluetoothConnectionActive = mBluetoothConnectionActive
-                            && message.arg1 != BluetoothAdapter.STATE_OFF;
+                case CMD_BLUETOOTH_CONNECTION_STATE_CHANGE: {
                     mWifiNative.setBluetoothCoexistenceScanMode(
-                            mInterfaceName, mBluetoothConnectionActive);
-                    mWifiConnectivityManager.setBluetoothConnected(mBluetoothConnectionActive);
-                    break;
-                }
-                case CMD_BLUETOOTH_ADAPTER_CONNECTION_STATE_CHANGE: {
-                    // Transition to a non-disconnected state does correctly
-                    // indicate BT is connected or being connected.
-                    mBluetoothConnectionActive =
-                            message.arg1 != BluetoothAdapter.STATE_DISCONNECTED;
-                    mWifiNative.setBluetoothCoexistenceScanMode(
-                            mInterfaceName, mBluetoothConnectionActive);
-                    mWifiConnectivityManager.setBluetoothConnected(mBluetoothConnectionActive);
+                            mInterfaceName, mWifiGlobals.isBluetoothConnected());
                     break;
                 }
                 case CMD_SET_SUSPEND_OPT_ENABLED: {
diff --git a/service/java/com/android/server/wifi/ClientModeManager.java b/service/java/com/android/server/wifi/ClientModeManager.java
index e7b1d70..b13d79e 100644
--- a/service/java/com/android/server/wifi/ClientModeManager.java
+++ b/service/java/com/android/server/wifi/ClientModeManager.java
@@ -59,9 +59,11 @@
 
     void resetSimAuthNetworks(@ClientModeImpl.ResetSimReason int resetReason);
 
-    void sendBluetoothAdapterConnectionStateChange(int state);
-
-    void sendBluetoothAdapterStateChange(int state);
+    /**
+     * Notification that the Bluetooth connection state changed. The latest connection state can be
+     * fetched from {@link WifiGlobals#isBluetoothConnected()}.
+     */
+    void onBluetoothConnectionStateChanged();
 
     int syncGetWifiState();
 
diff --git a/service/java/com/android/server/wifi/ConcreteClientModeManager.java b/service/java/com/android/server/wifi/ConcreteClientModeManager.java
index f70be35..9a2f485 100644
--- a/service/java/com/android/server/wifi/ConcreteClientModeManager.java
+++ b/service/java/com/android/server/wifi/ConcreteClientModeManager.java
@@ -769,13 +769,8 @@
     }
 
     @Override
-    public void sendBluetoothAdapterConnectionStateChange(int state) {
-        mClientModeImpl.sendBluetoothAdapterConnectionStateChange(state);
-    }
-
-    @Override
-    public void sendBluetoothAdapterStateChange(int state) {
-        mClientModeImpl.sendBluetoothAdapterStateChange(state);
+    public void onBluetoothConnectionStateChanged() {
+        mClientModeImpl.onBluetoothConnectionStateChanged();
     }
 
     @Override
diff --git a/service/java/com/android/server/wifi/DefaultClientModeManager.java b/service/java/com/android/server/wifi/DefaultClientModeManager.java
index 6932f99..87d1736 100644
--- a/service/java/com/android/server/wifi/DefaultClientModeManager.java
+++ b/service/java/com/android/server/wifi/DefaultClientModeManager.java
@@ -104,10 +104,7 @@
     public void resetSimAuthNetworks(@ClientModeImpl.ResetSimReason int resetReason) { }
 
     @Override
-    public void sendBluetoothAdapterConnectionStateChange(int state) { }
-
-    @Override
-    public void sendBluetoothAdapterStateChange(int state) { }
+    public void onBluetoothConnectionStateChanged() { }
 
     @Override
     public int syncGetWifiState() {
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java
index 6c69217..e23c170 100644
--- a/service/java/com/android/server/wifi/WifiConnectivityManager.java
+++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java
@@ -433,13 +433,6 @@
         mConfigManager.updateUserDisabledList(results);
     }
 
-    /**
-     * Set whether bluetooth is in the connected state
-     */
-    public void setBluetoothConnected(boolean isBluetoothConnected) {
-        mNetworkSelector.setBluetoothConnected(isBluetoothConnected);
-    }
-
     private class CachedWifiCandidates {
         public final long timeSinceBootMs;
         public final Map<WifiCandidates.Key, Integer> candidateRssiMap;
diff --git a/service/java/com/android/server/wifi/WifiGlobals.java b/service/java/com/android/server/wifi/WifiGlobals.java
index 32d903d..2d69fe9 100644
--- a/service/java/com/android/server/wifi/WifiGlobals.java
+++ b/service/java/com/android/server/wifi/WifiGlobals.java
@@ -41,6 +41,7 @@
 
     private final AtomicInteger mPollRssiIntervalMillis = new AtomicInteger(-1);
     private final AtomicBoolean mIpReachabilityDisconnectEnabled = new AtomicBoolean(true);
+    private final AtomicBoolean mIsBluetoothConnected = new AtomicBoolean(false);
 
     public WifiGlobals(Context context) {
         mContext = context;
@@ -73,4 +74,24 @@
     public void setIpReachabilityDisconnectEnabled(boolean enabled) {
         mIpReachabilityDisconnectEnabled.set(enabled);
     }
+
+    /** Set whether bluetooth is enabled. */
+    public void setBluetoothEnabled(boolean isEnabled) {
+        // If BT was connected and then turned off, there is no CONNECTION_STATE_CHANGE message.
+        // So set mIsBluetoothConnected to false if we get a bluetooth disable while connected.
+        // But otherwise, Bluetooth being turned on doesn't mean that we're connected.
+        if (!isEnabled) {
+            mIsBluetoothConnected.set(false);
+        }
+    }
+
+    /** Set whether bluetooth is connected. */
+    public void setBluetoothConnected(boolean isConnected) {
+        mIsBluetoothConnected.set(isConnected);
+    }
+
+    /** Get whether bluetooth is connected */
+    public boolean isBluetoothConnected() {
+        return mIsBluetoothConnected.get();
+    }
 }
diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java
index 98f8618..60b61e4 100644
--- a/service/java/com/android/server/wifi/WifiInjector.java
+++ b/service/java/com/android/server/wifi/WifiInjector.java
@@ -322,7 +322,7 @@
         mThroughputPredictor = new ThroughputPredictor(mContext);
         mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiScoreCard, mScoringParams,
                 mWifiConfigManager, mClock, mConnectivityLocalLog, mWifiMetrics, mWifiNative,
-                mThroughputPredictor, mWifiChannelUtilizationScan);
+                mThroughputPredictor, mWifiChannelUtilizationScan, mWifiGlobals);
         CompatibilityScorer compatibilityScorer = new CompatibilityScorer(mScoringParams);
         mWifiNetworkSelector.registerCandidateScorer(compatibilityScorer);
         ScoreCardBasedScorer scoreCardBasedScorer = new ScoreCardBasedScorer(mScoringParams);
diff --git a/service/java/com/android/server/wifi/WifiNetworkSelector.java b/service/java/com/android/server/wifi/WifiNetworkSelector.java
index 1444fcb..70c7b98 100644
--- a/service/java/com/android/server/wifi/WifiNetworkSelector.java
+++ b/service/java/com/android/server/wifi/WifiNetworkSelector.java
@@ -103,11 +103,11 @@
     private final WifiNative mWifiNative;
     private final ThroughputPredictor mThroughputPredictor;
     private final WifiChannelUtilization mWifiChannelUtilization;
+    private final WifiGlobals mWifiGlobals;
 
     private final Map<String, WifiCandidates.CandidateScorer> mCandidateScorers = new ArrayMap<>();
     private boolean mIsEnhancedOpenSupportedInitialized = false;
     private boolean mIsEnhancedOpenSupported;
-    private boolean mIsBluetoothConnected = false;
 
     /**
      * Interface for WiFi Network Nominator
@@ -963,7 +963,7 @@
                 scanDetail.getNetworkDetail().getMaxNumberSpatialStreams(),
                 scanDetail.getNetworkDetail().getChannelUtilization(),
                 channelUtilizationLinkLayerStats,
-                mIsBluetoothConnected);
+                mWifiGlobals.isBluetoothConnected());
     }
 
     /**
@@ -1026,13 +1026,6 @@
     private static final int ID_PREFIX = 42;
     private static final int MIN_SCORER_EXP_ID = ID_PREFIX * ID_SUFFIX_MOD;
 
-    /**
-     * Set whether bluetooth is in the connected state
-     */
-    public void setBluetoothConnected(boolean isBlueToothConnected) {
-        mIsBluetoothConnected = isBlueToothConnected;
-    }
-
     WifiNetworkSelector(
             Context context,
             WifiScoreCard wifiScoreCard,
@@ -1043,7 +1036,8 @@
             WifiMetrics wifiMetrics,
             WifiNative wifiNative,
             ThroughputPredictor throughputPredictor,
-            WifiChannelUtilization wifiChannelUtilization) {
+            WifiChannelUtilization wifiChannelUtilization,
+            WifiGlobals wifiGlobals) {
         mContext = context;
         mWifiScoreCard = wifiScoreCard;
         mScoringParams = scoringParams;
@@ -1054,5 +1048,6 @@
         mWifiNative = wifiNative;
         mThroughputPredictor = throughputPredictor;
         mWifiChannelUtilization = wifiChannelUtilization;
+        mWifiGlobals = wifiGlobals;
     }
 }
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java
index e673577..74ccd5d 100644
--- a/service/java/com/android/server/wifi/WifiServiceImpl.java
+++ b/service/java/com/android/server/wifi/WifiServiceImpl.java
@@ -191,6 +191,7 @@
     private final WifiLog mLog;
     private final WifiConnectivityManager mWifiConnectivityManager;
     private final ConnectHelper mConnectHelper;
+    private final WifiGlobals mWifiGlobals;
     /**
      * Verbose logging flag. Toggled by developer options.
      */
@@ -288,6 +289,7 @@
         mWifiDataStall = wifiInjector.getWifiDataStall();
         mWifiNative = wifiInjector.getWifiNative();
         mConnectHelper = wifiInjector.getConnectHelper();
+        mWifiGlobals = wifiInjector.getWifiGlobals();
     }
 
     /**
@@ -409,13 +411,19 @@
                     } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
                         int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                                 BluetoothAdapter.STATE_DISCONNECTED);
+                        boolean isConnected = state != BluetoothAdapter.STATE_DISCONNECTED;
+                        mWifiGlobals.setBluetoothConnected(isConnected);
+                        // TODO(b/159060934): should this notification be sent to all CMMs?
                         mActiveModeWarden.getPrimaryClientModeManager()
-                                .sendBluetoothAdapterConnectionStateChange(state);
+                                .onBluetoothConnectionStateChanged();
                     } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                         int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                 BluetoothAdapter.STATE_OFF);
+                        boolean isEnabled = state != BluetoothAdapter.STATE_OFF;
+                        mWifiGlobals.setBluetoothEnabled(isEnabled);
+                        // TODO(b/159060934): should this notification be sent to all CMMs?
                         mActiveModeWarden.getPrimaryClientModeManager()
-                                .sendBluetoothAdapterStateChange(state);
+                                .onBluetoothConnectionStateChanged();
                     } else if (action.equals(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)) {
                         handleIdleModeChanged();
                     } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
@@ -3225,7 +3233,7 @@
             @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
             @NonNull String[] args) {
         WifiShellCommand shellCommand =  new WifiShellCommand(mWifiInjector, this, mContext,
-                mActiveModeWarden.getPrimaryClientModeManager(), mWifiInjector.getWifiGlobals());
+                mActiveModeWarden.getPrimaryClientModeManager(), mWifiGlobals);
         return shellCommand.exec(this, in.getFileDescriptor(), out.getFileDescriptor(),
                 err.getFileDescriptor(), args);
     }
diff --git a/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
index b6a6354..bff0ae5 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
@@ -57,7 +57,6 @@
 import android.app.ActivityManager;
 import android.app.test.MockAnswerUtil.AnswerWithArguments;
 import android.app.test.TestAlarmManager;
-import android.bluetooth.BluetoothAdapter;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -4095,39 +4094,28 @@
         verify(mWifiDataStall).disablePhoneStateListener();
     }
 
-    /**
-     * Verify that Bluetooth active is set correctly with BT state/connection state changes
-     */
     @Test
-    public void verifyBluetoothStateAndConnectionStateChanges() throws Exception {
+    public void onBluetoothConnectionStateChanged() throws Exception {
+        assertEquals("DefaultState", getCurrentState().getName());
+        mCmi.onBluetoothConnectionStateChanged();
+        mLooper.dispatchAll();
+        verify(mWifiNative, never()).setBluetoothCoexistenceScanMode(any(), anyBoolean());
+
+        // Enter Connect Mode
+        when(mWifiGlobals.isBluetoothConnected()).thenReturn(false);
         startSupplicantAndDispatchMessages();
-        mCmi.sendBluetoothAdapterStateChange(BluetoothAdapter.STATE_ON);
-        mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(1)).setBluetoothConnected(false);
+        assertEquals("DisconnectedState", getCurrentState().getName());
+        verify(mWifiNative).setBluetoothCoexistenceScanMode(any(), eq(false));
 
-        mCmi.sendBluetoothAdapterConnectionStateChange(BluetoothAdapter.STATE_CONNECTED);
+        when(mWifiGlobals.isBluetoothConnected()).thenReturn(true);
+        mCmi.onBluetoothConnectionStateChanged();
         mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(1)).setBluetoothConnected(true);
+        verify(mWifiNative).setBluetoothCoexistenceScanMode(any(), eq(true));
 
-        mCmi.sendBluetoothAdapterStateChange(BluetoothAdapter.STATE_OFF);
+        when(mWifiGlobals.isBluetoothConnected()).thenReturn(false);
+        mCmi.onBluetoothConnectionStateChanged();
         mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(2)).setBluetoothConnected(false);
-
-        mCmi.sendBluetoothAdapterStateChange(BluetoothAdapter.STATE_ON);
-        mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(3)).setBluetoothConnected(false);
-
-        mCmi.sendBluetoothAdapterConnectionStateChange(BluetoothAdapter.STATE_CONNECTING);
-        mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(2)).setBluetoothConnected(true);
-
-        mCmi.sendBluetoothAdapterConnectionStateChange(BluetoothAdapter.STATE_DISCONNECTED);
-        mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(4)).setBluetoothConnected(false);
-
-        mCmi.sendBluetoothAdapterConnectionStateChange(BluetoothAdapter.STATE_CONNECTED);
-        mLooper.dispatchAll();
-        verify(mWifiConnectivityManager, times(3)).setBluetoothConnected(true);
+        verify(mWifiNative, times(2)).setBluetoothCoexistenceScanMode(any(), eq(false));
     }
 
     /**
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
index 9b3fbc4..30db308 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
@@ -3103,17 +3103,6 @@
     }
 
     /**
-     *  Verify that WifiNetworkSelector sets bluetoothConnected correctly
-     */
-    @Test
-    public void verifyWifiNetworkSelectorSetBluetoothConnected() {
-        mWifiConnectivityManager.setBluetoothConnected(true);
-        verify(mWifiNS).setBluetoothConnected(true);
-        mWifiConnectivityManager.setBluetoothConnected(false);
-        verify(mWifiNS).setBluetoothConnected(false);
-    }
-
-    /**
      *  Verify that WifiChannelUtilization is updated
      */
     @Test
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiGlobalsTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiGlobalsTest.java
index 561c957..cb48d29 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiGlobalsTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiGlobalsTest.java
@@ -16,6 +16,8 @@
 
 package com.android.server.wifi;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import static junit.framework.Assert.assertEquals;
 
 import static org.mockito.Mockito.when;
@@ -61,4 +63,29 @@
         mResources.setInteger(R.integer.config_wifiPollRssiIntervalMilliseconds, 7000);
         assertEquals(6000, mWifiGlobals.getPollRssiIntervalMillis());
     }
+
+    /** Verify that Bluetooth active is set correctly with BT state/connection state changes */
+    @Test
+    public void verifyBluetoothStateAndConnectionStateChanges() {
+        mWifiGlobals.setBluetoothEnabled(true);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isFalse();
+
+        mWifiGlobals.setBluetoothConnected(true);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isTrue();
+
+        mWifiGlobals.setBluetoothEnabled(false);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isFalse();
+
+        mWifiGlobals.setBluetoothEnabled(true);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isFalse();
+
+        mWifiGlobals.setBluetoothConnected(true);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isTrue();
+
+        mWifiGlobals.setBluetoothConnected(false);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isFalse();
+
+        mWifiGlobals.setBluetoothConnected(true);
+        assertThat(mWifiGlobals.isBluetoothConnected()).isTrue();
+    }
 }
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java
index 0531cd6..425a6c6 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java
@@ -39,7 +39,6 @@
 import androidx.test.filters.SmallTest;
 
 import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs;
-import com.android.server.wifi.hotspot2.NetworkDetail;
 import com.android.server.wifi.proto.nano.WifiMetricsProto;
 import com.android.wifi.resources.R;
 
@@ -81,7 +80,8 @@
 
         mLocalLog = new LocalLog(512);
 
-        mWifiNetworkSelector = new WifiNetworkSelector(mContext,
+        mWifiNetworkSelector = new WifiNetworkSelector(
+                mContext,
                 mWifiScoreCard,
                 mScoringParams,
                 mWifiConfigManager, mClock,
@@ -89,7 +89,8 @@
                 mWifiMetrics,
                 mWifiNative,
                 mThroughputPredictor,
-                mWifiChannelUtilization);
+                mWifiChannelUtilization,
+                mWifiGlobals);
 
         mWifiNetworkSelector.registerNetworkNominator(mDummyNominator);
         mDummyNominator.setNominatorToSelectCandidate(true);
@@ -246,9 +247,9 @@
     @Spy private MockResources mResource = new MockResources();
     @Mock private WifiInfo mWifiInfo;
     @Mock private Clock mClock;
-    @Mock private NetworkDetail mNetworkDetail;
     @Mock private ThroughputPredictor mThroughputPredictor;
     @Mock private WifiChannelUtilization mWifiChannelUtilization;
+    @Mock private WifiGlobals mWifiGlobals;
     private ScoringParams mScoringParams;
     private LocalLog mLocalLog;
     private int mThresholdMinimumRssi2G;