qt for android, android12 的BLE 设置MTU后无法获取数据

在qt for android 中, 有时多换几个版本的手机,能发现不同的问题,以上就是在开发过程中,遇到的问题: 有的android手机在作ble通信时,不需设置mtu, 默认即可; 有些需要设置; 而有些设置后仍无法成功连接BLE。

终不断的测试与研究,最终发现,对于有些版本的手机,甚至是同版本的手机但品牌不同,测试出的结果也不一样,究其原因是,在BluetoothGatt设置Mtu值后,有些手机不会立马生效,需要有一个等待期,然后再开始Service的搜索。关键代码片段如下:

java 复制代码
         

 ///开始连接;
 m_bluetoothGatt = device.connectGatt(this,false, new BluetoothGattCallback() {//连接某ble;

  @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {//连接状态;
     if (newState == BluetoothProfile.STATE_CONNECTED) {//已连接;

         gatt.requestMtu(247);//设置mtu;有些android手机无法获得数据,需要等待一段时间让配置生效;
                       
         try{

              Thread.sleep(1000); //等待配置生效;

          } catch (InterruptedException e){

               e.printStackTrace();
          }

          gatt.discoverServices(); //开始搜索服务;

    }
相关推荐
plainGeekDev19 分钟前
Android 专家岗 Kotlin 面试题:能答出这些,说明你对语言设计有自己的理解
android·kotlin
plainGeekDev24 分钟前
Android 资深岗 Kotlin 面试题:只会用协程不够,你得懂它为什么这么设计
android·kotlin
StarShip35 分钟前
第一阶段:应用层视图绘制
android
StarShip37 分钟前
第二阶段:RenderThread 渲染处理
android
通玄1 小时前
Jetpack Compose 入门系列(一):从零搭建到基础控件使用
android
StarShip1 小时前
Android 图形渲染流水线完整架构与执行流程分析
android
流年如夢1 小时前
类和对象(上)
android·java·开发语言
用户86022504674722 小时前
从入门到进阶的 React Native 实战指南
android·前端
沐言人生2 小时前
ReactNative 源码分析10——Native View创建流程createView
android·react native