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

    }
相关推荐
李艺为44 分钟前
Android Studio使用switch匹配资源id时报需要常量表达式解决办法
android
YaBingSec3 小时前
玄机靶场-2024ccb初赛sc05 WP
android·运维·网络·笔记·安全·ssh
常利兵3 小时前
解锁Android嵌入式照片选择器,让你的App体验丝滑起飞
android
峥嵘life3 小时前
Android 切换用户后无法获取 MAC 地址分析解决
android·python·macos
JJay.3 小时前
Android BLE 为什么连上了却收不到数据
android
歪楼小能手3 小时前
Android16在开机向导最后添加一个声明界面
android·java·平板
夏沫琅琊3 小时前
Android联系人导入导出
android·kotlin
zh_xuan3 小时前
把Android Library 上传到github并在工程中引用该远程仓库
android·github·远程仓库
诸神黄昏EX3 小时前
Android Google MADA
android
盖丽男3 小时前
使用 GitHub Actions 自动打包 Android APK
android·github