9***@qq.com
9***@qq.com
  • 发布:2021-04-14 11:12
  • 更新:2023-04-26 10:23
  • 阅读:1577

uni.app蓝牙开锁分包发送指令(存在并行调用多次会存在写失败)怎么处理

分类:uni-app

/ 执行演示分包操作 /
writeDevice(_Buffer) {
let _self = this;
let Num = 0;
let ByteLength = _Buffer.byteLength;
console.log(ByteLength, 'ByteLength')
let i = 1;
while (ByteLength > 0) {
i++;
let TmpBuffer;
console.log(i)
console.log("TmpBuffer", TmpBuffer)
console.log("Num", Num)
console.log("ByteLength", ByteLength)
console.log(_self.DeviceID, _self.serviceId, _self.characterId)
if (ByteLength > 20) {
TmpBuffer = _Buffer.slice(Num, Num + 20);
Num += 20;
ByteLength -= 20;
let _self = this;
uTool.delayed(200).then(() => {
console.log('执行常规循环')
// setTimeout(() => {
uni.writeBLECharacteristicValue({
deviceId: _self.DeviceID,
serviceId: _self.serviceId,
characteristicId: _self.characterId,
value: TmpBuffer,
success: (res) => {
/ 发送成功 /
console.log('写入数据成功', res)

                            },  
                            fail: (error) => {  
                                /* 发送失败 */  
                                console.log('写入数据失败', error)  
                            }  
                        })  
                        // }, 500)  
                    })  
                } else {  
                    TmpBuffer = _Buffer.slice(Num, Num + ByteLength)  
                    Num += ByteLength  
                    ByteLength -= ByteLength  
                    /* 当长度不满20的时候执行最后一次发送即可 */  
                    uTool.delayed(1000).then(() => {  
                        console.log('执行最后一次')  
                        // _self.sendAlwaydata()  
                        // setTimeout(() => {  
                        uni.writeBLECharacteristicValue({  
                            deviceId: _self.DeviceID,  
                            serviceId: _self.serviceId,  
                            characteristicId: _self.characterId,  
                            value: TmpBuffer,  
                            success: (res) => {  
                                /* 发送成功 */  
                                console.log('最后一次写入成功', res)  
                            },  
                            fail: (error) => {  
                                /* 发送失败 */  
                                console.log('最后一次写入失败', error)  
                                // _self.sendAlwaydata()  
                            }  
                        })  
                        // }, 400)  
                    })  
                }  
            }  
        },
2021-04-14 11:12 负责人:无 分享
已邀请:
9***@qq.com

9***@qq.com (作者)

IOS的可以成功发送数据,但是安卓的只有第一包成功,后面的都是失败

  • 木杉丶

    最后有解决方案吗

    2021-09-17 18:27

抱米花

抱米花

这个该如何解决呀

y***@qq.com

y***@qq.com

要延时发送

要回复问题请先登录注册