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(); //开始搜索服务;

    }
相关推荐
三少爷的鞋6 小时前
Android 架构七条铁律:架构师真正设计的不是代码,而是秩序
android
xiangxiongfly9158 小时前
Android GsonFactory总结
android·gson·gsonfactory
WythePlus9 小时前
Android 14 SELinux策略深度解析
android·安全策略·selinux策略
俩个逗号。。13 小时前
Kotlin flow 详解
android·开发语言·kotlin
橙光成长15 小时前
android-ad-cleaner:android免 Root 批量关闭系统开屏与营销推送
android·1024程序员节
深圳雨林凯AI15 小时前
图案裂变的底色衰减:alpha通道、亮度对比与白墨分色的量化拆解
android
mmsx15 小时前
Android 模块化收口:能力组合框架、手写 LifecycleOwner 与热修复共存
android
恋猫de小郭18 小时前
Flutter 可以更直接地调用原生 API 了,Dart Direct Native Interop
android·前端·flutter
2501_9160088918 小时前
iPhone 设备信息查看,用 KeyMob 看型号、序列号、电池健康和 UDID
android·ios·小程序·https·uni-app·iphone·webview
好的佩奇1 天前
蓝牙之通信架构
android·flutter·蓝牙