From d1989cd4a2aff585e85a02455745c692386b6a2d Mon Sep 17 00:00:00 2001 From: zw_git <459788263@qq.com> Date: Mon, 13 Sep 2021 11:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=AF=86=E8=8E=B7=E5=8F=96=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E6=98=8E=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/btls/bleHandler.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/utils/btls/bleHandler.js b/utils/btls/bleHandler.js index 9676999..40e0477 100644 --- a/utils/btls/bleHandler.js +++ b/utils/btls/bleHandler.js @@ -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; } }