Reputation: 25
Below is the code for enabling/disabling but it does not disable the device settings.
I want to know how to disable bluetooth(Camera/Wifi) facility of android device and enable it only through my customized app. User should be able to on the bluetooth only through our app.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
boolean isEnabled = bluetoothAdapter.isEnabled();
if (isEnabled) {
Toast.makeText(getApplicationContext(), "Enabled",
Toast.LENGTH_SHORT).show();
bluetoothAdapter.disable();
} else {
Toast.makeText(getApplicationContext(), "Not Enabled",
Toast.LENGTH_SHORT).show();
bluetoothAdapter.enable();
}
Upvotes: 0
Views: 629
Reputation: 830
You need to implement BroadcastReceiver that listens to BluetoothAdapter.ACTION_STATE_CHANGED
(link). It should disable it back and show some information about your lock to user.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
//do nothing
break;
case BluetoothAdapter.STATE_TURNING_OFF:
//do nothing
break;
case BluetoothAdapter.STATE_ON:
//disable it
break;
case BluetoothAdapter.STATE_TURNING_ON:
//do nothing
break;
}
}
}
};
This is a bit hacky, but this is the best you can do - like I wrote previously there is no way to disable this switch in Settings, as Settings are separate application and you cannot modify it, just like other apps cannot modify your application.
Upvotes: 0