简体   繁体   中英

How to check if a bluetooth device is paired

I'm trying to check if there is a bluetooth device paired when running my app.

In the main activity, I find bluetooth devices and pair to them. In the second activity, I must check if there is a device paired or not.

If a device is conected, it starts automatically sending data, but if there is no conexion, then it simply shows a toast.

I need to do this just when the second activity starts. I found this code, but I don't know how to make it to start when the activity is just created.

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

};

Here is a complete description of the problem, with the correct answer to solve it:

Action to know if there is any bluetooth paired device

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM