如何检查是否蓝牙连接到设备断开? [英] How to check if bluetooth connection to a device is disconnected?

查看:1589
本文介绍了如何检查是否蓝牙连接到设备断开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候我的蓝牙连接到设备断开连接。我发现这个检查:

I want to know when my bluetooth connection to the device gets disconnected. I found this to check:

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
    this.registerReceiver(mReceiver, filter3);

  //The BroadcastReceiver that listens for bluetooth broadcasts
   mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
               //Device found
            }
            else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
               //Device is now connected
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //Done searching
            }
            else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
               //Device is about to disconnect
            }
            else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
               //Device has disconnected
            }           
        }
    };

ACTION_ACL_FOUND ACTION_ACL_DISCONNECT_REQUESTED ACTION_ACL_DISCONNECTED 的onReceive 方法不能得到解决,或者不是一个字段。

But the ACTION_ACL_FOUND, ACTION_ACL_DISCONNECT_REQUESTED and the ACTION_ACL_DISCONNECTED in the onReceive method can't be resolved or is not a field.

那么,为什么他们不能得到解决?

So why they cannot be resolved?

还是有另一种新的解决方案是什么?

Or is there another new solution for that?

推荐答案

更​​改的onReceive 来:

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           //Device has disconnected
        }           
    }

3场你提到不要在BluetoothAdapter存在:(的http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html)

它们是在BluetoothDevice类: (<一href="http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED" rel="nofollow">http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED)

They are in BluetoothDevice: (http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED)

您也只能注册为监听3的动作,所以你并不真正需要的人,除非你是注册为他们在其他地方。

You are also only registering to listen for 3 actions, so you don't really need the others unless you are registering for them somewhere else.

这篇关于如何检查是否蓝牙连接到设备断开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆