您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wjService.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. function _interopRequireDefault(e) {
  11. return e && e.__esModule ? e : {
  12. default: e
  13. }
  14. }
  15. var connectedDeviceId, timeID, onDisconnect, ListenFlag = void 0,
  16. DataListenerCallBack = void 0,
  17. foundDevices = [],
  18. TAG_FUNCTION = "function",
  19. services = "0000FEE7-0000-1000-8000-00805F9B34FB",
  20. notifyServiceId = "0000FEC8-0000-1000-8000-00805F9B34FB",
  21. writeServiceId = "0000FEC7-0000-1000-8000-00805F9B34FB";
  22. function reallyscanConnect(s, r) {
  23. var d = {};
  24. _wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"), foundDevices = [], wx
  25. .closeBluetoothAdapter(), wx.openBluetoothAdapter({
  26. success: function(e) {
  27. wx.startBluetoothDevicesDiscovery({
  28. services: [],
  29. success: function(e) {
  30. wx.onBluetoothDeviceFound(function(e) {
  31. for (var i = 0; i < e.devices.length; i++) {
  32. for (var n = !1, c = 0; c < foundDevices.length; c++)
  33. if (e.devices[i].deviceId == foundDevices[c].deviceId) {
  34. n = !0;
  35. break
  36. } var t = e.devices[i].name;
  37. if (_wjUtils2.default.showLog("connectedDeviceName", e
  38. .devices[i].name), 0 == n && "" != t && null != t &&
  39. (foundDevices.push(e.devices[i]), -1 != t.indexOf(
  40. "WJ") || -1 != t.indexOf("WanJi"))) {
  41. connectedDeviceId = e.devices[i].deviceId;
  42. var o = {};
  43. o.name = t, o.deviceId = connectedDeviceId, _wjUtils2
  44. .default.showLog("connectedDeviceId", e.devices), wx
  45. .stopBluetoothDevicesDiscovery({
  46. success: function(e) {
  47. _wjUtils2.default.showLog(
  48. "停止扫描,开始连接"), null !=
  49. timeID && (clearTimeout(timeID),
  50. timeID = null),
  51. reallyConnect(o, function(e) {
  52. (void 0 === s ?
  53. "undefined" :
  54. _typeof(s)) ==
  55. TAG_FUNCTION && s(e)
  56. }, function(e) {
  57. (void 0 === r ?
  58. "undefined" :
  59. _typeof(r)) ==
  60. TAG_FUNCTION && r(e)
  61. })
  62. },
  63. fail: function(e) {
  64. _wjUtils2.default.showError(
  65. "stopBluetoothDevicesDiscovery:" +
  66. e), d.serviceCode = 1003, d
  67. .serviceInfo =
  68. "stopBluetoothDevicesDiscovery fail!",
  69. (void 0 === s ? "undefined" :
  70. _typeof(s)) ==
  71. TAG_FUNCTION && s(d)
  72. }
  73. });
  74. break
  75. }
  76. }
  77. })
  78. },
  79. fail: function(e) {
  80. _wjUtils2.default.showError("scanerror:" + e), d.serviceCode = 1002, d
  81. .serviceInfo = "scanerror fail!", (void 0 === s ? "undefined" : _typeof(
  82. s)) == TAG_FUNCTION && s(d)
  83. }
  84. }), timeID = setTimeout(function() {
  85. wx.closeBluetoothAdapter(), _wjUtils2.default.showError("scan timeout"), d
  86. .serviceCode = 100100, d.serviceInfo = "scan timeout fail!", (void 0 === s ?
  87. "undefined" : _typeof(s)) == TAG_FUNCTION && s(d)
  88. }, 5e3)
  89. },
  90. fail: function(e) {
  91. _wjUtils2.default.showError("openadapter:" + e), d.serviceCode = 1001, d.serviceInfo =
  92. "openadapter fail!", (void 0 === s ? "undefined" : _typeof(s)) == TAG_FUNCTION && s(d)
  93. }
  94. })
  95. }
  96. function reallyConnect(e, i, n) {
  97. _wjUtils2.default.showLog("start reallyConnect,20190830001,V0.0.4"), _onBLEConnectionStateChange(), onDisconnect =
  98. n;
  99. var c = {},
  100. t = e.name;
  101. _wjUtils2.default.showLog("name:", t), -1 != t.indexOf("WJ") || -1 != t.indexOf("WanJi") || -1 != t.indexOf("ZZ") ?
  102. (connectedDeviceId = e.deviceId, wx.createBLEConnection({
  103. deviceId: connectedDeviceId,
  104. success: function(e) {
  105. _wjUtils2.default.showLog("已连接,开始使能服务", connectedDeviceId), _enableService(function(e) {
  106. 0 == e.serviceCode ? (_wjUtils2.default.showLog("已连接,并使能成功"), c.serviceCode = 0,
  107. c.serviceInfo = "connected", (void 0 === i ? "undefined" : _typeof(
  108. i)) == TAG_FUNCTION && i(c), (void 0 === onDisconnect ? "undefined" :
  109. _typeof(onDisconnect)) == TAG_FUNCTION && onDisconnect(c)) : ((c =
  110. e).serviceInfo = "连接成功,但服务使能失败!", (void 0 === i ? "undefined" :
  111. _typeof(i)) == TAG_FUNCTION && i(c))
  112. }), timeID = setTimeout(function() {
  113. wx.closeBLEConnection({
  114. deviceId: connectedDeviceId,
  115. success: function(e) {}
  116. }), wx.closeBluetoothAdapter(), _wjUtils2.default.showError("scan timeout"),
  117. c.serviceCode = 100100, c.serviceInfo = "scan timeout fail!", (void 0 ===
  118. i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(c)
  119. }, 5e3)
  120. },
  121. fail: function(e) {
  122. _wjUtils2.default.showError("creatcon error:" + e), c.serviceCode = 1005, c.serviceInfo =
  123. "creatcon error fail!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(
  124. c)
  125. }
  126. })) : (c.serviceCode = 1004, c.serviceInfo = "device's name error", (void 0 === i ? "undefined" : _typeof(i)) ==
  127. TAG_FUNCTION && i(c))
  128. }
  129. function _enableService(t) {
  130. var o = {};
  131. wx.getBLEDeviceServices({
  132. deviceId: connectedDeviceId,
  133. success: function(e) {
  134. for (var i = 0; i < e.services.length; i++) {
  135. var n = e.services[i].uuid;
  136. n == services && wx.getBLEDeviceCharacteristics({
  137. deviceId: connectedDeviceId,
  138. serviceId: n,
  139. success: function(e) {
  140. for (var i = 0, n = 0; n < e.characteristics.length; n++) {
  141. var c = e.characteristics[n].uuid;
  142. c == notifyServiceId ? i++ : c == writeServiceId && i++
  143. }
  144. i < 2 ? (o.serviceCode = -1, o.serviceInfo =
  145. "getBLEDeviceCharacteristics temp<2!", (void 0 === t ?
  146. "undefined" : _typeof(t)) == TAG_FUNCTION && t(o)) : wx
  147. .notifyBLECharacteristicValueChange({
  148. deviceId: connectedDeviceId,
  149. serviceId: services,
  150. characteristicId: notifyServiceId,
  151. state: !0,
  152. success: function(e) {
  153. _onBLECharacteristicValueChange(), null != timeID &&
  154. (clearTimeout(timeID), timeID = null), o
  155. .serviceCode = 0, o.serviceInfo =
  156. "enable success!", (void 0 === t ? "undefined" :
  157. _typeof(t)) == TAG_FUNCTION && t(o)
  158. },
  159. fail: function() {
  160. o.serviceCode = 1008, o.serviceInfo =
  161. "notifyBLECharacteristicValueChange fail!", (
  162. void 0 === t ? "undefined" : _typeof(t)) ==
  163. TAG_FUNCTION && t(o)
  164. }
  165. })
  166. },
  167. fail: function() {
  168. o.serviceCode = 1007, o.serviceInfo =
  169. "getBLEDeviceCharacteristics fail!", (void 0 === t ? "undefined" :
  170. _typeof(t)) == TAG_FUNCTION && t(o)
  171. }
  172. })
  173. }
  174. },
  175. fail: function() {
  176. o.serviceCode = 1006, o.serviceInfo = "getBLEDeviceServices fail!", (void 0 === t ?
  177. "undefined" : _typeof(t)) == TAG_FUNCTION && t(o)
  178. }
  179. })
  180. }
  181. function _onBLEConnectionStateChange() {
  182. var i = {};
  183. wx.onBLEConnectionStateChange(function(e) {
  184. console.error("device " + e.deviceId + " state has changed, connected: " + e.connected), 0 == e
  185. .connected && (wx.closeBLEConnection({
  186. deviceId: connectedDeviceId,
  187. success: function(e) {}
  188. }), wx.closeBluetoothAdapter(), i.serviceCode = 1, i.serviceInfo = "disconnected", (void 0 ===
  189. onDisconnect ? "undefined" : _typeof(onDisconnect)) == TAG_FUNCTION && onDisconnect(i))
  190. })
  191. }
  192. function reallyDisConnect(i) {
  193. var n = {};
  194. wx.closeBLEConnection({
  195. deviceId: connectedDeviceId,
  196. success: function(e) {
  197. _wjUtils2.default.showLog("closeBLEConnection:", e)
  198. }
  199. }), wx.closeBluetoothAdapter({
  200. success: function(e) {
  201. _wjUtils2.default.showLog("closeBluetoothAdapter", e), n.serviceCode = 0, n.serviceInfo =
  202. "断开蓝牙成功!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(n)
  203. }
  204. })
  205. }
  206. function _writeBLECharacteristicValue(e, i) {
  207. var n = {};
  208. wx.writeBLECharacteristicValue({
  209. deviceId: connectedDeviceId,
  210. serviceId: services,
  211. characteristicId: writeServiceId,
  212. value: e,
  213. success: function(e) {
  214. _wjUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg), n.serviceCode = 0, n
  215. .serviceInfo = e.errMsg, (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(n)
  216. }
  217. })
  218. }
  219. function _onBLECharacteristicValueChange() {
  220. wx.onBLECharacteristicValueChange(function(e) {
  221. _wjUtils2.default.showLog("有回复"), 1 == ListenFlag && (void 0 === DataListenerCallBack ? "undefined" :
  222. _typeof(DataListenerCallBack)) == TAG_FUNCTION && DataListenerCallBack(e.value)
  223. })
  224. }
  225. function _SetDataListenerCallBack(e, i) {
  226. 1 == e ? (ListenFlag = !0, DataListenerCallBack = i) : 0 == e && (ListenFlag = !1)
  227. }
  228. module.exports = {
  229. reallyscanConnect: reallyscanConnect,
  230. reallyDisConnect: reallyDisConnect,
  231. reallyConnect: reallyConnect,
  232. _writeBLECharacteristicValue: _writeBLECharacteristicValue,
  233. _SetDataListenerCallBack: _SetDataListenerCallBack
  234. };