You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
416 lines
13 KiB
416 lines
13 KiB
// import { writeBLECharacteristicValue, reverseHexString, intToHexString } from '../../utils/bleUtil.js'
|
|
|
|
Page({
|
|
data: {
|
|
// array: [{
|
|
// message: 'foo',
|
|
// }, {
|
|
// message: 'bar'
|
|
// }]
|
|
list:[],
|
|
bean:{}
|
|
},
|
|
updateList: function() {
|
|
const newArray = this.data.array.concat({ message: 'baz' });
|
|
this.setData({
|
|
array: newArray
|
|
});
|
|
},
|
|
// sendOrder:function(e){
|
|
// const bean =
|
|
// },
|
|
onLoad(options) {
|
|
wx.showToast({
|
|
title: `点击了子项 ${options.name}`,
|
|
icon: 'none'
|
|
});
|
|
const s = options.name;
|
|
wx.request({
|
|
url: 'https://keytest.aik518.com/prod-api/work_key/obd/key/type/search',
|
|
method: 'POST', // 请求方法
|
|
data: JSON.stringify({ // 手动序列化对象为 JSON 字符串
|
|
typeId: s,
|
|
language: 'zh'
|
|
}),
|
|
header: {
|
|
'Content-Type': 'application/json' // 关键标识:ml-citation{ref="1,4" data="citationList"}
|
|
},
|
|
success: res => {
|
|
console.log(res);
|
|
this.setData({
|
|
// list:res.data
|
|
list:res.data.records[0].commandMap
|
|
|
|
// bean:res.data
|
|
|
|
// list: res.data.map(item => ({
|
|
// ...item,
|
|
// isExpanded: false,
|
|
// bgColor: '#fff'
|
|
// }))
|
|
})
|
|
|
|
}
|
|
});
|
|
this.initBluetooth();
|
|
|
|
},initBluetooth:function () {
|
|
wx.getConnectedBluetoothDevices({
|
|
services: [getApp().globalData.serviceUuid],
|
|
success: (res) => {
|
|
if (res.devices.length === 0) {
|
|
// 设备已断开,需重新连接
|
|
console.error("重连失败:", 1)
|
|
this.reconnectDevice();
|
|
} else {
|
|
console.error("重连失败:", 2)
|
|
// 直接复用全局参数,但需重新获取特征值(iOS限制)
|
|
this.getCharacteristics();
|
|
}
|
|
}
|
|
});
|
|
},reconnectDevice:function(){
|
|
wx.createBLEConnection({
|
|
deviceId: getApp().globalData.deviceId,
|
|
success: () => this.getCharacteristics(),
|
|
fail: (err) => console.error("重连失败:", err)
|
|
});
|
|
},getCharacteristics:function(){
|
|
console.error("重连失败:", getApp().globalData.deviceId)
|
|
console.error("重连失败:", getApp().globalData.serviceUuid)
|
|
// wx.getBLEDeviceCharacteristics({
|
|
// deviceId: getApp().globalData.deviceId,
|
|
// serviceId: getApp().globalData.serviceUuid,
|
|
// success: (res) => {
|
|
// const notifyChar = res.characteristics.find(c => c.properties.notify);
|
|
// const writeChar = res.characteristics.find(c => c.properties.write);
|
|
// if (notifyChar && writeChar) {
|
|
// // 更新全局特征值(防止缓存失效)
|
|
// getApp().globalData.notifiUuid = notifyChar.uuid;
|
|
// getApp().globalData.serviceUuid = writeChar.uuid;
|
|
// this.enableNotify();
|
|
// }
|
|
// },
|
|
// fail: (err) => console.error("获取特征值失败:", err)
|
|
// });
|
|
this.enableNotify();
|
|
},enableNotify:function (){
|
|
wx.notifyBLECharacteristicValueChange({
|
|
deviceId: getApp().globalData.deviceId,
|
|
serviceId: getApp().globalData.serviceUuid,
|
|
characteristicId: getApp().globalData.notifiUuid,
|
|
state: true,
|
|
type: 'notification', // iOS 必填
|
|
success: () => {
|
|
console.log("B页面通知已开启");
|
|
this.listenData();
|
|
},
|
|
fail: (err) => console.error("B页面开启通知失败:", err)
|
|
});
|
|
},checkSum:function (hexString) {
|
|
// 移除空格并转换为字节数组
|
|
const hexArray = hexString.replace(/\s+/g, '').match(/.{2}/g);
|
|
if (!hexArray) return 0;
|
|
|
|
// 将HEX字节转换为十进制并累加
|
|
let sum = 0;
|
|
hexArray.forEach(hex => {
|
|
sum += parseInt(hex, 16);
|
|
});
|
|
|
|
// 取低8位
|
|
let yu = sum%256;
|
|
let lastHex = yu.toString(16).padStart(2, '0');
|
|
return lastHex;
|
|
},
|
|
buildHex:function (params) {
|
|
const head = "A3";
|
|
const realData = params;
|
|
const rl = realData.length;
|
|
let l = rl/2+4;
|
|
const ll = l.toString(16);
|
|
const lll = ll.length;
|
|
let lastLenth = "";
|
|
if(lll==1){
|
|
lastLenth = "000"+ll;
|
|
}else if(lll==2){
|
|
lastLenth= "00"+ll;
|
|
}else if(lll==3){
|
|
lastLenth= "0"+ll;
|
|
}else if(lll>4){
|
|
lastLenth = ll.substr(lll-4,lll);
|
|
}
|
|
const frameLenth = ""; //2字节
|
|
let crc = "";//1 byte
|
|
let lldata = head+lastLenth+realData;
|
|
crc = this.checkSum(lldata);
|
|
const lastHex = head+lastLenth+realData+crc;
|
|
return lastHex;
|
|
},bytesToHex:function (bytes) {
|
|
let strHex = ''
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
strHex = strHex + bytes[i].toString(16).padStart(2, '0').toUpperCase()
|
|
}
|
|
return strHex;
|
|
},solveHex:function (params) {
|
|
let hex = params.toString().toUpperCase;
|
|
console.log("hex====>"+hex)
|
|
if(hex.startsWith("A3")){
|
|
let firstlenth = hex.substr(2,6);
|
|
let ff = parseInt(firstlenth,16);
|
|
if(ff/2 ==hex.length){
|
|
let realOrder = hex.substr(6,8);
|
|
switch(realOrder){
|
|
case "A0":
|
|
break;
|
|
case "A1":
|
|
break;
|
|
case "A2":
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '是否清楚故障码?',
|
|
success (res) {
|
|
if (res.confirm) {
|
|
console.log('用户点击确定')
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消')
|
|
}
|
|
}
|
|
})
|
|
break;
|
|
case "A3":
|
|
break;
|
|
case "A4":
|
|
break;
|
|
case "A5":
|
|
break;
|
|
|
|
}
|
|
}else{
|
|
let firstOrder
|
|
}
|
|
}
|
|
},
|
|
listenData:function () {
|
|
wx.onBLECharacteristicValueChange((res) => {
|
|
const value = new Uint8Array(res.value);
|
|
const hex = this.bytesToHex(value);
|
|
console.log("B页面收到数据:", value +"==="+hex);
|
|
// this.solveHex(hex);
|
|
//A3001246554E4354494F4E5F535441525408 A3000A A2F00000 15 00 54
|
|
//A3001246554E4354494F4E5F535441525408 A3000AA2F00000150054
|
|
// let hex = params.toString().toUpperCase;
|
|
//A300094552524F5236 A300064F4B43 A300064F4B43
|
|
//A3000B 554E4B4E4F574E DE
|
|
//A3000A A0100000070064 A3000AA0100000070064
|
|
//A300974C4F470000C1A50100000745080210C000000000000000C1B40200000765080250C0FFFFFFFFFF0000C1E90100000745080210C000000000000000C1F00200000765080250C0FFFFFFFFFF0000C25401000007450802270100000000000000C25F020000076508066701253FFCEFFF0000C2C3010000074508062702F9
|
|
//A300734C4F470000C350010000074508023E0100000000000000C350020000076508017EFFFFFFFFFFFF0000C3DE01000007450802212100000000000000C3F9020000076508100D6121020F8EFD0000C3F901000007450830000000000000000000C3FA0200000765082140EFAEED495C418F
|
|
|
|
//A3001346554E4354494F4E5F46494E4953483C
|
|
console.log("hex====>"+hex)
|
|
if(hex.startsWith("A3")){
|
|
let firstlenth = hex.substring(2,6); //1246
|
|
let ff = parseInt(firstlenth,16);
|
|
|
|
console.log("ff====>"+ff+"hex.length===>"+hex.length+"firstlenth===>"+firstlenth);
|
|
if(ff*2 ==hex.length){
|
|
let realOrder = hex.substring(6,8);
|
|
this.showOrder(realOrder);
|
|
}else{
|
|
let firstOrder = hex.substring(ff*2,hex.length);
|
|
|
|
console.log("firstOrder====>"+firstOrder);
|
|
if(firstOrder.startsWith("A3")){
|
|
let xx = firstOrder.substring(2,6);
|
|
let yy = parseInt(xx,16);
|
|
|
|
console.log("xx====>"+xx+"yy"+yy);
|
|
if(yy*2 ==firstOrder.length){
|
|
let realOrder = firstOrder.substring(6,8);
|
|
this.showOrder(realOrder);
|
|
}else{
|
|
let secondOrder = hex.substring(yy*2,firstOrder.length);
|
|
if(secondOrder.startsWith("A3")){
|
|
let zz = secondOrder.substring(2,6);
|
|
let rr = parseInt(zz,16);
|
|
if(rr*2 == secondOrder.length){
|
|
let realSecondOrder = secondOrder.substring(6,8);
|
|
this.showOrder(realSecondOrder);
|
|
}
|
|
}
|
|
}
|
|
}else{
|
|
|
|
}
|
|
}
|
|
}
|
|
// 自定义数据处理逻辑...
|
|
//163, 0, 18, 70, 85, 78, 67, 84, 73, 79, 78, 95, 83, 84, 65, 82, 84, 8, 163, 0, 10, 162, 240, 0, 0, 21, 0, 84
|
|
//A30012 46554E4354494F4E5F5354415254 08 A3000AA2F00000150054
|
|
});
|
|
},
|
|
showOrder(realOrder){
|
|
var that = this;
|
|
let order = "";
|
|
if(realOrder=="a0"){
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '正在读取,请稍后!',
|
|
showCancel:false,
|
|
success (res) {
|
|
if (res.confirm) {
|
|
|
|
}
|
|
}
|
|
})
|
|
}else if(realOrder=="A1"){
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '无故障码!',
|
|
showCancel:false,
|
|
success (res) {
|
|
if (res.confirm) {
|
|
order = "ccc1";
|
|
that.sendCommonOrder(order);
|
|
console.log('用户点击确定')
|
|
}
|
|
}
|
|
})
|
|
}else if(realOrder=="A2"){
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '是否清楚故障码?',
|
|
success (res) {
|
|
if (res.confirm) {
|
|
order = "ccc2aa";
|
|
that.sendCommonOrder(order);
|
|
console.log('用户点击确定')
|
|
} else if (res.cancel) {
|
|
order = "ccc255";
|
|
that.sendCommonOrder(order);
|
|
console.log('用户点击取消')
|
|
}
|
|
}
|
|
})
|
|
}else if(realOrder =="A3"){
|
|
wx.showModal({
|
|
title: '匹配遥控',
|
|
content: '请输入要匹配的遥控数量(范围1-5)',
|
|
editable:true,
|
|
success (res) {
|
|
if (res.confirm) {
|
|
order = "ccc2aa";
|
|
that.sendCommonOrder(order);
|
|
console.log('用户点击确定')
|
|
} else if (res.cancel) {
|
|
order = "ccc255";
|
|
that.sendCommonOrder(order);
|
|
console.log('用户点击取消')
|
|
}
|
|
}
|
|
})
|
|
}else if(realOrder =="A4"){
|
|
|
|
}else if(realOrder =="A5"){
|
|
|
|
}
|
|
|
|
|
|
},sendCommonOrder:function (hexString){
|
|
const hex = this.buildHex(hexString);
|
|
console.log("发送指令:", hex);
|
|
var buffer = new ArrayBuffer(1);
|
|
var dataView = new DataView(buffer);
|
|
dataView.setUint8(0, 0x01); // 示例数据
|
|
const bbbb = hexStringToArrayBuffer(hex);
|
|
wx.writeBLECharacteristicValue({
|
|
// AA60000005
|
|
deviceId: getApp().globalData.deviceId,
|
|
serviceId: getApp().globalData.serviceUuid,
|
|
characteristicId: getApp().globalData.writeUuid,
|
|
value: bbbb,
|
|
success: function(res) {
|
|
console.log('发送成功');
|
|
},
|
|
fail: function(err) {
|
|
console.error('发送失败', err);
|
|
}
|
|
});
|
|
// HEX转ArrayBuffer工具函数
|
|
function hexStringToArrayBuffer(hex) {
|
|
const bytes = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16)));
|
|
return bytes.buffer;
|
|
}
|
|
},sendBlueOrder:function(e){
|
|
const oder = "aa"+e.currentTarget.dataset.value;
|
|
let oo = "aa60000001";
|
|
this.sendCommonOrder(oo);
|
|
// const hex = this.buildHex(oder);
|
|
|
|
// console.log("发送指令:", hex);
|
|
// var buffer = new ArrayBuffer(1);
|
|
// var dataView = new DataView(buffer);
|
|
// dataView.setUint8(0, 0x01); // 示例数据
|
|
|
|
|
|
// var app = getApp();
|
|
// const bbbb = hexStringToArrayBuffer(hex);
|
|
// wx.writeBLECharacteristicValue({
|
|
// // AA60000005
|
|
// deviceId: getApp().globalData.deviceId,
|
|
// serviceId: getApp().globalData.serviceUuid,
|
|
// characteristicId: getApp().globalData.writeUuid,
|
|
// value: bbbb,
|
|
// success: function(res) {
|
|
// console.log('发送成功');
|
|
// },
|
|
// fail: function(err) {
|
|
// console.error('发送失败', err);
|
|
// }
|
|
// });
|
|
// // HEX转ArrayBuffer工具函数
|
|
// function hexStringToArrayBuffer(hex) {
|
|
// const bytes = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16)));
|
|
// return bytes.buffer;
|
|
// }
|
|
},
|
|
startNotifi:function(){
|
|
|
|
|
|
|
|
|
|
// 启用通知
|
|
|
|
var app = getApp();
|
|
wx.notifyBLECharacteristicValueChange({
|
|
deviceId: app.globalData.deviceId,
|
|
serviceId: '0000ffe0-0000-1000-8000-00805f9b34fb',
|
|
characteristicId: '0000ffe1-0000-1000-8000-00805f9b34fb',
|
|
state: true,
|
|
success: function(res) {
|
|
console.log('启用通知成功');
|
|
}
|
|
});
|
|
|
|
// 监听数据
|
|
wx.onBLECharacteristicValueChange(function(res) {
|
|
console.log('收到数据', res.value);
|
|
});
|
|
},
|
|
getDevice:function(){
|
|
wx.getBLEDeviceServices({
|
|
deviceId: app.globalData.deviceId,
|
|
success: function(res) {
|
|
res.services.forEach(function(service) {
|
|
wx.getBLEDeviceCharacteristics({
|
|
deviceId: app.globalData.deviceId,
|
|
serviceId: service.uuid,
|
|
success: function(res) {
|
|
console.log('特征值列表', res.characteristics);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
})
|