android - 什么触发了 BluetoothDevice.ACTION_ACL 广播?

标签 android bluetooth broadcastreceiver device

我想知道远程物理设备中的哪些事件会触发监听设备中的 ACTION_ACL_CONNECTEDACTION_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_DISCONNECTEDACTION_ACL_CONNECTED 在选项卡上快速连续
  • 在选项卡上禁用蓝牙:- 在选项卡上没有反应
  • 在选项卡上启用蓝牙:- 耳机 ACTION_ACL_CONNECTED 在选项卡上
  • startDiscovery() from phone: - PC 是唯一能被手机找到的设备 手机,虽然手机只与 Tab 配对,而不与 个人电脑。否则,手机只会对 Tab 的耳机使用react 从不 react 。

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

以下是 BroadcastReceiver 和 Activity 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();
}

最佳答案

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

我可以告诉你:

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

现在是相当烦人的部分。

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

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

关于android - 什么触发了 BluetoothDevice.ACTION_ACL 广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9537833/

相关文章:

android - 通过 Android 应用获取 Google+ 上的个人资料信息

java - Android:调用 Context.startService() 后未启动嵌套 Intent 服务

android - 在 android 中禁用 google ML Kit 库的 firebase 日志记录

android - 仅与蓝牙选项共享 Intent

ios - 通过 Wifi 和/或蓝牙进行点对点数据传输

android - 如何将列添加到 Activity 的 android 数据库表中?

ios - 如何使用 GCDAsyncUdpSocket 通过 wifi 和蓝牙进行多播

android - 如何接收所有的广播信息?

android - 如何检测 MEDIA_BUTTON 双击?

Android 服务意外关闭