- 发布:2023-06-20 11:33
- 更新:2024-05-30 11:20
- 阅读:384
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 22621.1848
HBuilderX类型: 正式
HBuilderX版本号: 3.8.4
手机系统: 全部
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
测试过的手机:
示例代码:
//分包写入
writeBLE(e, name) {
const validData = uni.base64ToArrayBuffer(e);
const validDataLength = new Uint8Array(validData).length;
const packageSize = 20; // 每个包的最大字节大小
const packageCount = Math.ceil(validDataLength / 19); // 包的数量
const packages = new Array(packageCount).fill().map((_, index) => {
const offset = index * 19;
const remainingData = validDataLength - offset;
const currentPackageSize = Math.min(remainingData, 19);
const packageData = new Uint8Array(currentPackageSize + 1);
// 设置首字节
const hasMorePackages = (index < packageCount - 1);
const firstByte = hasMorePackages ? 0x93 : 0x11
packageData[0] = firstByte
packageData.set(new Uint8Array(validData, offset, currentPackageSize), 1);
return packageData;
});
this.delayLoop(packages, 50, name); //分包发送
},
//延迟50ms发送
delayLoop(packages, delay = 50, name = '读表') {
let that = this
for (let i = 0; i < packages.length; i++) {
setTimeout(() => {
console.log(this.ab2hex(packages[i].buffer), `第${i}次指令`);
console.log(new Date().getTime())
that.write(packages[i].buffer, name);
}, 50 * (i + 1))
}
},
//分包写入
writeBLE(e, name) {
const validData = uni.base64ToArrayBuffer(e);
const validDataLength = new Uint8Array(validData).length;
const packageSize = 20; // 每个包的最大字节大小
const packageCount = Math.ceil(validDataLength / 19); // 包的数量
const packages = new Array(packageCount).fill().map((_, index) => {
const offset = index * 19;
const remainingData = validDataLength - offset;
const currentPackageSize = Math.min(remainingData, 19);
const packageData = new Uint8Array(currentPackageSize + 1);
// 设置首字节
const hasMorePackages = (index < packageCount - 1);
const firstByte = hasMorePackages ? 0x93 : 0x11
packageData[0] = firstByte
packageData.set(new Uint8Array(validData, offset, currentPackageSize), 1);
return packageData;
});
this.delayLoop(packages, 50, name); //分包发送
},
//延迟50ms发送
delayLoop(packages, delay = 50, name = '读表') {
let that = this
for (let i = 0; i < packages.length; i++) {
setTimeout(() => {
console.log(this.ab2hex(packages[i].buffer), `第${i}次指令`);
console.log(new Date().getTime())
that.write(packages[i].buffer, name);
}, 50 * (i + 1))
}
},
操作步骤:
分包写入即可
分包写入即可
预期结果:
写入成功
写入成功
实际结果:
写入失败,断开连接
写入失败,断开连接
bug描述:
uniapp低功耗蓝牙模块连接蓝牙设备,在小程序上writeBLECharacteristicValue分包写入数据正常,写入执行后有回调,打包成APP后无论是安卓还是IOS写入都没有回调,并且写入不成功,已设置延迟,并且多次分包20字节发送。
1 个回复
huangzoro
你好。请问这个问题你解决了吗?