Android - 蓝牙设备连接广播

发布于 2024-10-08 12:22:37 字数 116 浏览 3 评论 0原文

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

谢谢。

I want to have a broadcast receiver listening for BT devices connecting.
Could anybody tell me which broadcast I have to listen to?
I tried android.bluetooth.device.action.ACL_CONNECTED but it doesn't seem to work.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-10-15 12:22:37

如果您正在查看设备是否正在“连接”,那么您需要 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);

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

If you are looking whether the device is "connecting" then you'd want android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED

However, this does not get triggered when the device is STATE_CONNECTED. So what I did was when the state is connecting, to send a broadcast in a few seconds.

    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);

Kind of hackish, which is why I posted this issue in the Android defect tracker. http://code.google.com/p/android/issues/detail?id=25957

枫以 2024-10-15 12:22:37

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

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

You can pull the device by using the following inside your BroadcastReceiver.onReceive() function:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文