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

Commit 668d5582 authored by William Escande's avatar William Escande
Browse files

Cts lib, gracefully handle ble on mode

gms core can call enableBLE anytime. If this is being done just when the
scan setting is activated, the test may fail.
This will ensure that we allow and then disallow the scan setting only
during the wanted period of time.

Bug: 343871425
Flag: TEST_ONLY
Test: atest CtsBluetoothTestCases
Change-Id: I4ed69340de9fcd5fb96f58adeaee999a9a4cc4ee
parent faf3b0ae
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.provider.Settings
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import kotlin.time.Duration
@@ -49,6 +50,8 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull

private const val TAG: String = "BlockingBluetoothAdapter"
// There is no access to the module only API Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE
private const val BLE_SCAN_ALWAYS_AVAILABLE = "ble_scan_always_enabled"

object BlockingBluetoothAdapter {
    private val context = InstrumentationRegistry.getInstrumentation().getContext()
@@ -65,10 +68,14 @@ object BlockingBluetoothAdapter {

    /** Set Bluetooth in BLE mode. Only works if it was OFF before */
    @JvmStatic
    fun enableBLE(): Boolean {
    fun enableBLE(toggleScanSetting: Boolean): Boolean {
        if (!state.eq(STATE_OFF)) {
            throw IllegalStateException("Invalid call to enableBLE while current state is: $state")
        }
        if (toggleScanSetting) {
            Log.d(TAG, "Allowing the scan to be perform while Bluetooth is OFF")
            Settings.Global.putInt(context.contentResolver, BLE_SCAN_ALWAYS_AVAILABLE, 1)
        }
        Log.d(TAG, "Call to enableBLE")
        if (!withPermissions(BLUETOOTH_CONNECT).use { adapter.enableBLE() }) {
            Log.e(TAG, "enableBLE: Failed")
@@ -88,6 +95,8 @@ object BlockingBluetoothAdapter {
            Log.e(TAG, "disableBLE: Failed")
            return false
        }
        Log.d(TAG, "Disallowing the scan to be perform while Bluetooth is OFF")
        Settings.Global.putInt(context.contentResolver, BLE_SCAN_ALWAYS_AVAILABLE, 0)
        return state.waitForStateWithTimeout(stateChangeTimeout, STATE_OFF)
    }

+0 −16
Original line number Diff line number Diff line
@@ -51,20 +51,4 @@ public class BTAdapterUtils {
            BluetoothAdapter adapter, boolean persist, Context ctx) {
        return BlockingBluetoothAdapter.disable(persist);
    }

    /**
     * @deprecated see {@link android.bluetooth.test_utils.BlockingBluetoothAdapter#enableBLE}
     */
    @Deprecated
    public static final boolean enableBLE(BluetoothAdapter adapter, Context ctx) {
        return BlockingBluetoothAdapter.enableBLE();
    }

    /**
     * @deprecated see {@link android.bluetooth.test_utils.BlockingBluetoothAdapter#disableBLE}
     */
    @Deprecated
    public static final boolean disableBLE(BluetoothAdapter adapter, Context ctx) {
        return BlockingBluetoothAdapter.disableBLE();
    }
}