hengsen
hengsen
  • 发布:2025-10-31 14:22
  • 更新:2025-10-31 14:22
  • 阅读:17

【报Bug】使用uni.getBLEDeviceCharacteristics无法获取设备特征值,API响应成功但是返回结果为空数组

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 14.5 (23F79)

HBuilderX类型: 正式

HBuilderX版本号: 4.76

手机系统: Android

手机系统版本号: Android 12

手机厂商: 华为

手机机型: 华为荣耀60se

页面类型: vue

vue版本: vue3

打包方式: 离线

项目创建方式: HBuilderX

示例代码:

递归获取设备服务
_getCharacteristics(serviceId: string) {
const deviceId = this._deviceId;
const maxAttempts = 10;
let attempt = 0;

const tryGetCharacteristics = (): Promise<any[]> => {  
  return new Promise((resolve, reject) => {  
    uni.getBLEDeviceCharacteristics({  
      deviceId,  
      serviceId,  
      success(res: any) {  
        if (res.characteristics && res.characteristics.length > 0) {  
          resolve(res.characteristics);  
        } else if (attempt < maxAttempts - 1) {  
          attempt++;  
          setTimeout(() => {  
            tryGetCharacteristics().then(resolve).catch(reject);  
          }, 1000);  
        } else {  
          resolve(res.characteristics);  
        }  
      },  
      fail(e: any) {  
        if (attempt < maxAttempts - 1) {  
          attempt++;  
          setTimeout(() => {  
            tryGetCharacteristics().then(resolve).catch(reject);  
          }, 1000);  
        } else {  
          reject(new BluetoothError(e));  
        }  
      },  
    });  
  });  
};  

return tryGetCharacteristics();  

}

递归获取设备特征值
_getCharacteristics(serviceId: string) {
const deviceId = this._deviceId;
const maxAttempts = 10;
let attempt = 0;

const tryGetCharacteristics = (): Promise<any[]> => {  
  return new Promise((resolve, reject) => {  
    uni.getBLEDeviceCharacteristics({  
      deviceId,  
      serviceId,  
      success(res: any) {  
        if (res.characteristics && res.characteristics.length > 0) {  
          resolve(res.characteristics);  
        } else if (attempt < maxAttempts - 1) {  
          attempt++;  
          setTimeout(() => {  
            tryGetCharacteristics().then(resolve).catch(reject);  
          }, 1000);  
        } else {  
          resolve(res.characteristics);  
        }  
      },  
      fail(e: any) {  
        if (attempt < maxAttempts - 1) {  
          attempt++;  
          setTimeout(() => {  
            tryGetCharacteristics().then(resolve).catch(reject);  
          }, 1000);  
        } else {  
          reject(new BluetoothError(e));  
        }  
      },  
    });  
  });  
};  

return tryGetCharacteristics();  

}

操作步骤:

-

预期结果:

{
"characteristics": [
{
"uuid": "0000FF01-0000-1000-8000-00805F9B34FB",
"properties": {
"read": false,
"write": true,
"notify": false,
"indicate": false
}
},
{
"uuid": "0000FF02-0000-1000-8000-00805F9B34FB",
"properties": {
"read": true,
"write": false,
"notify": true,
"indicate": false
}
}
],
"errMsg": "getBLEDeviceCharacteristics:ok"
}

实际结果:

{
"characteristics": [],
"errMsg": "getBLEDeviceCharacteristics:ok"
}

bug描述:

测试过一加、红米、华为荣耀60、华为荣耀60se、pixel这些安卓手机,目前发现华为荣耀60se使用getBLEDeviceServices无法拿到服务uuid,看了社区有说增加延时可以拿到,所以在调用getBLEDeviceServices时使用了递归的方式。测试发现确实能拿到设备的服务uuid,但是使用设备id和设备服务uuid去获取特征值时,得到的一直是空数组。获取特征值的方法也使用了递归的方式。(在荣耀60se使用其它的蓝牙助手工具却能看到服务和特征值)

2025-10-31 14:22 负责人:无 分享
已邀请:

要回复问题请先登录注册