Reputation: 11
I want to use a switch: http://developer.android.com/guide/topics/ui/controls/togglebutton.html
to toggle something like Bluetooth which needs a prompt. The problem is when the user denies the action. If it's done just as shown in the example, the toggle will shown "on" even though the Bluetooth device is turned off, because the user denied. Is there a way to make the switch only show "on" if the user accepts the prompt and otherwise stay at "off"?
this is what I am trying to use the switch for:
Switch toggler = (Switch) findViewById(R.id.switch2);
toggler.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
final Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivityForResult(discoverableIntent, DISCOVERABLE_REQUEST);
} else {
// The toggle is disabled
final Intent stopDiscoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
stopDiscoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3);
startActivityForResult(stopDiscoverableIntent,DISCOVERABLE_REQUEST);
}
}
});
public void onActivityResult(int req_code, int res_code, Intent data){
System.out.println("Prior Check: " + res_code);
if(req_code==REQUEST_ENABLE_BT){
if(res_code==RESULT_OK){
Toast.makeText(getBaseContext(),"Bluetooth is now turned ON",Toast.LENGTH_SHORT).show();
System.out.println("BT Req.: " + res_code);
}
if(res_code==RESULT_CANCELED){
Toast.makeText(getBaseContext(),"Bluetooth enable totally failed bru!",Toast.LENGTH_SHORT).show();
System.out.println("BT Req.: " + res_code);
}
}
else if(req_code==DISCOVERABLE_REQUEST){
if (res_code==1){
toggle = true;
Toast.makeText(getBaseContext(),"Infinite discoverability enabled",Toast.LENGTH_SHORT).show();
System.out.println("Disc. Req.: " + res_code);
}
if(res_code==3){
toggle = false;
Toast.makeText(getBaseContext(),"Discover mode ending",Toast.LENGTH_SHORT).show();
System.out.println("Disc. Req.: " + res_code);
}
if(res_code==RESULT_CANCELED && toggle == true){
Toast.makeText(getBaseContext(),"Discover mode ending denied",Toast.LENGTH_SHORT).show();
System.out.println("Disc. Req.: " + res_code);
//Switch sw = (Switch) findViewById(R.id.switch2);
//sw.setChecked(false);
}
if(res_code==RESULT_CANCELED && toggle == false){
Toast.makeText(getBaseContext(),"Infinite discoverability denied",Toast.LENGTH_SHORT).show();
System.out.println("Disc. Req.: " + res_code);
//Switch sw = (Switch) findViewById(R.id.switch2);
//sw.setChecked(false);
}
}
}
Here once you press the switch you get a prompt for bluetooth, if you accept it does what it should do, but if I deny it still switches, which i would like it not to. I hope the question is more clear now.
Upvotes: 1
Views: 768
Reputation: 2130
You can use:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isEnabled())
{
//User accpeted keep toggle on or if off, programmatically turn on
}
else
{
//User rejected keep toggle off or if on, programmatically turn off
}
}
Upvotes: 1