Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android检测蓝牙关闭并停止进程对话框_Android - Fatal编程技术网

Android检测蓝牙关闭并停止进程对话框

Android检测蓝牙关闭并停止进程对话框,android,Android,我有一个进度条,它会一直运行,直到我连接到BLE。下面是代码- @Override public void Progress() { if (activity != null) activity.runOnUiThread(new Runnable() { @Override public void run() { if (progressDi

我有一个进度条,它会一直运行,直到我连接到BLE。下面是代码-

@Override
    public void Progress() {
        if (activity != null)
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (progressDialog == null) {
                        progressDialog = ProgressDialog.show(activity, getString(R.string.wait), getResources().getString(R.string.connecting), true, false);
                    }
                    progress.setVisibility(View.VISIBLE);
                    container.setVisibility(View.GONE);
                }
            });
    }
当进度运行时,我的意思是当显示进度对话框时,如果设置中的蓝牙被禁用,我需要检测它并停止进度并关闭对话框。在
run()

中如何执行此操作请像这样尝试

/**
 * Check for Bluetooth.

 * @return True if Bluetooth is available.
 */


public static boolean isBluetoothAvailable() {

    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return (bluetoothAdapter != null && bluetoothAdapter.isEnabled());
}

为蓝牙创建一个宽带广播接收器。 因此,这将在启用和禁用蓝牙时执行

您必须注册以下操作

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
在宽频广播接收器中,您必须按如下方式更正代码

@Override
public void onReceive(Context context, Intent intent) {
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
         boolean bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
                == BluetoothAdapter.STATE_ON;
        //when bluetooth enabled
        if(bluetoothState) {
            //hide your progress bar here
        }
    }

}

如果bluetooth状态为true,则表示启用蓝牙,如果为false,则表示禁用蓝牙。

如果您想在对话框已经出现的情况下关闭对话框,然后如果您禁用蓝牙并想隐藏对话框,而不在已打开的活动中执行任何操作,则可以使用广播接收器。有关此链接,请参阅:
/* Register BroadcastReceiver with intent action BluetoothAdapter.ACTION_STATE_CHANGED and move your notifiyng code into onReceive method. Don't forget to check if new state is OFF */

if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {

` if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
        == BluetoothAdapter.STATE_OFF)`}

或者,如果您想查看按钮单击,则可以尝试以下代码:

public static boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return (bluetoothAdapter != null && bluetoothAdapter.isEnabled());
}

您可以通过以下方式检查蓝牙是否已禁用:

    boolean isBluetoothDiasabled(){
        BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(!myBluetoothAdapter.isEnabled()){
          return true;
        }
         return false;     
  }

为了使用这个逻辑,我们需要添加权限。如果我不想侦听已启用/已禁用,该怎么办?我只是想知道在任何时候蓝牙的当前状态?然后你可以尝试下面的代码来检查蓝牙的当前状态<代码>BluetoothAdapter BluetoothAdapter=BluetoothAdapter.getDefaultAdapter()
boolean staus=bluetoothAdapter.isEnabled()
,如果返回值为true,则表示蓝牙已启用。。。。。