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

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

+3
source share
4 answers

You can do this using the BluetoothAdapter:

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

And add permission to manifest:

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

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"/>
0
source

, .

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();
}
0

All Articles