如何识别*哪个*蓝牙设备导致ACTION_ACL_CONNECTED广播?

时间:2012-02-27 03:27:46

标签: android bluetooth broadcastreceiver

我想听一些特定的蓝牙设备的连接/断开连接,这些设备的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)我直接使用“BluetoothDevice”。它对广播的反应很好,但它并没有告诉我这两个物理设备中的哪一个涉及行动。他们是一种找出方法吗?不允许使用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_DEVICE“在此类广播的每个意图中都用作Parcelable BluetoothDevice额外字段。”但它只向我返回null:

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

2 个答案:

答案 0 :(得分:15)

这使设备连接到:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

作为一个新手,我误解了这个可以理解的概念。 EXTRA_DEVICE是一个String,但它只是该对象的标记。因此,无需注册或收听BluetoothDevice的个别实例。当广播动作时,意图将告知哪个物理设备引起了该动作。 (我可以为此自己+1 :-D)

答案 1 :(得分:1)

intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED);
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED);

intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED);

是相同的值。它的静态价值。 BluetoothDevice.ACTION_ACL_CONNECTED和BluetoothDeviceACTION_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;
        }
    }
}

};

我希望这可以帮到你