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

554 lines
16 KiB

import {
  onBLEConChange,
  addConnectionListener,
  handleBleData,
  writeBLECharacteristicValue,
  onBLECharacteristicValueChange,
  stringToHex,
  intToHexString
} from '../../utils/bleUtil.js'
Page({
data: {
list:[],
obd_command:[],
commandMap:[],
support:"",
codeList:[],
videoUrl:""
},
// 在生命周期函数中加载数据
onLoad(options) {
//   onBLECharacteristicValueChange((result) => {
//       this.handleResult(result)
//     })
 
wx.showToast({
title: `点击了子项 ${options.name}`,
icon: 'none'
});
// wx.showLoading({title:"等待页面初始化"})
const s = options.name;
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);
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].videoUrl,
codeList:res.data.records[0].type.codeList
})
}
});
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) {
}
}
})
}
})
},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) => {
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);
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);
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 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();
}
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);
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"){
}
}
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"){
let type = secondOrder.substring(18,20);
let numberLenth = parseInt(number);
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"){
}
}
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"){
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 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,
})
}
},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){
const oder = "aa"+e.currentTarget.dataset.name;
// let oo = "aa60000001";
// let oo = "aa53000009";
this.sendCommonOrder(oder,true);
},
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);
}
});
});
}
});
}
})