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

    }
相关推荐
火柴就是我13 小时前
让我们实现一个更好看的内部阴影按钮
android·flutter
砖厂小工19 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心20 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心20 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker1 天前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴1 天前
Android17 为什么重写 MessageQueue
android
阿巴斯甜2 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker2 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95272 天前
Andorid Google 登录接入文档
android
黄林晴2 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack