getBLEDeviceServices() {
let deviceId = this.deviceId;
this._getBLEDeviceServices({ deviceId }).then(data => {
let services = data.services;
console.log('services',services)
}).catch(data => {
console.log('获取设备服务失败', data)
})
}
- 发布:2021-01-18 12:19
- 更新:2024-07-25 09:46
- 阅读:1889
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win7
HBuilderX类型: 正式
HBuilderX版本号: 3.0.4
手机系统: Android
手机系统版本号: Android 10
手机厂商: 华为
手机机型: mate20pro
页面类型: vue
打包方式: 云端
项目创建方式: HBuilderX
示例代码:
操作步骤:
初始化蓝牙设备 =》搜索蓝牙设备=》连接设备=》获取设备服务 服务为空
初始化蓝牙设备 =》搜索蓝牙设备=》连接设备=》获取设备服务 服务为空
预期结果:
获取到服务id
获取到服务id
实际结果:
有时候获取不到服务
有时候获取不到服务
bug描述:
正常连接低功耗蓝牙,连接成功后获取低功耗蓝牙服务,获取到的服务为空数组,但在ios和微信小程序上正常使用
async getServicesCharacteristics() {
await this.waitFor(1500);
for (let i = 0; i < 10; i++) {
try {
console.log("开始获取 BLE 设备服务");
let arr = await uni.getBLEDeviceServices({deviceId: this.deviceId});
console.log("获取 BLE 设备服务", this.deviceId, arr.services.length, `第${i}次`)
if (arr.services.length !== 0) {
break;
} else {
if (i === 9) {
throw "获取服务失败";
}
await this.waitFor(2000);
}
} catch (e) {
if (i === 9) {
throw "获取服务失败";
}
await this.waitFor(1000);
}
}
for (let i = 0; i < 10; i++) {
try {
let arr = await uni.getBLEDeviceCharacteristics({deviceId: this.deviceId, serviceId: SERVICE_UUID,});
// console.log("获取 BLE 设备特性", arr.characteristics, `第${i}次`)
if (arr.characteristics.length !== 0) {
break;
} else {
if (i === 9) {
throw "获取特性失败";
}
await this.waitFor(500);
}
} catch (e) {
if (i === 9) {
throw "获取特性失败";
}
await this.waitFor(500);
}
}
await this.waitFor(200);
if (uni.$u.os() === 'android') {
for (let i = 0; i < 10; i++) {
try {
await uni.setBLEMTU({deviceId: this.deviceId, mtu: this.mtu})
} catch (e) {
console.error("设置MTU失败", `第${i}次`)
if (i === 9) {
throw "设置MTU失败";
}
await this.waitFor(500);
}
}
}
await this.waitFor(200);
}
waitFor(time = 500) {
return new Promise((resolve) => setTimeout(resolve, time))
}
7***@qq.com
请问最后解决了吗
2022-06-06 15:08