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

Commit 397830c0 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Cts lib, gracefully handle ble on mode" into main am: 204f9819

parents 4b988c49 204f9819
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();
    }
}