From a569cabf7633574c8efbd0a5fb18c4894cbc6436 Mon Sep 17 00:00:00 2001 From: peng fan Date: Thu, 8 Aug 2024 15:21:00 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=A7=A3=E5=86=B3=E5=85=B6=E4=BB=96=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=97=A0=E6=B3=95=E6=8E=A5=E5=8F=97=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98=202.=E8=A7=A3=E5=86=B3=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=88=86=E5=8C=85=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/demo/demo.vue | 82 ++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/pages/demo/demo.vue b/pages/demo/demo.vue index 158eb9b..6b4352b 100644 --- a/pages/demo/demo.vue +++ b/pages/demo/demo.vue @@ -265,7 +265,8 @@ export default { console.log('未加工的数据', Array.from(Uint8ArrayData)) for(let i = 0; i < Math.ceil(data.byteLength / 20); i++){ - let buffer = new ArrayBuffer(20) + let length = i === (Math.ceil(data.byteLength / 20)-1)?data.byteLength%20:20 + let buffer = new ArrayBuffer(length) let binaryData = new Uint8Array(buffer) for(let j = 0; j < 20; j++){ if(i * 20 + j < data.byteLength){ @@ -277,7 +278,7 @@ export default { serviceId: that.serviceId, characteristicId: that.characteristicId2, value: buffer, - success(write) { + success() { const hexString = Array.from(binaryData, byte => byte.toString(10)).join(',') console.log('写入的数据', hexString) @@ -290,9 +291,38 @@ export default { }) } }, + // 解析特征值返回数据 + parsingdata(binaryData) { + const that = this + // 明文 0x20 SM4事先约定密钥 0x22 + if(binaryData[7] === 0x20) { + if(binaryData[12] === 48 && binaryData[13] === 144){ + const publicKey = binaryData.slice(15, 31) + console.log('公钥', Array.from(publicKey)) + that.publicKey = publicKey + } + } else if(binaryData[7] === 0x22) { + const cebBinaryData = binaryData.slice(12, binaryData.length - 2) + console.log('sm4返回参数', Array.from(cebBinaryData)) + const key = new Uint8Array(16) + for (let i = 0; i < that.lockId.length; i++) { + key[i] = that.lockId.charCodeAt(i) + } + const decrypted = sm4.decrypt(cebBinaryData, key, { mode: 'ecb', output: 'array' }) + console.log('ecb解密后的数据', decrypted) + if(decrypted[0] === 48 && decrypted[1] === 145) { + that.commKey = decrypted.slice(3, 19) + that.signKey = decrypted.slice(19, 35) + console.log('commKey', Array.from(that.commKey)) + console.log('signKey', Array.from(that.signKey)) + } + } + }, // 监听特征值变化 notifyBLECharacteristicValueChange() { const that = this + let completeData + let length uni.notifyBLECharacteristicValueChange({ state: true, deviceId: that.deviceId, @@ -308,27 +338,21 @@ export default { if(binaryData[0] === 239 && binaryData[1] === 1 && binaryData[2] === 238 && binaryData[3] === 2) { console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte => byte.toString(16)).join(',')) - // 明文 0x20 SM4事先约定密钥 0x22 - if(binaryData[7] === 0x20) { - if(binaryData[12] === 48 && binaryData[13] === 144){ - const publicKey = binaryData.slice(15, 31) - console.log('公钥', Array.from(publicKey)) - that.publicKey = publicKey - } - } else if(binaryData[7] === 0x22) { - const cebBinaryData = binaryData.slice(12, binaryData.length - 2) - console.log('sm4返回参数', Array.from(cebBinaryData)) - const key = new Uint8Array(16) - for (let i = 0; i < that.lockId.length; i++) { - key[i] = that.lockId.charCodeAt(i) - } - const decrypted = sm4.decrypt(cebBinaryData, key, { mode: 'ecb', output: 'array' }) - console.log('ecb解密后的数据', decrypted) - if(decrypted[0] === 48 && decrypted[1] === 145) { - that.commKey = decrypted.slice(3, 19) - that.signKey = decrypted.slice(19, 35) - console.log('commKey', Array.from(that.commKey)) - console.log('signKey', Array.from(that.signKey)) + length = binaryData[8] * 256 + binaryData[9] + if(length + 14 > binaryData.length) { + completeData = binaryData + } else { + that.parsingdata(binaryData) + } + } else { + if(completeData.length > 0) { + const combinedArray = new Uint8Array(completeData.length + binaryData.length) + combinedArray.set(completeData, 0); + combinedArray.set(binaryData, completeData.length) + completeData = combinedArray + if(length + 14 === completeData.length) { + that.parsingdata(completeData) + completeData = new Uint8Array(0) } } } @@ -373,18 +397,6 @@ export default { } that.showList = false that.notifyBLECharacteristicValueChange() - if(uni.$u.os() === 'android') { - uni.setBLEMTU({ - deviceId: that.deviceId, - mtu: 256, - success() { - console.log('mtu设置成功') - }, - fail(res) { - console.log('mtu设置失败', res) - } - }) - } uni.showToast({ title: '数据获取成功', icon: 'none',