|
|
@ -267,18 +267,18 @@ class BLEHandler { |
|
|
|
// 校验帧头帧尾
|
|
|
|
if (arrbf[0] != 0x89 || arrbf[1] != 0x89) { |
|
|
|
console.log('不是该设备返回的包') |
|
|
|
return false |
|
|
|
return [0]; |
|
|
|
} |
|
|
|
if (packetLen < 14) { |
|
|
|
console.log("包长太短,不是该设备返回的包"); |
|
|
|
return false; |
|
|
|
return [0]; |
|
|
|
} |
|
|
|
|
|
|
|
let dataLength = ((arrbf[10] << 8) & 0xff00) + (arrbf[11] & 0x00ff); |
|
|
|
if (dataLength != (packetLen - 13)) { |
|
|
|
let calLen = packetLen - 13; |
|
|
|
console.log("✘ 数据长度错误: " + dataLength + " is not " + calLen); |
|
|
|
return false; |
|
|
|
return [0]; |
|
|
|
} |
|
|
|
//校验包
|
|
|
|
let check = arrbf[0]; |
|
|
@ -286,10 +286,25 @@ class BLEHandler { |
|
|
|
check ^= arrbf[i]; |
|
|
|
if (check != arrbf[packetLen - 1]) { |
|
|
|
console.log("crc校验错误,请重发." + check); |
|
|
|
return false; |
|
|
|
return [0]; |
|
|
|
} |
|
|
|
|
|
|
|
//数据解密
|
|
|
|
key = arrbf[3]; |
|
|
|
let data_offset = 12; |
|
|
|
|
|
|
|
let result = new Uint8Array(dataLength) |
|
|
|
for (let i = 0; i < dataLength; i++) { |
|
|
|
result[i] = arrbf[i + data_offset]; |
|
|
|
result[i] = (result[i] ^ (key ^ ((byte) i))); |
|
|
|
result[i] += (dataLength - i) ^ key; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('✔ 数据校验成功,接收完整! packet= ' + arrbf) |
|
|
|
return true |
|
|
|
|
|
|
|
console.log('解密明文 result= ' + result) |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
} |