【问题标题】:Android - Bluetooth device connected broadcastAndroid - 蓝牙设备连接广播
【发布时间】:2010-12-22 15:06:17
【问题描述】:

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

谢谢。

【问题讨论】:

标签: android bluetooth broadcastreceiver


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多