Android 如何识别*哪个*蓝牙设备导致连接ACL的广播动作?

Android 如何识别*哪个*蓝牙设备导致连接ACL的广播动作?,android,bluetooth,broadcastreceiver,Android,Bluetooth,Broadcastreceiver,我想监听一些特定蓝牙设备的连接/断开连接,这些设备的MAC地址我知道,但不一定是成对的(我不想弄乱用户的配对设备列表,反之亦然)。我只对发现他们的存在感兴趣,而不是与他们交流 这与我下面的代码配合得很好!但我的问题是,我无法找出哪个特定的设备正在连接/断开连接,只能知道它发生在其中的某个人身上。我怎样才能知道行动涉及哪一项 首先,我为我的两个特定物理蓝牙设备实例化对象,并将它们添加到我的意图过滤器中: BluetoothDevice myPinkHeadset = mBluetoothA

我想监听一些特定蓝牙设备的连接/断开连接,这些设备的MAC地址我知道,但不一定是成对的(我不想弄乱用户的配对设备列表,反之亦然)。我只对发现他们的存在感兴趣,而不是与他们交流

这与我下面的代码配合得很好!但我的问题是,我无法找出哪个特定的设备正在连接/断开连接,只能知道它发生在其中的某个人身上。我怎样才能知道行动涉及哪一项

首先,我为我的两个特定物理蓝牙设备实例化对象,并将它们添加到我的意图过滤器中:

    BluetoothDevice myPinkHeadset = mBluetoothAdapter.getRemoteDevice("18:17:0C:EB:9C:81");
    BluetoothDevice myPcBluetoothDongle = mBluetoothAdapter.getRemoteDevice("5A:7A:CC:4B:C5:08");

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED);
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED);
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED);
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED);
然后我收听关于他们的广播:

    final BroadcastReceiver intentReceiver = new BroadcastReceiver() {  
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
现在,我想找出哪一个已经连接和/或断开,我不知道如何才能做到这一点

1)我直接使用“蓝牙设备”。它对广播的反应很好,但它没有告诉我这两个物理设备中的哪一个与动作有关。这是一种找到答案的方法吗?不允许Bluetooth.getName(),因为它不是静态类

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
        } 
或者2)我监听两个设备的两个动作

        if (myPinkHeadset .ACTION_ACL_CONNECTED.equals(action)) {
            Log.v(TAG, "Connected to myPinkHeadset ");
        }
        else if (myPinkHeadset .ACTION_ACL_DISCONNECTED.equals(action)) {
            Log.v(TAG, "Disconnected from myPinkHeadset ");
        }
        else if (myPcBluetoothDongle .ACTION_ACL_CONNECTED.equals(action)) { 
            Log.v(TAG, "Connected to myPcBluetoothDongle ");
        }
        else if (myPcBluetoothDongle .ACTION_ACL_DISCONNECTED.equals(action)) {
            Log.v(TAG, "Disconnected from myPcBluetoothDongle ");
但它会记录它与myPinkHeadset的连接,即使我实际激活的是myPvBluetoothDongle。它总是适用于if测试中的第一个测试。它只关心动作本身,而不关心它所关注的对象

我看到EXTRA_设备“在这个类的每一次意图广播中都用作一个可包裹的BluetoothDevice额外字段。”但它只向我返回null:

String extra = intent.getStringExtra(BluetoothDevice.EXTRA_DEVICE);

这使设备连接到:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
作为一个新手,我误解了包裹的概念。EXTRA_设备是一个字符串,但它只是对象的标记。因此,无需注册或侦听BluetoothDevice的单个实例。当一个动作被广播时,意图将告诉是哪个物理设备导致了它。(我能为这个+1自己吗:-D)

都是相同的值。它是静态值。 BluetoothDevice.ACTION\u ACL\u已连接,BluetoothDevice ACTION\u ACL\u已断开

private void register() {
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED ));
}

private final BroadcastReceiver bluetoothBroadCast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    switch (action) {
        case BluetoothDevice.ACTION_ACL_CONNECTED: {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(device.getAddress().equals(myPinkHeadset.getAddress)) {
                //Do what you want
            }
            break;
        }

        case BluetoothDevice.ACTION_ACL_DISCONNECTED: {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            break;
        }
    }
}
})


我希望这能帮助您

犯下与包裹相同的错误,因为字符串混淆
intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED);
private void register() {
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED ));
}

private final BroadcastReceiver bluetoothBroadCast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    switch (action) {
        case BluetoothDevice.ACTION_ACL_CONNECTED: {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(device.getAddress().equals(myPinkHeadset.getAddress)) {
                //Do what you want
            }
            break;
        }

        case BluetoothDevice.ACTION_ACL_DISCONNECTED: {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            break;
        }
    }
}