var dataUtil = require("./jy-bluetooth-obu-DataUtil.js"), errorCode = require("./jy-bluetooth-obu-ErrorCode.js"), XXXDeviceId = "", XXXServiceId = "", XXXWriteId = "", XXXReadId = "", XXXSendBuffer = [], XXXSendIndex = 0, XXXResendCount = 3, sendCallback, stephex = "", frameLength = 0, deployCall, frameArray = [], frameCount = 0, connectCall, isInitResponse = !1, SERVICE_UUID_FEE7 = "0000FEE7-0000-1000-8000-00805F9B34FB", WRITE_UUID_FEC7 = "0000FEC7-0000-1000-8000-00805F9B34FB", READ_UUID_FEC8 = "0000FEC8-0000-1000-8000-00805F9B34FB", SHORT_SERVICE_UUID_FEE7 = "FEE7", SHORT_WRITE_UUID_FEC7 = "FEC7", SHORT_READ_UUID_FEC8 = "FEC8", SERVICE_UUID = SERVICE_UUID_FEE7, WRITE_UUID = WRITE_UUID_FEC7, READ_UUID = READ_UUID_FEC8, SHORT_SERVICE_UUID = SHORT_SERVICE_UUID_FEE7, SHORT_WRITE_UUID = SHORT_WRITE_UUID_FEC7, SHORT_READ_UUID = SHORT_READ_UUID_FEC8, foundDevices = [], COS_SUCCESS_CODE = "9000", isScanFinish, connectDevice, reConnectNum = 1, scanTimeoutId = null, mScanTimeOut = 5E3, isBLEConnectionStateChange = !1, connectTimeoutId = null, isBleConnected = !1, isSending = !1, sendDataTimeoutId; function isConnected() { return isBleConnected } function initData() { stephex = ""; XXXSendBuffer = []; XXXSendIndex = 0; frameArray = [] } function sendData(a, b) { stephex = ""; XXXSendBuffer = a; XXXSendIndex = 0; sendCallback = b; frameArray = []; startSendData(b) } function startSendData(a) { var b = XXXSendBuffer[XXXSendIndex], c = Array.prototype.map.call(new Uint8Array(b), function(b) { return ("00" + b.toString(16)).slice(-2) }).join(""); dataUtil.consoleLog("\u53d1\u9001\u6570\u636e\uff1a" + c); wx.writeBLECharacteristicValue({ deviceId: XXXDeviceId, serviceId: XXXServiceId, characteristicId: XXXWriteId, value: b, success: function(b) { XXXResendCount = 3; XXXSendIndex++; XXXSendIndex < XXXSendBuffer.length ? startSendData() : isInitResponse && ("function" == typeof sendCallback && sendCallback(errorCode.SUCCESS_CODE), isInitResponse = !1) }, fail: function() { dataUtil.consoleLog("\u53d1\u9001\u5931\u8d25\u4e86\uff1a"); "function" == typeof a && a(errorCode.FAILED_CODE) } }) } function openReceiveData(a) { wx.notifyBLECharacteristicValueChange({ deviceId: XXXDeviceId, serviceId: XXXServiceId, characteristicId: XXXReadId, state: !0, success: function(b) { "function" === typeof a && a(errorCode.SUCCESS_CODE) }, fail: function() { "function" === typeof a && a(errorCode.FAILED_CODE) } }); wx.onBLECharacteristicValueChange(function(b) { b = Array.prototype.map.call(new Uint8Array(b.value), function(b) { return ("00" + b.toString(16)).slice(-2) }).join(""); dataUtil.consoleLog("\u63a5\u6536\u6570\u636e\uff1a" + b); 0 == stephex.length && (frameLength = 2 * parseInt(b.substring(4, 8), 16)); stephex += b; if (stephex.length == frameLength) if (b = stephex.substring(8, 12), "2711" == b) authResponse(); else if ("2713" == b) initResponse(); else if ("2712" == b) { b = stephex.substring(16); if (256 <= b.length - 16) { var a = 256 * parseInt(b.substring(8, 10), 16) + (2 * parseInt(b.substring(6, 8), 16) - 256); b = b.substring(10, 10 + a) } else b = b.substring(8, 8 + 2 * parseInt(b.substring(6, 8), 16)); genvictDataResponse(b) } }) } function initResponse() { var a = dataUtil.makeInitResponse(); isInitResponse = !0; var b = {}; sendData(a, function(a, d) { clearTimeout(connectTimeoutId); 0 == a ? initDevice(function(a, c) { dataUtil.consoleLog("res:" + c); a == errorCode.SUCCESS_CODE ? (2 < c.length && 10 < parseInt(c.substring(0, 2), 16) ? ( dataUtil.consoleLog("\u540e\u88c5OBU"), dataUtil.setProtocolType("genvict"), dataUtil.setPackageLen(100)) : (dataUtil.consoleLog("\u524d\u88c5OBU"), dataUtil .setProtocolType("pre"), a = c.substring(2, 4), dataUtil.setPackageLen(parseInt( a, 16))), b.code = errorCode.SUCCESS_CODE, b.msg = "\u8fde\u63a5\u6210\u529f") : (b .code = errorCode.ERR_INIT_DEVICE_CODE, b.msg = "\u8fde\u63a5\u5931\u8d25"); "function" == typeof connectCall && connectCall(b) }) : (b.code = errorCode.ERR_WX_INIT_CODE, "function" == typeof connectCall && connectCall(b)) }) } function authResponse() { var a = dataUtil.makeAuthResponse(); sendData(a, function(b, a) { 0 != b && (b = {}, b.code = errorCode.ERR_WX_AUTH_CODE, "function" == typeof connectCall && connectCall( b)) }) } function genvictDataResponse(a) { stephex = ""; frameLength = 0; 0 == frameArray.length && (frameCount = parseInt(a.substring(4, 6), 16) - 128 + 1); frameArray.push(a); if (frameCount == frameArray.length) { a = ""; for (var b = 0; b < frameArray.length; b++) { var c = "" + frameArray[b]; a += c.substring(8, c.length - 2) } frameArray = []; frameCount = 0; 150 != parseInt(a.substring(0, 2), 16) && "function" == typeof sendCallback && sendCallback(errorCode .SUCCESS_CODE, a) } } function deployBle(a) { initData(); XXXReadId = XXXWriteId = ""; var b = {}; wx.getBLEDeviceServices({ deviceId: XXXDeviceId, success: function(c) { dataUtil.consoleLog("getBLEDeviceServices-success"); for (var d = 0; d < c.services.length; d++) { var e = c.services[d].uuid; dataUtil.consoleLog("serviceuuid\uff1a" + e); if (e === SERVICE_UUID || e === SHORT_SERVICE_UUID) { XXXServiceId = e; wx.getBLEDeviceCharacteristics({ deviceId: XXXDeviceId, serviceId: XXXServiceId, success: function(c) { for (var d = 0; d < c.characteristics.length; d++) { var e = c.characteristics[d].uuid; dataUtil.consoleLog("chauuid" + e); if (e === READ_UUID || e === SHORT_READ_UUID) XXXReadId = e; else if (e === WRITE_UUID || e === SHORT_WRITE_UUID) XXXWriteId = e } dataUtil.consoleLog("XXXReadId\uff1a" + XXXReadId); dataUtil.consoleLog("XXXWriteId" + XXXWriteId); 0 === XXXWriteId.length || 0 === XXXReadId.length ? (b.code = errorCode.ERR_CONNECTBLE_CODE, "function" === typeof a && a( b)) : openReceiveData(function(c) { dataUtil.consoleLog("openReceiveData"); "0" === c ? "normal" == dataUtil.protocolType() || "hlj_ff15" == dataUtil.protocolType() ? (b.code = errorCode.SUCCESS_CODE, "function" === typeof a && a(b)) : (dataUtil .consoleLog("start wx auth init"), connectCall = a) : (b.code = errorCode.ERR_CONNECTBLE_CODE, "function" === typeof a && a(b)) }) }, fail: function() { b.code = errorCode.ERR_DEPLOPBLE_CODE; "function" === typeof a && a(b) } }); return } } b.code = errorCode.ERR_DEPLOPBLE_CODE; "function" === typeof a && a(b) }, fail: function() { b.code = errorCode.ERR_DEPLOPBLE_CODE; "function" === typeof a && a(b) } }) } function scanBle(a) { foundDevices = []; isScanFinish = !1; var b = {}; clearTimeout(scanTimeoutId); scanTimeoutId = setTimeout(function() { isScanFinish || (stopScanBle(function(b) {}), b.code = errorCode.ERR_SCANBLE_TIMEOUT, b.err_msg = "\u626b\u63cf\u8d85\u65f6", "function" === typeof a && a(b)) }, mScanTimeOut); wx.startBluetoothDevicesDiscovery({ success: function(c) { wx.onBluetoothDeviceFound(function(c) { for (var d = 0; d < c.devices.length && !isInvalidBle(c.devices[d]) && !isSameBle(c .devices[d]); d++) dataUtil.consoleLog("name:" + c.devices[d].name + " address:" + c.devices[d].deviceId), foundDevices.push(c.devices[d]), b .code = errorCode.SUCCESS_CODE, b.devices = foundDevices, isScanFinish = !0, a( b) }) }, fail: function() { b.code = errorCode.ERR_SCANBLE_CODE; "function" === typeof a && a(b) } }) } function setScanTimeOut(a) { mScanTimeOut = a } function isInvalidBle(a) { return null == a || null == a.deviceId || 0 >= a.deviceId.length || null == a.name || 0 >= a.name.length ? !0 : !1 } function isSameBle(a) { var b = !1; foundDevices.forEach(function(c) { a.name === c.name && a.deviceId === c.deviceId && (b = !0) }); return b } function openBle(a) { var b = {}; wx.openBluetoothAdapter({ success: function(c) { dataUtil.consoleLog("openBle success"); b.code = errorCode.SUCCESS_CODE; "function" === typeof a && a(b) }, fail: function() { dataUtil.consoleLog("openBle fail"); b.code = errorCode.ERR_OPENBLE_CODE; "function" === typeof a && a(b) } }) } function stopScanBle(a) { var b = {}; isScanFinish = !0; wx.stopBluetoothDevicesDiscovery({ success: function(c) { dataUtil.consoleLog("\u505c\u6b62\u641c\u7d22\u84dd\u7259\u8bbe\u5907"); b.code = errorCode.SUCCESS_CODE; "function" === typeof a && a(b) }, fail: function() { dataUtil.consoleLog("\u505c\u6b62\u641c\u7d22\u84dd\u7259\u8bbe\u5907\u5931\u8d25"); b.code = errorCode.ERR_STOPSCANBLE_CODE; "function" === typeof a && a(b) } }) } function connectBle(a, b) { var c = {}; clearTimeout(connectTimeoutId); connectTimeoutId = setTimeout(function() { disconnectBle(function() {}); c.code = errorCode.ERR_CONNECT_TIMEOUT; c.err_msg = "\u8fde\u63a5\u8d85\u65f6"; "function" === typeof b && b(c) }, 1E4); startOnBleConnectionStateChange(); connectDevice = a; XXXDeviceId = a.deviceId; dataUtil.consoleLog("\u5f00\u59cb\u8fde\u63a5"); reConnectNum = 1; createBleConnection(function(a) { a.code == errorCode.SUCCESS_CODE ? (isBleConnected = !0, "function" === typeof b && b(a)) : 0 < reConnectNum ? (reConnectNum--, disconnectBle(function(b) {}), dataUtil.consoleLog("\u5f00\u59cb\u91cd\u8fde"), createBleConnection(b)) : (clearTimeout(connectTimeoutId), c.code = errorCode .ERR_CONNECTBLE_CODE, "function" === typeof b && b(c)) }) } function startOnBleConnectionStateChange() { if (!isBLEConnectionStateChange) wx.onBLEConnectionStateChange(function(a) { isBLEConnectionStateChange = !0; a.connected ? dataUtil.consoleLog("onBLEConnectionStateChange-connected") : (dataUtil.consoleLog( "onBLEConnectionStateChange-disconnect"), isBleConnected = !1, wx.closeBLEConnection({ deviceId: XXXDeviceId })) }) } function createBleConnection(a) { var b = {}; wx.createBLEConnection({ deviceId: XXXDeviceId, success: function(b) { deployBle(a) }, fail: function(c) { b.code = errorCode.ERR_CONNECTBLE_CODE; "function" === typeof a && a(b) } }) } function closeBle(a) { var b = {}; wx.closeBluetoothAdapter({ success: function(c) { dataUtil.consoleLog("\u5173\u95ed\u84dd\u7259\u9002\u914d\u5668\u6210\u529f"); b.code = errorCode.SUCCESS_CODE; "function" === typeof a && a(b) }, fail: function() { dataUtil.consoleLog("\u5173\u95ed\u84dd\u7259\u9002\u914d\u5668\u5931\u8d25"); b.code = errorCode.FAILED_CODE; "function" === typeof a && a(b) } }) } function disconnectBle(a) { wx.closeBLEConnection({ XXXDeviceId: XXXDeviceId, success: function(b) { console.log("closeBLEConnection:" + b) } }) } function cosChannel(a, b, c) { sendDataTimeout(c); sendData(dataUtil.makeCosSendData(a, b), function(b, a) { clearTimeout(sendDataTimeoutId); if ("0" === b) if (dataUtil.consoleLog("initDevice-res\uff1a" + a), b = a.substring(2, 4), "00" == b) { b = a.substring(10, a.length); b = tlvUnpackData(b); for (var d = 0; d < b.length; d++) { var e = b[d].substring(b[d].length - 4, b[d].length); dataUtil.consoleLog("cosCode:" + e); if (e != COS_SUCCESS_CODE) { "function" === typeof c && c(e, a); return } } "function" === typeof c && c(errorCode.SUCCESS_CODE, b) } else "function" === typeof c && c(b, a); else "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, a) }) } function initDevice(a) { sendDataTimeout(a); sendData(dataUtil.initDeviceSendData(), function(b, c) { clearTimeout(sendDataTimeoutId); if ("0" === b) { dataUtil.consoleLog("initDevice-res\uff1a" + c); var d = c.substring(2, 4); "00" == d ? (c = c.substring(6, c.length), "function" === typeof a && a(b, c)) : "function" === typeof a && a(d, c) } else "function" === typeof a && a(errorCode.ERR_CMD_EXECUTE_CODE, c) }) } function cardChannel(a, b) { console.log("a,b", a, b) sendDataTimeout(b); sendData(dataUtil.makePiccChannelSendData("00", a), function(a, d) { clearTimeout(sendDataTimeoutId); if ("0" === a) if (dataUtil.consoleLog("cardChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a) { var c = d.substring(10, d.length); c = tlvUnpackData(c); for (a = 0; a < c.length; a++) { var f = c[a].substring(c[a].length - 4, c[a].length); dataUtil.consoleLog("cosCode:" + f); if (f != COS_SUCCESS_CODE) { "function" === typeof b && b(f, d); return } } "function" === typeof b && b(errorCode.SUCCESS_CODE, c) } else "function" === typeof b && b(a, c); else "function" === typeof b && b(errorCode.ERR_CMD_EXECUTE_CODE, d) }) } function deviceChannel(a, b) { sendDataTimeout(b); sendData(dataUtil.makeDeviceChannelSendData(a), function(a, d) { clearTimeout(sendDataTimeoutId); "0" === a ? (dataUtil.consoleLog("deviceChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a ? (d = d.substring(6, d.length - 6), "function" === typeof b && b(errorCode.SUCCESS_CODE, d)) : "function" === typeof b && b(a, d)) : "function" === typeof b && b(errorCode .ERR_CMD_EXECUTE_CODE, d) }) } function authenChannel(a, b, c) { sendDataTimeout(c); sendData(dataUtil.makeAuthenChannelSendData(a, b), function(a, b) { clearTimeout(sendDataTimeoutId); "0" === a ? (dataUtil.consoleLog("authenChannel-res\uff1a" + b), "function" === typeof c && c(a, b)) : "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, b) }) } function cardReset(a) { sendDataTimeout(a); sendData(dataUtil.makePiccResetSendData(), function(b, c) { clearTimeout(sendDataTimeoutId); "0" === b ? (dataUtil.consoleLog("cardReset-res\uff1a" + c), b = c.substring(2, 4), "00" == b ? (c = c .substring(6, c.length - 6), "function" === typeof a && a(errorCode.SUCCESS_CODE, c)) : "function" === typeof a && a(b, c)) : "function" === typeof a && a(errorCode .ERR_CMD_EXECUTE_CODE, c) }) } function esamReset(a) { sendDataTimeout(a); sendData(dataUtil.makeEsamResetSendData(), function(b, c) { clearTimeout(sendDataTimeoutId); "0" === b ? (dataUtil.consoleLog("esamReset-res\uff1a" + c), b = c.substring(2, 4), "00" == b ? (c = c .substring(6, c.length - 6), "function" === typeof a && a(errorCode.SUCCESS_CODE, c)) : "function" === typeof a && a(b, c)) : "function" === typeof a && a(errorCode .ERR_CMD_EXECUTE_CODE, c) }) } function esamChannel(a, b) { sendDataTimeout(b); sendData(dataUtil.makeEsamChannelSendData("00", a), function(a, d) { clearTimeout(sendDataTimeoutId); if ("0" === a) if (dataUtil.consoleLog("esamChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a) { a = d.substring(10, d.length); a = tlvUnpackData(a); for (var c = 0; c < a.length; c++) { var f = a[c].substring(a[c].length - 4, a[c].length); dataUtil.consoleLog("cosCode:" + f); if (f != COS_SUCCESS_CODE) { "function" === typeof b && b(f, d); return } } "function" === typeof b && b(errorCode.SUCCESS_CODE, a) } else "function" === typeof b && b(a, d); else "function" === typeof b && b(errorCode.ERR_CMD_EXECUTE_CODE, d) }) } function deviceAuthenChannel(a, b, c) { sendDataTimeout(c); sendData(dataUtil.makeDeviceAuthenSendData(a, b), function(a, b) { clearTimeout(sendDataTimeoutId); "0" === a ? (dataUtil.consoleLog("deviceAuthenChannel-res\uff1a" + b), a = b.substring(2, 4), "00" == a ? (b = b.substring(8, b.length), "function" === typeof c && c(errorCode.SUCCESS_CODE, b)) : "function" === typeof c && c(a, b)) : "function" === typeof c && c(errorCode .ERR_CMD_EXECUTE_CODE, b) }) } function selfDeviceChannel(a, b) { sendDataTimeout(b); sendData(dataUtil.makeSelfDeviceChannelSendData(a), function(a, d) { clearTimeout(sendDataTimeoutId); "0" === a ? (a = d.substring(2, 4), "00" == a ? (d = d.substring(8, d.length), dataUtil.consoleLog( "selfChannel-data\uff1a" + d), "function" === typeof b && b(errorCode.SUCCESS_CODE, d)) : "function" === typeof b && b(a, d)) : "function" === typeof b && b(errorCode .ERR_CMD_EXECUTE_CODE, d) }) } function updateChannel(a, b, c) { sendDataTimeout(c); sendData(dataUtil.makeUpdateChannelSendData(a, b), function(a, b) { clearTimeout(sendDataTimeoutId); if ("0" === a) { var d = b.substring(2, 4); "00" == d ? (b = b.substring(8, b.length), "function" === typeof c && c(a, b)) : "function" === typeof c && c(d, b) } else "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, b) }) } function sendDataTimeout(a) { sendDataTimeoutId = setTimeout(function() { dataUtil.consoleLog("\u6570\u636e\u54cd\u5e94\u8d85\u65f6\uff0c\u8fd4\u56de\u9519\u8bef"); "function" === typeof a && a(errorCode.ERR_RECVDATA_TIMEOUT, "\u6570\u636e\u54cd\u5e94\u8d85\u65f6") }, 5E3) } function tlvUnpackData(a) { var b = parseInt(a.substring(2, 4), 16), c = []; 128 >= b ? (b = 2 * parseInt(a.substring(2, 4), 16), a = a.substring(4, a.length)) : 129 == b ? (b = 2 * parseInt(a .substring(4, 6), 16), a = a.substring(6, a.length)) : (b = 2 * parseInt(a.substring(4, 8), 16), a = a .substring(8, a.length)); for (var d = 0, e = 0, f; 0 < b;) f = 2 * parseInt(a.substring(2 + e, 4 + e), 16), c[d] = a.substring(4 + e, f + 4 + e), e = e + 4 + f, d++, b = b - 4 - f; return c } function encryptRandom(a) { a = a.match(/.{1,2}/g).map(function(a) { return parseInt(a, 16) }); for (var b = "67656E7669637430".match(/.{1,2}/g).map(function(a) { return parseInt(a, 16) }), c = new Uint8Array(a.length), d = 0; d < a.length; d++) c[d] = a[d] ^ b[d]; return byteArrayToHexStr(c) } function stringtoHex(a) { return dataUtil.stringtoHex(a) } function hexToBytes(a) { return dataUtil.hexToBytes(a) } function hexToString(a) { return dataUtil.hexToString(a) } function byteArrayToHexStr(a) { return dataUtil.byteArrayToHexStr(a) } function fromCharCode(a) { return dataUtil.fromCharCode(a) } function numberToHexString(a, b, c) { return dataUtil.numberToHexString(a, b, c) } function calcPercent(a, b) { a = (a / b).toFixed(2); a = 100 * parseFloat(a); return parseInt(a) } function consoleLog(a) { dataUtil.consoleLog(a) } module.exports = { deployBle: deployBle, openBle: openBle, closeBle: closeBle, setScanTimeOut: setScanTimeOut, scanBle: scanBle, stopScanBle: stopScanBle, connectBle: connectBle, disconnectBle: disconnectBle, isConnected: isConnected, cosChannel: cosChannel, initDevice: initDevice, cardChannel: cardChannel, deviceChannel: deviceChannel, authenChannel: authenChannel, cardReset: cardReset, esamReset: esamReset, esamChannel: esamChannel, deviceAuthenChannel: deviceAuthenChannel, selfDeviceChannel: selfDeviceChannel, updateChannel: updateChannel, encryptRandom: encryptRandom, stringtoHex: stringtoHex, fromCharCode: fromCharCode, numberToHexString: numberToHexString, hexToBytes: hexToBytes, hexToString: hexToString, byteArrayToHexStr: byteArrayToHexStr, calcPercent: calcPercent, consoleLog: consoleLog };