gpt4 book ai didi

未为配对设备调用 Android BLE ACTION_ACL_CONNECTED

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:13 24 4
gpt4 key购买 nike

我使用的是 Android 4.4。我有 2 个蓝牙设备,一个老式蓝牙和一个 BLE。就它们传输的数据而言,它们在功能上是相同的。它们都有一个按钮,按下时可以连接和传输数据。

我有蓝牙权限并在我的 list 中注册了一个 BroadcastReceiver,就像这样

...

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

...

<receiver
android:name=".BluetoothBroadcastReceiver">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>

为了测试,我只是在 BroadcastReceiver 中记录对 onReceive 的调用

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getAction());
}

问题是:两个设备都与手机配对,但只有老式蓝牙设备会在按下蓝牙设备按钮时生成 ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED 广播。 BLE 设备尝试连接和传输时没有任何反应。

如果我使用

主动扫描 BLE 连接,我能够成功连接并从 BLE 设备接收数据
mBluetoothAdapter.startLeScan(mLeScanCallback);

我是否必须明确扫描 BLE 设备以允许它们连接? BLE 设备是否在未明确扫描 BLE 设备时生成 ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED 广播?还是应该在与手机配对后自动连接?

最佳答案

ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED 广播仅适用于 BR/EDR(经典蓝牙),不适用于 BLE。

对于 BLE,您必须使用最新 Android 版本提供的 API 和回调。

关于未为配对设备调用 Android BLE ACTION_ACL_CONNECTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com