【问题标题】:What triggers the BluetoothDevice.ACTION_ACL broadcasts?什么触发了 BluetoothDevice.ACTION_ACL 广播?
【发布时间】:2012-03-02 17:38:25
【问题描述】:

我想知道远程物理设备中的哪些事件会触发监听设备中的ACTION_ACL_CONNECTED 和ACTION_ACL_DISCONNECTED。我的测试结果毫无意义。我收集了几个彼此相距几分米的设备:

  • 运行 Android 3.1 的 Galaxy Tab P7500
  • 运行 Android 2.2 的 i5500 手机
  • 带有蓝牙 USB 加密狗的 WinXP 电脑
  • 两个带开/关按钮的耳机

首先,我手动与选项卡中的所有设备配对。 PC 和手机均未与 Tab 以外的任何其他设备配对。 (其中一个耳机永远无法通过选项卡以任何方式找到,但可以通过手动和编程方式轻松地从手机中找到)。然后我有一个简单的应用程序来开始发现并侦听并显示 ACL 广播。这就是发生的事情(每次都是同样的事情,它的疯狂是一致的):

  • startDiscovery() 来自启用所有设备的选项卡:- PC 是唯一找到的设备
  • 在 PC 上禁用蓝牙:- 选项卡上没有反应
  • 在 PC 上启用蓝牙:- 选项卡上没有反应
  • 第一次打开耳机:-标签上的ACTION_ACL_CONNECTED
  • 关闭耳机:- 选项卡上没有反应
  • 再次打开耳机电源:- 在选项卡上快速连续地ACTION_ACL_DISCONNECTED 和ACTION_ACL_CONNECTED
  • 在选项卡上禁用蓝牙:- 在选项卡上没有反应
  • 在选项卡上启用蓝牙:- 在选项卡上启用耳机 ACTION_ACL_CONNECTED
  • startDiscovery() 来自电话:- PC 是唯一被 手机,虽然手机只与 Tab 配对,而不与 个人电脑。否则,手机只会对 Tab 的耳机做出反应 从不反应。

如何解决这个混乱?不能依赖导致ACTION_ACL_CONNECT 的设备,即使它已配对并在范围内通电?

以下是 BroadcastReceiver 和活动 onCreate 的方法,但我不认为这段代码中的细节很重要:

BroadcastReceiver intentReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (device != null) {
            name = device.getName();
        Log.v(TAG, "Device=" + device.getName());
        }
        else {
            name = "None";
        }

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            text1.setText(name + " connected " + (checkCounter++));
            Log.v(TAG, "connected: " + device);
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            text2.setText(name + " disconnected " + (checkCounter++));
        Log.v(TAG, "disconnected: " + device);
        }
        else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            text3.setText( name + " found " + (checkCounter++));
        Log.v(TAG, "found: " + device + "");
        }
        else if (blueAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            text4.setText("Started " + (checkCounter++));
            Log.v(TAG, "Discovery started");
        }
        else if (blueAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            text4.setText("Finished " + (checkCounter++));
            Log.v(TAG, "Discovery finished");
        }
    }
};


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.bluetoothlayout);

    text1 = (TextView)findViewById(R.id.textView1);
    text2 = (TextView)findViewById(R.id.textView2);
    text3 = (TextView)findViewById(R.id.textView3);
    text4 = (TextView)findViewById(R.id.textView4);

    BluetoothDevice mw600 =         blueAdapter.getRemoteDevice("58:17:0C:EB:C5:08");
    BluetoothDevice bt500 =         blueAdapter.getRemoteDevice("00:1D:43:00:C4:54");
    BluetoothDevice galaxyTab = blueAdapter.getRemoteDevice("00:07:AB:6A:96:7D");
    BluetoothDevice pcDongle =  blueAdapter.getRemoteDevice("00:15:83:4D:8B:57");

    intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    intentFilter.addAction(blueAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(blueAdapter.ACTION_DISCOVERY_FINISHED);
    if (!isReceiverRegistered) {
        registerReceiver(intentReceiver, intentFilter);
        isReceiverRegistered = true;
    }
    if (!blueAdapter.isEnabled()) {
        blueAdapter.enable();
    }
    blueAdapter.startDiscovery();
}

【问题讨论】:

    标签: android bluetooth broadcastreceiver device


    【解决方案1】:

    我正在使用 androids 很长一段时间以来确实搞砸了 bt,

    我可以告诉你:

    只要建立成功的连接,就会发送 ACTION_ACL_CONNECTED。这个很简单。

    现在是相当烦人的部分。

    只要连接在硬件级别关闭,就会发送 ACTION_ACL_DISCONNECTED。何时发生这种情况取决于设备本身。 如果您手动断开/拔出另一台设备,它不会以某种方式向机器人发送“老兄,我走了”信号,而是在长达 20 秒后一些看门狗吠叫并且连接正在关闭并发送意图。

    现在我确实只对我连接的 SPP 设备进行了尝试。 耳机 afaik 正在主动连接,因为它不是 SPP。因此,如果您已配对并处于聆听模式,它会自动连接到您。现在我不知道如果你“关机”耳机会做什么。也许它正确断开了连接,或者它只是在没有说再见的情况下中断了连接。在后一种情况下,看门狗与机器人端断开连接需要一些时间,这可能需要 0 到 20 秒,不要问我为什么,这只是我的观察。

    【讨论】:

    • 同意你。可能是 10 秒连接后,接收断开连接的广播
    • 我同意 Android BT 是“一团糟”。回复:ACL_DISCONNECT,如果有硬件断开连接,BT 设备无法告诉 Android 它已经消失,因为设备发生了突然的意外故障。因此,需要通过 Android 轮询来确认设备是否仍然存在...
    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多