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

    }
相关推荐
渡我白衣4 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
Android-Flutter6 小时前
flutter GetX 详解
android·flutter
MyFreeIT8 小时前
Android Manual
android
hai_android8 小时前
AIDL 实现 IPC 进程间通信
android·android studio
AFinalStone10 小时前
Android 7系统无障碍服务(五)AccessibilityNodeInfo 的构建与查询
android·无障碍服务
今天要早睡_11 小时前
C++ 核心语法速过:命名空间、引用、函数重载与 nullptr 深度解析
android·java·c++
啦啦啦~~~22211 小时前
PC端+安卓端阅读器推荐!开源本地小说阅读器软件,
android·论文阅读·windows·开源软件·福昕阅读器
我命由我1234512 小时前
Android Jetpack Compose 开发问题:Unresolved reference ‘Icons‘.
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
爱笑鱼12 小时前
Android 系统启动机制(六):Zygote 创建 App 为什么走 Socket?哪些是源码事实,哪些是架构解释?
android·linux
AI2中文网12 小时前
安卓模拟器启动卡在进度条?两步解决硬件加速冲突
android