Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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_Bluetooth - Fatal编程技术网

Android 如何检查蓝牙设备是否配对

Android 如何检查蓝牙设备是否配对,android,bluetooth,Android,Bluetooth,我正在尝试在运行我的应用程序时检查是否有蓝牙设备配对 在主要活动中,我找到蓝牙设备并与之配对。在第二个活动中,我必须检查是否有设备配对 如果连接了一个设备,它会自动开始发送数据,但如果没有连接,它只会显示一个toast 我需要在第二个活动开始时这样做。我找到了这段代码,但我不知道如何在刚刚创建活动时启动它 public void onCreate() { //... IntentFilter filter1 = new IntentFilter(BluetoothDevice.A

我正在尝试在运行我的应用程序时检查是否有蓝牙设备配对

在主要活动中,我找到蓝牙设备并与之配对。在第二个活动中,我必须检查是否有设备配对

如果连接了一个设备,它会自动开始发送数据,但如果没有连接,它只会显示一个toast

我需要在第二个活动开始时这样做。我找到了这段代码,但我不知道如何在刚刚创建活动时启动它

public void onCreate() {
    //...
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
}

//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        //Do something if connected
    }
    else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
        //Do something if disconnected
    }
}

})

以下是问题的完整描述,以及解决问题的正确答案: