"use strict"; var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) { return typeof e } : function(e) { return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e }, _wjUtils = require("./wjUtils.js"), _wjUtils2 = _interopRequireDefault(_wjUtils), _wjDataDecode = require("./wjDataDecode"), _wjDataDecode2 = _interopRequireDefault(_wjDataDecode); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } var reality_indicateServiceId, reality_notifyServiceId, reality_writeServiceId, reality_services, ListenFlag = void 0, DataListenerCallBack = void 0, connectedDeviceId = void 0, timeID = void 0, TAG_FUNCTION = "function", sendCallback = void 0, TimerID = null, handshake = !1, services = "0000FEE7-0000-1000-8000-00805F9B34FB", retdata = ""; function reallyScanConnect(n) { var o = {}, s = (_wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"), []); wx.openBluetoothAdapter({ success: function(e) { wx.startBluetoothDevicesDiscovery({ services: [], success: function(e) { wx.onBluetoothDeviceFound(function(e) { for (var t = 0; t < e.devices.length; t++) { for (var i = !1, c = 0; c < s.length; c++) if (e.devices[t].deviceId == s[c].deviceId) { i = !0; break } if (_wjUtils2.default.showLog("connectedDeviceName", e .devices[t].name), 0 == i) { s.push(e.devices[t]); var a = e.devices[t].name.toUpperCase(); if (a.includes("WJ") || a.includes("WANJI") || a .includes("ETC") || a.includes("3601") || a .includes("YTK") || a.includes("HB") || a.includes( "CQ") || a.includes("YUN") || a.includes( "SH") || a.includes("HCZJ") || a.includes("NM") || a .includes("SC") || a.includes("LJ") || a.includes( "HN")) if ("break" === function() { connectedDeviceId = e.devices[t].deviceId, _wjUtils2.default.showLog( "connectedDeviceId:" + connectedDeviceId); var i = {}; return i.device_name = a, i.device_id = connectedDeviceId, wx .stopBluetoothDevicesDiscovery({ success: function(e) { null != timeID && ( clearTimeout( timeID), timeID = null), _wjUtils2.default .showLog( "停止扫描,开始连接"), reallyConnect(i, function(e) { (void 0 === n ? "undefined" : _typeof( n) ) == TAG_FUNCTION && n(e) }) }, fail: function(e) { _wjUtils2.default .showError( "stopBluetoothDevicesDiscovery fail", e), o .serviceCode = 1103, o.serviceInfo = "stopBluetoothDevicesDiscovery fail", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } }), "break" }()) break } } }) }, fail: function(e) { _wjUtils2.default.showError("startBluetoothDevicesDiscovery fail", e), o .serviceCode = 1102, o.serviceInfo = "startBluetoothDevicesDiscovery fail", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } }) }, fail: function(e) { _wjUtils2.default.showError("openBluetoothAdapter fail", e), o.serviceCode = 1101, o .serviceInfo = "openBluetoothAdapter fail", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } }), timeID = setTimeout(function() { wx.closeBluetoothAdapter(), wx.stopBluetoothDevicesDiscovery(), _wjUtils2.default.showLog( "scan timeout fail"), o.serviceCode = -13, o.serviceInfo = "scan timeout fail", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) }, 5e3) } function reallyConnect(e, i) { _wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"); var t = {}, c = e.device_name.toUpperCase(); _wjUtils2.default.showLog("name", c), c.includes("WJ") || c.includes("WANJI") || c.includes("ETC") || c.includes( "3601") || c.includes("YTK") || c.includes("HB") || c.includes("CQ") || c.includes("YUN") || c.includes( "SH") || c.includes("HCZJ") || c.includes("NM") || c.includes("SC") || c.includes("LJ") || c.includes( "HN") ? (_onBLEConnectionStateChange(), connectedDeviceId = e.device_id, wx.createBLEConnection({ deviceId: connectedDeviceId, success: function(e) { _wjUtils2.default.showLog("已连接,开始使能服务:" + connectedDeviceId), _enableService(function(e) { 0 == e.serviceCode ? setTimeout(function() { _wjUtils2.default.showLog("已连接,并使能成功"), (t = e).serviceInfo = "已连接,并使能成功!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) }, 1e3) : ((t = e).serviceInfo = "连接成功,但服务使能失败!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t)) }) }, fail: function(e) { _wjUtils2.default.showError("createBLEConnection fail", e), t.serviceCode = 1105, t .serviceInfo = "createBLEConnection fail", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) } }), timeID = setTimeout(function() { wx.closeBluetoothAdapter(), _wjUtils2.default.showError("conn timeout fail"), t.serviceCode = -13, t.serviceInfo = "conn timeout fail!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) }, 5e3)) : (t.serviceCode = 1104, t.serviceInfo = "device name error", (void 0 === i ? "undefined" : _typeof( i)) == TAG_FUNCTION && i(t)) } function _enableService(n) { _wjUtils2.default.showLog("/***********Runing :: Do _enableService() begin *************/"); var o = {}; wx.getBLEDeviceServices({ deviceId: connectedDeviceId, success: function(e) { for (var i = 0; i < e.services.length; i++) { var t = e.services[i].uuid; _wjUtils2.default.showLog("serviceuuid:" + t), t == services && (reality_services = t, wx .getBLEDeviceCharacteristics({ deviceId: connectedDeviceId, serviceId: reality_services, success: function(e) { var i = 0; reality_indicateServiceId = ""; for (var t = 0; t < e.characteristics.length; t++) { var c = e.characteristics[t].uuid, a = e.characteristics[t].properties; _wjUtils2.default.showLog("chauuid:" + c), a.indicate ? ( reality_indicateServiceId = c, i++) : a.notify ? ( reality_notifyServiceId = c, i++) : a.write && ( reality_writeServiceId = c, i++) } "" == reality_indicateServiceId && (reality_indicateServiceId = reality_notifyServiceId), i < 2 ? (o.serviceCode = 1110, o .serviceInfo = "getBLEDeviceCharacteristics temp<2!", ( void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o)) : wx .notifyBLECharacteristicValueChange({ deviceId: connectedDeviceId, serviceId: reality_services, characteristicId: reality_indicateServiceId, state: !0, success: function(e) { _SetDataListenerCallBack(!0, datacallback), _onBLECharacteristicValueChange(), null != timeID && (clearTimeout(timeID), timeID = null); var i = setInterval(function() { handshake && (console.log("握手认证成功"), handshake = !1, o .serviceCode = 0, o .serviceInfo = "enable success!", clearInterval(i), console .log("握手成功之后", o .serviceCode), ( void 0 === n ? "undefined" : _typeof(n) ) == TAG_FUNCTION) && n( o) }, 5) }, fail: function() { o.serviceCode = 1109, o.serviceInfo = "notifyBLECharacteristicValueChange fail!", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } }) }, fail: function() { o.serviceCode = 1108, o.serviceInfo = "getBLEDeviceCharacteristics fail!", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } })) } }, fail: function() { o.serviceCode = 1107, o.serviceInfo = "getBLEDeviceServices fail!", (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o) } }) } function _onBLEConnectionStateChange() { wx.onBLEConnectionStateChange(function(e) { _wjUtils2.default.showLog("device " + e.deviceId + " state has changed, connected: " + e.connected), 0 == e.connected && (wx.closeBLEConnection({ deviceId: e.deviceId, success: function(e) { _wjUtils2.default.showLog("closeBLEConnection:", e) } }), wx.closeBluetoothAdapter({ success: function(e) { _wjUtils2.default.showLog("closeBluetoothAdapter", e) } })) }) } function reallyDisConnect(i) { var t = {}; null != connectedDeviceId && wx.closeBLEConnection({ deviceId: connectedDeviceId, success: function(e) { _wjUtils2.default.showLog("closeBLEConnection:", e) } }), wx.closeBluetoothAdapter({ success: function(e) { _wjUtils2.default.showLog("closeBluetoothAdapter", e), t.serviceCode = 0, t.serviceInfo = "断开蓝牙成功!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) }, fail: function(e) { console.log(e), t.serviceCode = -1, t.serviceInfo = e.errMsg, (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) } }) } function _writeBLECharacteristicValue(e, i) { var t = {}; wx.writeBLECharacteristicValue({ deviceId: connectedDeviceId, serviceId: reality_services, characteristicId: reality_writeServiceId, value: e, success: function(e) { _wjUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg), t.serviceCode = 0, t .serviceInfo = e.errMsg, (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t) } }) } function _onBLECharacteristicValueChange() { wx.onBLECharacteristicValueChange(function(e) { _wjUtils2.default.showLog(e.deviceId + " 'characteristic has changed"), console.log("rev:" + e), e .characteristicId === reality_indicateServiceId && 1 == ListenFlag && (void 0 === DataListenerCallBack ? "undefined" : _typeof(DataListenerCallBack)) == TAG_FUNCTION && DataListenerCallBack(e.value) }) } function _SetDataListenerCallBack(e, i) { 1 == e ? (ListenFlag = !0, DataListenerCallBack = i) : 0 == e && (ListenFlag = !1) } function datacallback(e) { _SetDataListenerCallBack(!0, datacallback), _ReceiveData(e) } function _ReceiveData(e) { var i, t = {}, c = _wjUtils2.default.getProtocolType(); 0 == c ? i = _wjDataDecode2.default.decode_GB(e) : 1 == c && (i = _wjDataDecode2.default.decode_WX(e)), 0 === i .serviceCode && (c = i.serviceData.strData, i.serviceData.strData.startsWith( "fe01001a271100010a0018808004200128023a06") && 52 === i.serviceData.strData.length || "fe010013271300020a001201411a0400000000" == i.serviceData.strData ? (i.serviceData.strData.startsWith( "fe01001a271100010a0018808004200128023a06") && 52 === i.serviceData.strData.length && (handshake = ! 0), null != TimerID && (clearTimeout(TimerID), TimerID = null), _SetDataListenerCallBack(!1), t .serviceCode = 0, t.serviceInfo = "数据接收成功", t.serviceData = c, console.log("result_DataInteract", t), retdata = t.serviceData) : null != (e = PackageDecode(c)) ? (_wjUtils2.default.showLog(TimerID, "接收数据成功,取消时钟"), null != TimerID && (clearTimeout(TimerID), TimerID = null), _SetDataListenerCallBack(!1), t.serviceCode = 0, t.serviceInfo = "数据接收成功", t.serviceData = {}, t .serviceData.dataBuff = e) : (_wjUtils2.default.showError(TimerID, "数据解析失败,取消时钟"), null != TimerID && ( clearTimeout(TimerID), TimerID = null), wjService._SetDataListenerCallBack(!1), t.serviceCode = -1, t.serviceInfo = "数据解析失败"), (void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) == TAG_FUNCTION) && sendCallback(t) } function PackageDecode(e) { return _wjUtils2.default.showLog("WJPackageDecode:", e), null } function _StartSendData(o, e) { var s = {}, l = 0, r = 0; null != TimerID && (clearTimeout(TimerID), TimerID = null), _wjDataDecode2.default.init(), "fe0100164e2300030a06080012024f4b100018002000" == o ? _SetDataListenerCallBack(!1) : (TimerID = setTimeout( pro_timeout, 1e4), _wjUtils2.default.showLog(TimerID, "开启时钟"), _SetDataListenerCallBack(!0, datacallback)), sendCallback = e, function i() { var e = ""; 40 * r >= o[l].length && (l++, r = 0); console.log("currentsegment", r, "currentPackage", l, o); l < o.length && (e = o[l].length >= 40 * (r + 1) ? o[l].substring(40 * r, 40 * (r + 1)) : o[l].substring( 40 * r, o[l].length)); console.log("currentsegment", r, "currentPackage", l, o); if ("" == e) _wjUtils2.default.showLog("数据发送完毕!"), "fe0100164e2300030a06080012024f4b100018002000" == o[s .serviceCode = 0] && (void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) == TAG_FUNCTION && sendCallback(s); else { for (var t = _wjUtils2.default.hexStr2byteArray(e), c = new ArrayBuffer(t.byteLength), a = new DataView( c), n = 0; n < t.byteLength; n++) a.setUint8(n, t[n]); _wjUtils2.default.showLog("发送数据:", e), _writeBLECharacteristicValue(c, function(e) { 0 == e.serviceCode ? (r++, i()) : (_wjUtils2.default.showError(TimerID, "数据发送失败,取消时钟"), null != TimerID && (clearTimeout(TimerID), TimerID = null), _SetDataListenerCallBack(!1), s.serviceCode = -2, s.serviceInfo = "数据发送失败", ( void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) == TAG_FUNCTION && sendCallback(s)) }) } }() } function pro_timeout() { var e = {}; _wjUtils2.default.showError(TimerID, "数据接收超时,取消时钟"), null != TimerID && (clearTimeout(TimerID), TimerID = null), _SetDataListenerCallBack(!1), e.serviceCode = -3, e.serviceInfo = "数据接收超时", (void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) == TAG_FUNCTION && sendCallback(e) } module.exports = { reallyConnect: reallyConnect, reallyScanConnect: reallyScanConnect, reallyDisConnect: reallyDisConnect, _writeBLECharacteristicValue: _writeBLECharacteristicValue, _SetDataListenerCallBack: _SetDataListenerCallBack, _enableService: _enableService };