1.解决其他设备无法接受回调问题
2.解决设备分包返回数据,解析问题
This commit is contained in:
parent
a2e7ad12f4
commit
a569cabf76
@ -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',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user