Edu
Edu

Reputation: 61

Is it possible to access the bluetooth in android [enable() or disable()] without the user interaction?

I am developing an Android application which will access the Bluetooth to enable and disable it without any user interaction. Can somebody help me on how to do it?

Upvotes: 6

Views: 6456

Answers (5)

r0n9
r0n9

Reputation: 2759

I faced the same issue recently. The target android SDK version is 28.

Based on Android doc the enable()

This method was deprecated in API level 33. Starting with Build.VERSION_CODES.TIRAMISU, applications are not allowed to enable/disable Bluetooth. Compatibility Note: For applications targeting Build.VERSION_CODES.TIRAMISU or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.S or below), they can continue to use this API.

Deprecation Exemptions:

Device Owner (DO), Profile Owner (PO) and system apps.

Hence, to use enable()/disable() without worring SDK version, the app needs to have system permission

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:sharedUserId="android.uid.system"
    ...>

and otehr bluetooth permissions

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

One thing I found annoy after the adapter is enabled is based on the article, the ble scanner could be null if the BluetoothAdapter is disabled. In my case, after the enable() is called the BluetoothApater is not enabled immediatele by some reason that I can not figure out. So when I do var bleScanner = bluetoothAdapter.bluetoothLeScanner the bleScanner is null. The solution I have is not ideal, but works

suspend fun scanDevices() {
...
    bluetoothAdapter.enable()
    var bleScanner = bluetoothAdapter.bluetoothLeScanner
    run waitBleAdapterAvailable@{
        repeat(20) {
            if (null == bleScanner) {
                delay(100)
                bleScanner = bluetoothAdapter.bluetoothLeScanner
            } else {
                return@waitBleAdapterAvailable
            }
        }
    }
    bleScanner.?{
        bleScanner.startScan(filters, settings, scanCallback)
        ...
        bleScanner.stopScan
    }
...
}

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

To enable/disable Bluetooth programmatically without user interaction, use the following code:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (!mBluetoothAdapter.isEnabled()) { //Disable it if enabled
    Intent localIntent;
    localIntent = new Intent();
    localIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    localIntent.addCategory("android.intent.category.ALTERNATIVE");
    localIntent.setData(Uri.parse("4"));
    getBroadcast(paramContext, 0, localIntent, 0).send();
} else { //Enable if disabled
    Intent localIntent;
    localIntent = new Intent();
    localIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    localIntent.addCategory("android.intent.category.ALTERNATIVE");
    localIntent.setData(Uri.parse("4"));
    getBroadcast(paramContext, 0, localIntent, 0).send();
} 

And in your Manifest.xml file:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

Upvotes: 0

user1109618
user1109618

Reputation: 9

yes it is.

private static BluetoothAdapter getBA(){
    return BluetoothAdapter.getDefaultAdapter();
}

/*Methode startet das Bluetooth Modul
 */
public static void BT_ON(){
    BA = getBA();
    BA.enable();
}

public static void BT_STATUS(){
    BA = getBA();
}

Upvotes: 0

Chemik
Chemik

Reputation: 1479

You can do this by using BluetoothAdapter:

    BluetoothAdapter.getDefaultAdapter().disable();
    BluetoothAdapter.getDefaultAdapter().enable();

And add ths permission to manifest:

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Upvotes: 9

Related Questions