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.

wjService.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. "use strict";
  2. var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
  3. return typeof e
  4. } : function(e) {
  5. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" :
  6. typeof e
  7. },
  8. _wjUtils = require("./wjUtils.js"),
  9. _wjUtils2 = _interopRequireDefault(_wjUtils),
  10. _wjDataDecode = require("./wjDataDecode"),
  11. _wjDataDecode2 = _interopRequireDefault(_wjDataDecode);
  12. function _interopRequireDefault(e) {
  13. return e && e.__esModule ? e : {
  14. default: e
  15. }
  16. }
  17. var reality_indicateServiceId, reality_notifyServiceId, reality_writeServiceId, reality_services, ListenFlag = void 0,
  18. DataListenerCallBack = void 0,
  19. connectedDeviceId = void 0,
  20. timeID = void 0,
  21. TAG_FUNCTION = "function",
  22. sendCallback = void 0,
  23. TimerID = null,
  24. handshake = !1,
  25. services = "0000FEE7-0000-1000-8000-00805F9B34FB",
  26. retdata = "";
  27. function reallyScanConnect(n) {
  28. var o = {},
  29. s = (_wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"), []);
  30. wx.openBluetoothAdapter({
  31. success: function(e) {
  32. wx.startBluetoothDevicesDiscovery({
  33. services: [],
  34. success: function(e) {
  35. wx.onBluetoothDeviceFound(function(e) {
  36. for (var t = 0; t < e.devices.length; t++) {
  37. for (var i = !1, c = 0; c < s.length; c++)
  38. if (e.devices[t].deviceId == s[c].deviceId) {
  39. i = !0;
  40. break
  41. } if (_wjUtils2.default.showLog("connectedDeviceName", e
  42. .devices[t].name), 0 == i) {
  43. s.push(e.devices[t]);
  44. var a = e.devices[t].name.toUpperCase();
  45. if (a.includes("WJ") || a.includes("WANJI") || a
  46. .includes("ETC") || a.includes("3601") || a
  47. .includes("YTK") || a.includes("HB") || a.includes(
  48. "CQ") || a.includes("YUN") || a.includes(
  49. "SH") || a.includes("HCZJ") || a.includes("NM") || a
  50. .includes("SC") || a.includes("LJ") || a.includes(
  51. "HN"))
  52. if ("break" === function() {
  53. connectedDeviceId = e.devices[t].deviceId,
  54. _wjUtils2.default.showLog(
  55. "connectedDeviceId:" +
  56. connectedDeviceId);
  57. var i = {};
  58. return i.device_name = a, i.device_id =
  59. connectedDeviceId, wx
  60. .stopBluetoothDevicesDiscovery({
  61. success: function(e) {
  62. null != timeID && (
  63. clearTimeout(
  64. timeID),
  65. timeID = null),
  66. _wjUtils2.default
  67. .showLog(
  68. "停止扫描,开始连接"),
  69. reallyConnect(i,
  70. function(e) {
  71. (void 0 ===
  72. n ?
  73. "undefined" :
  74. _typeof(
  75. n)
  76. ) ==
  77. TAG_FUNCTION
  78. && n(e)
  79. })
  80. },
  81. fail: function(e) {
  82. _wjUtils2.default
  83. .showError(
  84. "stopBluetoothDevicesDiscovery fail",
  85. e), o
  86. .serviceCode = 1103,
  87. o.serviceInfo =
  88. "stopBluetoothDevicesDiscovery fail",
  89. (void 0 === n ?
  90. "undefined" :
  91. _typeof(n)) ==
  92. TAG_FUNCTION && n(o)
  93. }
  94. }), "break"
  95. }()) break
  96. }
  97. }
  98. })
  99. },
  100. fail: function(e) {
  101. _wjUtils2.default.showError("startBluetoothDevicesDiscovery fail", e), o
  102. .serviceCode = 1102, o.serviceInfo =
  103. "startBluetoothDevicesDiscovery fail", (void 0 === n ? "undefined" :
  104. _typeof(n)) == TAG_FUNCTION && n(o)
  105. }
  106. })
  107. },
  108. fail: function(e) {
  109. _wjUtils2.default.showError("openBluetoothAdapter fail", e), o.serviceCode = 1101, o
  110. .serviceInfo = "openBluetoothAdapter fail", (void 0 === n ? "undefined" : _typeof(n)) ==
  111. TAG_FUNCTION && n(o)
  112. }
  113. }), timeID = setTimeout(function() {
  114. wx.closeBluetoothAdapter(), wx.stopBluetoothDevicesDiscovery(), _wjUtils2.default.showLog(
  115. "scan timeout fail"), o.serviceCode = -13, o.serviceInfo = "scan timeout fail", (void 0 === n ?
  116. "undefined" : _typeof(n)) == TAG_FUNCTION && n(o)
  117. }, 5e3)
  118. }
  119. function reallyConnect(e, i) {
  120. _wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/");
  121. var t = {},
  122. c = e.device_name.toUpperCase();
  123. _wjUtils2.default.showLog("name", c), c.includes("WJ") || c.includes("WANJI") || c.includes("ETC") || c.includes(
  124. "3601") || c.includes("YTK") || c.includes("HB") || c.includes("CQ") || c.includes("YUN") || c.includes(
  125. "SH") || c.includes("HCZJ") || c.includes("NM") || c.includes("SC") || c.includes("LJ") || c.includes(
  126. "HN") ? (_onBLEConnectionStateChange(), connectedDeviceId = e.device_id, wx.createBLEConnection({
  127. deviceId: connectedDeviceId,
  128. success: function(e) {
  129. _wjUtils2.default.showLog("已连接,开始使能服务:" + connectedDeviceId), _enableService(function(e) {
  130. 0 == e.serviceCode ? setTimeout(function() {
  131. _wjUtils2.default.showLog("已连接,并使能成功"), (t = e).serviceInfo =
  132. "已连接,并使能成功!", (void 0 === i ? "undefined" : _typeof(i)) ==
  133. TAG_FUNCTION && i(t)
  134. }, 1e3) : ((t = e).serviceInfo = "连接成功,但服务使能失败!", (void 0 === i ?
  135. "undefined" : _typeof(i)) == TAG_FUNCTION && i(t))
  136. })
  137. },
  138. fail: function(e) {
  139. _wjUtils2.default.showError("createBLEConnection fail", e), t.serviceCode = 1105, t
  140. .serviceInfo = "createBLEConnection fail", (void 0 === i ? "undefined" : _typeof(i)) ==
  141. TAG_FUNCTION && i(t)
  142. }
  143. }), timeID = setTimeout(function() {
  144. wx.closeBluetoothAdapter(), _wjUtils2.default.showError("conn timeout fail"), t.serviceCode = -13,
  145. t.serviceInfo = "conn timeout fail!", (void 0 === i ? "undefined" : _typeof(i)) ==
  146. TAG_FUNCTION && i(t)
  147. }, 5e3)) : (t.serviceCode = 1104, t.serviceInfo = "device name error", (void 0 === i ? "undefined" : _typeof(
  148. i)) == TAG_FUNCTION && i(t))
  149. }
  150. function _enableService(n) {
  151. _wjUtils2.default.showLog("/***********Runing :: Do _enableService() begin *************/");
  152. var o = {};
  153. wx.getBLEDeviceServices({
  154. deviceId: connectedDeviceId,
  155. success: function(e) {
  156. for (var i = 0; i < e.services.length; i++) {
  157. var t = e.services[i].uuid;
  158. _wjUtils2.default.showLog("serviceuuid:" + t), t == services && (reality_services = t, wx
  159. .getBLEDeviceCharacteristics({
  160. deviceId: connectedDeviceId,
  161. serviceId: reality_services,
  162. success: function(e) {
  163. var i = 0;
  164. reality_indicateServiceId = "";
  165. for (var t = 0; t < e.characteristics.length; t++) {
  166. var c = e.characteristics[t].uuid,
  167. a = e.characteristics[t].properties;
  168. _wjUtils2.default.showLog("chauuid:" + c), a.indicate ? (
  169. reality_indicateServiceId = c, i++) : a.notify ? (
  170. reality_notifyServiceId = c, i++) : a.write && (
  171. reality_writeServiceId = c, i++)
  172. }
  173. "" == reality_indicateServiceId && (reality_indicateServiceId =
  174. reality_notifyServiceId), i < 2 ? (o.serviceCode = 1110, o
  175. .serviceInfo = "getBLEDeviceCharacteristics temp<2!", (
  176. void 0 === n ? "undefined" : _typeof(n)) ==
  177. TAG_FUNCTION && n(o)) : wx
  178. .notifyBLECharacteristicValueChange({
  179. deviceId: connectedDeviceId,
  180. serviceId: reality_services,
  181. characteristicId: reality_indicateServiceId,
  182. state: !0,
  183. success: function(e) {
  184. _SetDataListenerCallBack(!0, datacallback),
  185. _onBLECharacteristicValueChange(), null !=
  186. timeID && (clearTimeout(timeID), timeID =
  187. null);
  188. var i = setInterval(function() {
  189. handshake && (console.log("握手认证成功"),
  190. handshake = !1, o
  191. .serviceCode = 0, o
  192. .serviceInfo =
  193. "enable success!",
  194. clearInterval(i), console
  195. .log("握手成功之后", o
  196. .serviceCode), (
  197. void 0 === n ?
  198. "undefined" : _typeof(n)
  199. ) == TAG_FUNCTION) && n(
  200. o)
  201. }, 5)
  202. },
  203. fail: function() {
  204. o.serviceCode = 1109, o.serviceInfo =
  205. "notifyBLECharacteristicValueChange fail!",
  206. (void 0 === n ? "undefined" : _typeof(n)) ==
  207. TAG_FUNCTION && n(o)
  208. }
  209. })
  210. },
  211. fail: function() {
  212. o.serviceCode = 1108, o.serviceInfo =
  213. "getBLEDeviceCharacteristics fail!", (void 0 === n ?
  214. "undefined" : _typeof(n)) == TAG_FUNCTION && n(o)
  215. }
  216. }))
  217. }
  218. },
  219. fail: function() {
  220. o.serviceCode = 1107, o.serviceInfo = "getBLEDeviceServices fail!", (void 0 === n ?
  221. "undefined" : _typeof(n)) == TAG_FUNCTION && n(o)
  222. }
  223. })
  224. }
  225. function _onBLEConnectionStateChange() {
  226. wx.onBLEConnectionStateChange(function(e) {
  227. _wjUtils2.default.showLog("device " + e.deviceId + " state has changed, connected: " + e.connected),
  228. 0 == e.connected && (wx.closeBLEConnection({
  229. deviceId: e.deviceId,
  230. success: function(e) {
  231. _wjUtils2.default.showLog("closeBLEConnection:", e)
  232. }
  233. }), wx.closeBluetoothAdapter({
  234. success: function(e) {
  235. _wjUtils2.default.showLog("closeBluetoothAdapter", e)
  236. }
  237. }))
  238. })
  239. }
  240. function reallyDisConnect(i) {
  241. var t = {};
  242. null != connectedDeviceId && wx.closeBLEConnection({
  243. deviceId: connectedDeviceId,
  244. success: function(e) {
  245. _wjUtils2.default.showLog("closeBLEConnection:", e)
  246. }
  247. }), wx.closeBluetoothAdapter({
  248. success: function(e) {
  249. _wjUtils2.default.showLog("closeBluetoothAdapter", e), t.serviceCode = 0, t.serviceInfo =
  250. "断开蓝牙成功!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t)
  251. },
  252. fail: function(e) {
  253. console.log(e), t.serviceCode = -1, t.serviceInfo = e.errMsg, (void 0 === i ? "undefined" :
  254. _typeof(i)) == TAG_FUNCTION && i(t)
  255. }
  256. })
  257. }
  258. function _writeBLECharacteristicValue(e, i) {
  259. var t = {};
  260. wx.writeBLECharacteristicValue({
  261. deviceId: connectedDeviceId,
  262. serviceId: reality_services,
  263. characteristicId: reality_writeServiceId,
  264. value: e,
  265. success: function(e) {
  266. _wjUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg), t.serviceCode = 0, t
  267. .serviceInfo = e.errMsg, (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(t)
  268. }
  269. })
  270. }
  271. function _onBLECharacteristicValueChange() {
  272. wx.onBLECharacteristicValueChange(function(e) {
  273. _wjUtils2.default.showLog(e.deviceId + " 'characteristic has changed"), console.log("rev:" + e), e
  274. .characteristicId === reality_indicateServiceId && 1 == ListenFlag && (void 0 ===
  275. DataListenerCallBack ? "undefined" : _typeof(DataListenerCallBack)) == TAG_FUNCTION &&
  276. DataListenerCallBack(e.value)
  277. })
  278. }
  279. function _SetDataListenerCallBack(e, i) {
  280. 1 == e ? (ListenFlag = !0, DataListenerCallBack = i) : 0 == e && (ListenFlag = !1)
  281. }
  282. function datacallback(e) {
  283. _SetDataListenerCallBack(!0, datacallback), _ReceiveData(e)
  284. }
  285. function _ReceiveData(e) {
  286. var i, t = {},
  287. c = _wjUtils2.default.getProtocolType();
  288. 0 == c ? i = _wjDataDecode2.default.decode_GB(e) : 1 == c && (i = _wjDataDecode2.default.decode_WX(e)), 0 === i
  289. .serviceCode && (c = i.serviceData.strData, i.serviceData.strData.startsWith(
  290. "fe01001a271100010a0018808004200128023a06") && 52 === i.serviceData.strData.length ||
  291. "fe010013271300020a001201411a0400000000" == i.serviceData.strData ? (i.serviceData.strData.startsWith(
  292. "fe01001a271100010a0018808004200128023a06") && 52 === i.serviceData.strData.length && (handshake = !
  293. 0), null != TimerID && (clearTimeout(TimerID), TimerID = null), _SetDataListenerCallBack(!1), t
  294. .serviceCode = 0, t.serviceInfo = "数据接收成功", t.serviceData = c, console.log("result_DataInteract", t),
  295. retdata = t.serviceData) : null != (e = PackageDecode(c)) ? (_wjUtils2.default.showLog(TimerID,
  296. "接收数据成功,取消时钟"), null != TimerID && (clearTimeout(TimerID), TimerID = null),
  297. _SetDataListenerCallBack(!1), t.serviceCode = 0, t.serviceInfo = "数据接收成功", t.serviceData = {}, t
  298. .serviceData.dataBuff = e) : (_wjUtils2.default.showError(TimerID, "数据解析失败,取消时钟"), null != TimerID && (
  299. clearTimeout(TimerID), TimerID = null), wjService._SetDataListenerCallBack(!1), t.serviceCode = -1,
  300. t.serviceInfo = "数据解析失败"), (void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) ==
  301. TAG_FUNCTION) && sendCallback(t)
  302. }
  303. function PackageDecode(e) {
  304. return _wjUtils2.default.showLog("WJPackageDecode:", e), null
  305. }
  306. function _StartSendData(o, e) {
  307. var s = {},
  308. l = 0,
  309. r = 0;
  310. null != TimerID && (clearTimeout(TimerID), TimerID = null), _wjDataDecode2.default.init(),
  311. "fe0100164e2300030a06080012024f4b100018002000" == o ? _SetDataListenerCallBack(!1) : (TimerID = setTimeout(
  312. pro_timeout, 1e4), _wjUtils2.default.showLog(TimerID, "开启时钟"), _SetDataListenerCallBack(!0,
  313. datacallback)), sendCallback = e,
  314. function i() {
  315. var e = "";
  316. 40 * r >= o[l].length && (l++, r = 0);
  317. console.log("currentsegment", r, "currentPackage", l, o);
  318. l < o.length && (e = o[l].length >= 40 * (r + 1) ? o[l].substring(40 * r, 40 * (r + 1)) : o[l].substring(
  319. 40 * r, o[l].length));
  320. console.log("currentsegment", r, "currentPackage", l, o);
  321. if ("" == e) _wjUtils2.default.showLog("数据发送完毕!"), "fe0100164e2300030a06080012024f4b100018002000" == o[s
  322. .serviceCode = 0] && (void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) ==
  323. TAG_FUNCTION && sendCallback(s);
  324. else {
  325. for (var t = _wjUtils2.default.hexStr2byteArray(e), c = new ArrayBuffer(t.byteLength), a = new DataView(
  326. c), n = 0; n < t.byteLength; n++) a.setUint8(n, t[n]);
  327. _wjUtils2.default.showLog("发送数据:", e), _writeBLECharacteristicValue(c, function(e) {
  328. 0 == e.serviceCode ? (r++, i()) : (_wjUtils2.default.showError(TimerID, "数据发送失败,取消时钟"),
  329. null != TimerID && (clearTimeout(TimerID), TimerID = null),
  330. _SetDataListenerCallBack(!1), s.serviceCode = -2, s.serviceInfo = "数据发送失败", (
  331. void 0 === sendCallback ? "undefined" : _typeof(sendCallback)) ==
  332. TAG_FUNCTION && sendCallback(s))
  333. })
  334. }
  335. }()
  336. }
  337. function pro_timeout() {
  338. var e = {};
  339. _wjUtils2.default.showError(TimerID, "数据接收超时,取消时钟"), null != TimerID && (clearTimeout(TimerID), TimerID = null),
  340. _SetDataListenerCallBack(!1), e.serviceCode = -3, e.serviceInfo = "数据接收超时", (void 0 === sendCallback ?
  341. "undefined" : _typeof(sendCallback)) == TAG_FUNCTION && sendCallback(e)
  342. }
  343. module.exports = {
  344. reallyConnect: reallyConnect,
  345. reallyScanConnect: reallyScanConnect,
  346. reallyDisConnect: reallyDisConnect,
  347. _writeBLECharacteristicValue: _writeBLECharacteristicValue,
  348. _SetDataListenerCallBack: _SetDataListenerCallBack,
  349. _enableService: _enableService
  350. };