gtRfnkN
gtRfnkN

Reputation: 489

Android disable/enable Wifi and Bluetooth permanently

I have an application that should manage the wifi and bluetooth state of a devices. For that it receives an message with the state and wheter or not this state should be forced. Then it applies the state and saves both values.

For example: i send an message to disable wifi and force it. Then i turn wifi off and save the state and that this is forced. Also i have a BroadcastReceiver that listens to Wifi state changes and if received, it first checks if wifi was enabled and if that is okay. if not, it instantly disables wifi again. That works like a charm: public class WifiStateReceiver extends BroadcastReceiver {

public void onReceive(final Context context, final Intent intent) {
    // get new wifi state
    final int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_ENABLING);
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    // if enabling, check if thats okay
    if (wifiState == WifiManager.WIFI_STATE_ENABLING && WIFI_FORCE_DISABLE) {
        wifiManager.setWifiEnabled(false);
    } else

    // if disabling, check if thats okay
    if (wifiState == WifiManager.WIFI_STATE_DISABLING && WIFI_FORCE_ENABLE) {
        wifiManager.setWifiEnabled(true);
    }
}

But if i try the exact same thing with bluetooth, it doesnt switch it back...

public void onReceive(final Context context, final Intent intent) {
    // get new wifi state
    final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // if enabling, check if thats okay
    if (bluetoothState == BluetoothAdapter.STATE_TURNING_ON && BT_FORCE_DISABLE) {
        mBluetoothAdapter.disable();
    } else

    // if disabling, check if thats okay
    if (bluetoothState == BluetoothAdapter.STATE_TURNING_OFF && BT_FORCE_ENABLE) {
        mBluetoothAdapter.enable();
    }
}

Any ideas how i can disable bluetooth permanently?

Upvotes: 2

Views: 2657

Answers (1)

gtRfnkN
gtRfnkN

Reputation: 489

Just 5 minutes more got me the on the right track...

the problem with my approach above is, that i wait listen to turning off / on. It seems that if i disable the bluetooth while it is just turning on, it will just continue to turn on and stay on. So i have to wait until it is actually turned on and then disable it. In other words, i had to remove 8 characters and it works fine:

public void onReceive(final Context context, final Intent intent) {
    // get new wifi state
    final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // if enabling, check if thats okay
    if (bluetoothState == BluetoothAdapter.STATE_ON && BT_FORCE_DISABLE) {
        mBluetoothAdapter.disable();
    } else

    // if disabling, check if thats okay
    if (bluetoothState == BluetoothAdapter.STATE_OFF && BT_FORCE_ENABLE) {
        mBluetoothAdapter.enable();
    }
}

Upvotes: 1

Related Questions