Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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