繁体   English   中英

Android - 蓝牙设备连接广播

[英]Android - Bluetooth device connected broadcast

我想让广播接收器监听 BT 设备的连接。 谁能告诉我必须听哪个广播? 我尝试了android.bluetooth.device.action.ACL_CONNECTED但它似乎不起作用。

谢谢你。

您可以在BroadcastReceiver.onReceive()函数中使用以下内容来拉取设备:

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

如果您正在查看设备是否正在“连接”,那么您需要 android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED

但是,当设备为 STATE_CONNECTED 时,不会触发此操作。 所以我所做的是当状态连接时,在几秒钟内发送一个广播。

    if (bluetoothAdapter
            .getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTING
            || bluetoothAdapter
                    .getProfileConnectionState(BluetoothProfile.A2DP) == BluetoothProfile.STATE_CONNECTING) {

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (bluetoothAdapter
                        .getProfileConnectionState(BluetoothProfile.HEADSET) != BluetoothProfile.STATE_CONNECTING
                        || bluetoothAdapter
                                .getProfileConnectionState(BluetoothProfile.A2DP) != BluetoothProfile.STATE_CONNECTING) {
                    context.sendBroadcast(new Intent(
                            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED));
                }
            }
        }, 2000);

有点骇人听闻,这就是我在 Android 缺陷跟踪器中发布此问题的原因。 http://code.google.com/p/android/issues/detail?id=25957

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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