android - Android BLE:onCharacteristicRead 仅在第一次工作
问题描述
Gatt 通信仅在第一次使用时才有效。
我已经阅读了很多与此相关的问题,但没有解决方案有帮助。
整个过程:
1. 重启手机
2. 运行应用
3. 应用连接到 BLE 设备并获取可访问的 Wifi 网络列表 (SdkGattAttributes.WIFI_CHAR_WIFI_LIST)
到现在一切正常
4. 重启应用
5. 应用连接到设备并尝试获取 wifi列表,但onCharacteristicRead
从未收到。没有writeCharacteristic
在这 6 之前发送
。手机重启后应用程序能够获取 wifi 列表,但只能再次获取一次
什么可能是错误的。一些资源释放还是什么?如果需要,我可以发布一些代码。
提前致谢。
解决方案
我最终找到了解决方案,所以我在这里为其他人发布。
问题是在成功连接后设置 MTUmBluetoothGatt.requestMtu(512)
并且一个接一个地请求服务,mBluetoothGatt.discoverServices()
这可能会使 gatt 感到困惑。
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
mBluetoothGatt.requestMtu(512);
}
}
解决方案:首先请求mtu,完成后发现服务
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.requestMtu(512);
}
}
public void onMtuChanged (BluetoothGatt gatt, int mtu, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
mBluetoothGatt.discoverServices();
}
}
推荐阅读
- android-jetpack-compose - jetpack compose:滚动到底部侦听器(列表末尾)
- flutter - 在颤动中使用底部导航栏时如何传递列表视图数据?
- auth0 - Auth0 - 使用不同的配置文件登录 1 个租户中的 2 个应用程序
- docker - 为 Kafka-MQTT-MongoDB 运行 docker 容器;LEADER_NOT_AVAILABLE 用于 kafka 主题
- ios - Firebase 实时数据库 - 如果添加了新数据,tableview 会更新所有单元格数据
- javascript - Vue 3 Composition Api - 查找对象并渲染
- batch-file - 如何在 bat 文件中使用 sendKey 方法?
- python - 无法通过网络表单在 django 中上传文件
- delphi - Delphi 中区分大小写的 MatchesMask 替代方案
- android - 在颤振中调整 api 26 的 build.gradle