1

我想使用一个开关:http: //developer.android.com/guide/topics/ui/controls/togglebutton.html

切换需要提示的蓝牙之类的东西。问题是当用户拒绝该操作时。如果按照示例中所示完成,即使蓝牙设备关闭,切换也会显示为“on”,因为用户拒绝了。如果用户接受提示,有没有办法让开关只显示“开”,否则保持“关”?

这就是我试图将开关用于:

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

在这里,一旦您按下开关,您就会收到蓝牙提示,如果您接受它会做它应该做的事情,但如果我拒绝它仍然会切换,我不希望它这样做。我希望这个问题现在更清楚了。

4

1 回答 1

1

您可以使用:

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
}

}
于 2015-05-11T12:06:12.877 回答