From e0bb3496767a74b7de8b106fc4811dd5edd312fd Mon Sep 17 00:00:00 2001 From: Kai Shi Date: Thu, 12 Dec 2019 12:15:28 -0800 Subject: Wifi: bug fix of BT connection active signal. When BT was connected and then turned off, there is no ACTION_CONNECT_STATE_CHANGE message during on->off transition. The connection active signal remains true in this case. The change is to pass the ACTION_STATE_CHANGE message to ClientModeImpl and generate bluetoothConnectionActive signal correctlY. Bug: 144126207 Test: unit test with atest com.android.server.wifi Test: manual test with phone and BT headset. Change-Id: Ib95ac1a45443f0a0385ff7ce2cb7c9052a8dfc66 --- .../com/android/server/wifi/ClientModeImpl.java | 43 +++++++++++++++++++--- .../com/android/server/wifi/WifiServiceImpl.java | 5 +++ 2 files changed, 43 insertions(+), 5 deletions(-) (limited to 'service') diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java index fcd124c80..bfc1acdb0 100644 --- a/service/java/com/android/server/wifi/ClientModeImpl.java +++ b/service/java/com/android/server/wifi/ClientModeImpl.java @@ -432,8 +432,10 @@ public class ClientModeImpl extends StateMachine { /* 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; /* Get adaptors */ static final int CMD_GET_SUPPORTED_FEATURES = BASE + 61; @@ -1677,12 +1679,21 @@ public class ClientModeImpl extends StateMachine { } /** - * Send a message indicating bluetooth adapter connection state changed + * 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); + } + /** * Trigger dump on the class IpClient object. */ @@ -3088,8 +3099,18 @@ public class ClientModeImpl extends StateMachine { 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; + 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); + message.arg1 != BluetoothAdapter.STATE_DISCONNECTED; mWifiConnectivityManager.setBluetoothConnected(mBluetoothConnectionActive); break; case CMD_ENABLE_RSSI_POLL: @@ -3934,8 +3955,20 @@ public class ClientModeImpl extends StateMachine { } break; case CMD_BLUETOOTH_ADAPTER_STATE_CHANGE: - mBluetoothConnectionActive = (message.arg1 - != BluetoothAdapter.STATE_DISCONNECTED); + // 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; + 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); diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 43a200686..9d31528c4 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -364,6 +364,7 @@ public class WifiServiceImpl extends BaseWifiService { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_USER_REMOVED); intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); + intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); intentFilter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); intentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); boolean trackEmergencyCallState = mContext.getResources().getBoolean( @@ -2589,6 +2590,10 @@ public class WifiServiceImpl extends BaseWifiService { } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.STATE_DISCONNECTED); + mClientModeImpl.sendBluetoothAdapterConnectionStateChange(state); + } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { + int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, + BluetoothAdapter.STATE_OFF); mClientModeImpl.sendBluetoothAdapterStateChange(state); } else if (action.equals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) { boolean emergencyMode = -- cgit v1.2.3