首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中使用开关切换蓝牙

在Android中使用开关切换蓝牙
EN

Stack Overflow用户
提问于 2015-05-11 19:59:24
回答 1查看 718关注 0票数 1

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

要切换像蓝牙这样的东西,需要提示。问题是当用户拒绝该操作时。如果按照示例所示进行操作,则即使蓝牙设备已关闭,切换也将显示为"on“,因为用户拒绝。有没有办法使开关只显示“开”,如果用户接受提示,否则保持在“关”?

这就是我尝试使用开关的目的:

代码语言:javascript
复制
        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);
        }
    }
}

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

EN

回答 1

Stack Overflow用户

发布于 2015-05-11 20:06:13

您可以使用:

代码语言:javascript
复制
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
}

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30167110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档