gpt4 book ai didi

android - 在 Android 中使用开关切换蓝牙

转载 作者:行者123 更新时间:2023-11-30 02:10:33 25 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

您可以使用:

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
}

}

关于android - 在 Android 中使用开关切换蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30167110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com