1.解决其他设备无法接受回调问题
2.解决设备分包返回数据,解析问题
This commit is contained in:
parent
a2e7ad12f4
commit
a569cabf76
@ -265,7 +265,8 @@ export default {
|
|||||||
console.log('未加工的数据', Array.from(Uint8ArrayData))
|
console.log('未加工的数据', Array.from(Uint8ArrayData))
|
||||||
|
|
||||||
for(let i = 0; i < Math.ceil(data.byteLength / 20); i++){
|
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)
|
let binaryData = new Uint8Array(buffer)
|
||||||
for(let j = 0; j < 20; j++){
|
for(let j = 0; j < 20; j++){
|
||||||
if(i * 20 + j < data.byteLength){
|
if(i * 20 + j < data.byteLength){
|
||||||
@ -277,7 +278,7 @@ export default {
|
|||||||
serviceId: that.serviceId,
|
serviceId: that.serviceId,
|
||||||
characteristicId: that.characteristicId2,
|
characteristicId: that.characteristicId2,
|
||||||
value: buffer,
|
value: buffer,
|
||||||
success(write) {
|
success() {
|
||||||
const hexString = Array.from(binaryData, byte =>
|
const hexString = Array.from(binaryData, byte =>
|
||||||
byte.toString(10)).join(',')
|
byte.toString(10)).join(',')
|
||||||
console.log('写入的数据', hexString)
|
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() {
|
notifyBLECharacteristicValueChange() {
|
||||||
const that = this
|
const that = this
|
||||||
|
let completeData
|
||||||
|
let length
|
||||||
uni.notifyBLECharacteristicValueChange({
|
uni.notifyBLECharacteristicValueChange({
|
||||||
state: true,
|
state: true,
|
||||||
deviceId: that.deviceId,
|
deviceId: that.deviceId,
|
||||||
@ -308,27 +338,21 @@ export default {
|
|||||||
if(binaryData[0] === 239 && binaryData[1] === 1 && binaryData[2] === 238 && binaryData[3] === 2) {
|
if(binaryData[0] === 239 && binaryData[1] === 1 && binaryData[2] === 238 && binaryData[3] === 2) {
|
||||||
console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte =>
|
console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte =>
|
||||||
byte.toString(16)).join(','))
|
byte.toString(16)).join(','))
|
||||||
// 明文 0x20 SM4事先约定密钥 0x22
|
length = binaryData[8] * 256 + binaryData[9]
|
||||||
if(binaryData[7] === 0x20) {
|
if(length + 14 > binaryData.length) {
|
||||||
if(binaryData[12] === 48 && binaryData[13] === 144){
|
completeData = binaryData
|
||||||
const publicKey = binaryData.slice(15, 31)
|
} else {
|
||||||
console.log('公钥', Array.from(publicKey))
|
that.parsingdata(binaryData)
|
||||||
that.publicKey = publicKey
|
}
|
||||||
}
|
} else {
|
||||||
} else if(binaryData[7] === 0x22) {
|
if(completeData.length > 0) {
|
||||||
const cebBinaryData = binaryData.slice(12, binaryData.length - 2)
|
const combinedArray = new Uint8Array(completeData.length + binaryData.length)
|
||||||
console.log('sm4返回参数', Array.from(cebBinaryData))
|
combinedArray.set(completeData, 0);
|
||||||
const key = new Uint8Array(16)
|
combinedArray.set(binaryData, completeData.length)
|
||||||
for (let i = 0; i < that.lockId.length; i++) {
|
completeData = combinedArray
|
||||||
key[i] = that.lockId.charCodeAt(i)
|
if(length + 14 === completeData.length) {
|
||||||
}
|
that.parsingdata(completeData)
|
||||||
const decrypted = sm4.decrypt(cebBinaryData, key, { mode: 'ecb', output: 'array' })
|
completeData = new Uint8Array(0)
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -373,18 +397,6 @@ export default {
|
|||||||
}
|
}
|
||||||
that.showList = false
|
that.showList = false
|
||||||
that.notifyBLECharacteristicValueChange()
|
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({
|
uni.showToast({
|
||||||
title: '数据获取成功',
|
title: '数据获取成功',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user