小程序
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.
 

768 lines
23 KiB

import {
  onBLEConChange,
  addConnectionListener,
  handleBleData,
  writeBLECharacteristicValue,
  onBLECharacteristicValueChange,
  stringToHex,
  intToHexString
} from '../../utils/bleUtil.js'
Page({
data: {
list:[],
obd_command:[],
commandMap:[],
support:"",
codeList:[],
videoUrl:"",
title:"",
showDelete:false
},
// 在生命周期函数中加载数据
onLoad(options) {
//   onBLECharacteristicValueChange((result) => {
//       this.handleResult(result)
//     })
 
// wx.showToast({
// title: `点击了子项 ${options.name}`,
// icon: 'none'
// });
// wx.showLoading({title:"等待页面初始化"})
const s = options.name;
// console.log(s);
wx.request({
url: 'https://keytest.aik518.com/prod-api/work_key/obd/zk_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);
let list = res.data.records[0].commandMap;
let obdCommondList = res.data.dict.obd_command;
for(var i=0;i<list.length;i++){
let childCommond = list[i];
for(var b = 0;b<obdCommondList.length;b++){
if(childCommond.key==obdCommondList[b].value){
list[i].key = obdCommondList[b].label;
}
}
// if(childCommond.key=="newKeyAdd"){
// list[i].key="新增钥匙";
// }else if(childCommond.key=="oldKeyAdd"){
// list[i].key="老版新增钥匙";
// }else if(childCommond.key=="Keys_DECALL"){
// list[i].key="删除钥匙";
// }
};
wx.setNavigationBarTitle({ title: res.data.records[0].name })
this.setData({
obd_command:res.data.dict.obd_command,
commandMap:list,
support:res.data.records[0].support,
videoUrl:res.data.records[0].videoMiniUrl,
codeList:res.data.records[0].type.codeList,
title:res.data.records[0].name,
showDelete:res.data
})
}
});
this.initBluetooth();
},getservicedata(s){
wx.request({
url: 'https://keytest.aik518.com/prod-api/work_key/obd/zk_key/calc_pin?bcmCode='+s,
method: 'GET', // 请求方法
success: res => {
//{"code":1,"data":{"oldPin":"0862","newPin":"0746","flag":true},"dict":null,"i18nMsg":"请求成功","msg":"请求成功"}
// A3000AAA5501 0CC2FB 76
let oldPin = res.data.data.oldPin;
let newPin = res.data.data.newPin;
console.log(res.data+"==="+oldPin+"==="+newPin);
wx.showModal({
title: '密钥',
content: '老密钥:'+oldPin+" 新密钥:"+newPin,
complete: (res) => {
if (res.confirm) {
}
}
})
}
})
},getserviceKeydata(s){
var that = this;
wx.request({
url: 'https://keytest.aik518.com/prod-api/work_key/obd/zk_key/calc_key?hexStr='+s,
method: 'GET', // 请求方法
success: res => {
//{"code":1,"data":{"flag":true,"hex":"000C0077655F103F009F1FA70A76"},"dict":null,"i18nMsg":"请求成功","msg":"请求成功"}
// 000C0077655F103F009F1FA70A76
let hex = res.data.data.hex;
console.log(res.data+"==="+hex+"===");
let lastHex = "C5"+hex;
that.sendCommonOrder(lastHex,true);
}
})
},
onShareAppMessage() {
return {
title: 'OBD小程序',
path: '/pages/index/index'
};
}
,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("成功==>设备id:", getApp().globalData.deviceId)
console.error("成功===>服务UUID:", getApp().globalData.serviceUuid)
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("打开通道成功====>"+getApp().globalData.notifiUuid);
wx.showToast({
title: '初始化成功',
})
wx.hideLoading();
this.listenData();
},
// fail: (err) => console.error("B页面开启通知失败:", err)
fail:(err) => wx.showModal({
title: '页面开启通知失败',
content: err,
complete: (res) => {
if (res.confirm) {
}
}
})
});
},listenData:function () {
wx.onBLECharacteristicValueChange((res) => {
//A3001246554E4354494F4E5F535441525408 卡住的返回数据
const value = new Uint8Array(res.value);
let hex = this.bytesToHex(value);
console.log("当前页面收到数据:", value +"==="+hex);
// wx.showModal({
// title: '页面收到数据',
// content: hex,
// complete: (res) => {
// if (res.confirm) {
// }
// }
// })
//470007650804 A3000AA1F000001B0059
//023E0100000000000000C350020000076508C2 A3000B4C4F47017EFFFF0D A3002F4C4F47FFFFFFFF0000C738010000074508023E0100000000000000C738020000076508017EFFFFFFFFFFFF33 A3000AA11000000B0069
console.log("hex====>"+hex)
//A3000AAA55010E19C195 A3000AAA55010E19C195
if(hex.includes("AA5501")&&hex.includes("A300")){
wx.hideLoading();
let index = hex.lastIndexOf("AA5501");
index = index+6;
let miyao = hex.substring(index,index+6);
console.log("密钥==>"+miyao);
let finishOrder = "A30006CCC136";
this.sendCommonOrder(finishOrder,false);
this.getservicedata(miyao);
return;
}
if(!hex.startsWith("A3")&&hex.includes("A300")){
let index = hex.lastIndexOf("A300");
console.log("index===>"+index);
hex = hex.substring(index,hex.length);
console.log("hex===>"+hex);
}
if(hex.startsWith("A3")){
let firstlenth = hex.substring(2,6); //1246
let ff = parseInt(firstlenth,16);
// A30010C50009E44CE7E549BF40F9E5A3
// ff====>16hex.length===>32firstlenth===>0010
console.log("ff====>"+ff+"hex.length===>"+hex.length+"firstlenth===>"+firstlenth);
if(ff*2 ==hex.length){
let realOrder = hex.substring(6,8);
let content = hex.substring(8,16);
let inputType = "";
let number = hex.substring(16,18);
let realCount = 0;
if(number !=="00"){
let type = hex.substring(18,20);
let numberLenth = parseInt(number,16);
let indexLast = 20+numberLenth*2;
let count = hex.substring(20,indexLast);
if(type =="A1"){
realCount = parseInt(count);
}else if(type =="A2"){
realCount = parseInt(count,16);
}else if(type =="A3"){
let realNotice = "";
for (var n = 0; n < count.length; n += 2) {
realNotice += count.substr(n+1, 1);
}
realCount = parseInt(realNotice);
}
}
// let inputContent = "";
// let noticeConetntLengh = hex.substring(16,18);
// let noticeContentType = "";
if(realOrder=="A3"){
//A3 00 0D A1 10 00 00 01 02 A1 10 11 27
//A3000A A3 A2 F000002 608
//A3000AA3 A1 F000003112 本田 输入框命令
content = hex.substring(10,18);
inputType = hex.substring(8,10);
// noticeConetntLengh = hex.substring(18,20);
// inputContent = hex.substring();
}
//A30010C50009E44CE7E549BF40F9E5A3
if(realOrder=="C5"){
let lastIndex = hex.length-2;
let c5Hex = hex.substring(8,lastIndex);
this.getserviceKeydata(c5Hex);
return;
}
this.showOrder(realOrder,content,inputType,realCount);
}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);
let content2 = firstOrder.substring(8,16);
let inputType = "";
if(realOrder=="A3"){
content2 = firstOrder.substring(10,18);
inputType = firstOrder.substring(8,10);
}
let number = firstOrder.substring(16,18);
let realCount = 0;
if(number !=="00"){
let type = firstOrder.substring(18,20);
let numberLenth = parseInt(number,16);
let indexLast = 20+numberLenth*2;
let count = firstOrder.substring(20,indexLast);
if(type =="A1"){
realCount = parseInt(count);
}else if(type =="A2"){
realCount = parseInt(count,16);
}else if(type =="A3"){
let realNotice = "";
for (var n = 0; n < count.length; n += 2) {
realNotice += count.substr(n+1, 1);
}
realCount = parseInt(realNotice);
}
}
if(realOrder=="A3"){
//A3 00 0D A1 10 00 00 01 02 A1 10 11 27
//A3000A A3 A2 F000002 608
//A3000AA3 A1 F000003112 本田 输入框命令
content = hex.substring(10,18);
inputType = hex.substring(8,10);
// noticeConetntLengh = hex.substring(18,20);
// inputContent = hex.substring();
}
if(realOrder=="C5"){
let lastIndex = firstOrder.length-2;
let c5Hex = firstOrder.substring(8,lastIndex);
this.getserviceKeydata(c5Hex);
return;
}
this.showOrder(realOrder,content2,inputType,realCount);
}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);
let content3 = secondOrder.substring(8,16);
let inputType = "";
if(realSecondOrder=="A3"){
content3 = secondOrder.substring(10,18);
inputType =secondOrder.substring(8,10);
}
let number = secondOrder.substring(16,18);
let realCount = 0;
if(number !=="00"){
// A3 00 0F A1 10 00 00 1D 04 A3 39 32 34 30 F6
let type = secondOrder.substring(18,20);
let numberLenth = parseInt(number,16);
let indexLast = 20+numberLenth*2;
let count = secondOrder.substring(20,indexLast);
if(type =="A1"){
realCount = parseInt(count);
}else if(type =="A2"){
realCount = parseInt(count,16);
}else if(type =="A3"){
let realNotice = "";
for (var n = 0; n < count.length; n += 2) {
realNotice += count.substr(n+1, 1);
}
realCount = parseInt(realNotice);
}
}
if(realOrder=="A3"){
//A3 00 0D A1 10 00 00 01 02 A1 10 11 27
//A3000A A3 A2 F000002 608
//A3000AA3 A1 F000003112 本田 输入框命令
content = hex.substring(10,18);
inputType = hex.substring(8,10);
// noticeConetntLengh = hex.substring(18,20);
// inputContent = hex.substring();
}
if(realOrder=="C5"){
let lastIndex = secondOrder.length-2;
let c5Hex = secondOrder.substring(8,lastIndex);
this.getserviceKeydata(c5Hex);
return;
}
this.showOrder(realSecondOrder,content3,inputType,realCount);
}
}
}
}else{
}
}
}
});
},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;
},get10to16Hex:function(s){
let hex = s.toString(16);
let hexS = hex.toUpperCase();
if(hexS.length%2!=0){
hexS = "0"+hexS;
}
return hexS;
},
// 点击事件处理函数
handleTextTap: function (e) {
const name = e.currentTarget.dataset.name;
wx.navigateTo({
url: '/pages/playvideo/video?url='+encodeURIComponent(name) // 支持参数传递
})
},addkey(e){
},service(e){
}, getContent(content){
console.log("当前命令====>"+content);
let listdata = this.data.codeList;
for(let i=0;i<listdata.length;i++){
if(listdata[i].type===content){
console.log("返回命令====>"+listdata[i]);
return listdata[i].def;
}
}
},
showOrder(realOrder,content,inputType,realCount){
var that = this;
let order = "";
let ccc = content.toUpperCase();
let message = this.getContent(ccc);
let realCountxxx = realCount;
if(realCount>0){
message = message+realCountxxx;
}
console.log("返回showOrder命令====>"+realOrder+"===="+content+"===="+message+"==="+inputType);
if(realOrder=="A0"){
wx.showLoading({
title: message,
})
// setTimeout(function () {
// wx.hideLoading()
// }, 2000)
}else if(realOrder=="A1"){
if(ccc.includes('10000005')||ccc.includes('10000010')){
this.addObdBurnCount();
}
wx.hideLoading();
wx.showModal({
title: '提示',
content: message,
showCancel:false,
success (res) {
if (res.confirm) {
order = "ccc1";
that.sendCommonOrder(order,true);
console.log('用户点击确定')
}
}
})
}else if(realOrder=="A2"){
wx.hideLoading();
wx.showModal({
title: '提示',
content: message,
cancelText:"否",
confirmText:"是",
success (res) {
if (res.confirm) {
order = "ccc2aa";
that.sendCommonOrder(order,true);
console.log('用户点击确定')
} else if (res.cancel) {
order = "ccc255";
that.sendCommonOrder(order,true);
console.log('用户点击取消')
}
}
})
}else if(realOrder =="A3"){
wx.hideLoading();
wx.showModal({
title: '匹配遥控'+"\r\n"+message,
// content: message,
placeholderText:"请输入",
editable:true,
success (res) {
if (res.confirm) {
console.log("res.content===>"+res.content);
// let key = parseInt( res.content);
let key = res.content;
let lastHex = "";
// // let keyHex = this.get10to16Hex(key);
// let keyHex = key.toString(16);
// keyHex = keyHex.toUpperCase();
// if(keyHex.length%2!=0){
// keyHex = "0"+keyHex;
// }
if(inputType=="A1"){
let key1 = parseInt( key);
lastHex = that.get10to16Hex(key1);
}else if(inputType=="A2"){
// let key = res.content;
lastHex = key;
if(key.length%2==1){
lastHex = "0"+key;
}
}else if(inputType=="A3"){
let spendHex = "";
for(let i=0;i<key.length;i++ ){
let a = key.substring(i,i+1);
a = "3"+a;
spendHex = spendHex+a;
}
lastHex = spendHex;
}
let contentLengh = lastHex.length/2;
let lenghHex = that.get10to16Hex(contentLengh);
order = "ccc3"+inputType+lenghHex+lastHex;
that.sendCommonOrder(order,true);
console.log('用户点击确定')
} else if (res.cancel) {
order = "ccc30055";
that.sendCommonOrder(order,true);
console.log('用户点击取消')
}
}
})
}else if(realOrder =="A4"){
wx.showLoading({
title: message,
})
}else if(realOrder =="A5"){
wx.showLoading({
title: message,
})
}
},
hex_to_ascii(hex) {
hex = hex.toString();
let str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
},sendCommonOrder:function (hexString,isAllHex){
let hex = hexString;
if(isAllHex){
hex = this.buildHex(hexString);
}else{
hex = hexString;
}
// const hex = this.buildHex(hexString);
let upHex = hex.toUpperCase();
console.log("发送指令:", upHex);
var buffer = new ArrayBuffer(1);
var dataView = new DataView(buffer);
dataView.setUint8(0, 0x01); // 示例数据
const bbbb = hexStringToArrayBuffer(upHex);
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){
// let id = getApp().globalData.deviceId;
// let deviceType = getApp().globalData.currentDevice;
// let mac = wx.getStorageSync(id);
// console.log(id+"==="+deviceType+"=="+mac)
const oder = "aa"+e.currentTarget.dataset.name;
// let oo = "aa60000001";
// let oo = "aa53000009";
// this.sendCommonOrder(oder,true);
this.checkObdBurnCount(oder)
},
handleItemClick:function (params) {
const order = "aa"+params.currentTarget.dataset.id;
this.checkObdBurnCount(order)
},
checkObdBurnCount:function (e) {
const oder = e;
this.sendCommonOrder(oder,true);
// let macAddress = getApp().globalData.deviceId;
// let deviceType = getApp().globalData.currentDevice;
// if(deviceType.includes('ios')){
// macAddress = wx.getStorageSync(getApp().globalData.deviceId);
// }
// wx.request({
// // /work_key/obd/count/{mac}
// url: 'https://keytest.aik518.com/prod-api/work_key/obd/count/'+macAddress,
// method: 'GET', // 请求方法
// success: res => {
// //{"code":1,"data":{"oldPin":"0862","newPin":"0746","flag":true},"dict":null,"i18nMsg":"请求成功","msg":"请求成功"}
// // A3000AAA5501 0CC2FB 76
// let currentCount = res.data.data.count;
// console.log(res.data+"===当前烧录次数"+currentCount);
// let intCount = parseInt(currentCount);
// if(intCount>6){
// wx.showModal({
// title: '温馨提示',
// content: '当前设备已经烧录6次,不能继续烧录',
// cancelText:false,
// complete: (res) => {
// if (res.confirm) {
// }
// }
// })
// }else{
// this.sendCommonOrder(oder,true);
// }
// }
// })
}
,
addObdBurnCount:function(){
// const s = options.name;
let macAddress = getApp().globalData.deviceId;
let deviceType = getApp().globalData.currentDevice;
if(deviceType.includes('ios')){
macAddress = wx.getStorageSync(getApp().globalData.deviceId);
}
wx.request({
url: 'https://keytest.aik518.com/prod-api/work_key/obd/count/add',
method: 'POST', // 请求方法
data:{
mac: macAddress
},
// data: JSON.stringify({ // 手动序列化对象为 JSON 字符串
// mac: macAddress,
// language: 'zh'
// }),
header: {
'Content-Type': 'application/json' // 关键标识‌:ml-citation{ref="1,4" data="citationList"}
},
success: res => {
console.log(res);
// this.setData({
// obd_command:res.data.dict.obd_command,
// commandMap:res.data.records[0].commandMap,
// support:res.data.records[0].support,
// videoUrl:res.data.records[0].videoMiniUrl,
// codeList:res.data.records[0].type.codeList
// })
}
});
},
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);
}
});
});
}
});
}
})