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

    }
相关推荐
alexhilton1 小时前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab4 小时前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇7 小时前
AOSP15 Input专题InputManager源码分析
android·操作系统
RdoZam9 小时前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
奥陌陌14 小时前
android 打印函数调用堆栈
android
用户9851200358314 小时前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack
恋猫de小郭14 小时前
Android 官方正式官宣 AI 支持 AppFunctions ,Android 官方 MCP 和系统级 OpenClaw 雏形
android·前端·flutter
黄林晴15 小时前
Android 17 Beta 2,隐私这把锁又拧紧了
android
Kapaseker16 小时前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin
bqliang16 小时前
Compose 媒体查询 (Media Query API) 🖱️👇🕹️
android·android jetpack