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.

kcService.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. "use strict";
  2. var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
  3. return typeof e
  4. }
  5. : function (e) {
  6. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  7. }
  8. , _kcUtils = require("./kcUtils.js")
  9. , _kcUtils2 = _interopRequireDefault(_kcUtils);
  10. function _interopRequireDefault(e) {
  11. return e && e.__esModule ? e : {
  12. default: e
  13. }
  14. }
  15. var timeID, ListenFlag = void 0, DataListenerCallBack = void 0, connectedDeviceId = "", foundDevices = [], TAG_FUNCTION = "function", services = "", androidServices = "0000FEE7-0000-1000-8000-00805F9B34FB", iosServices = "FEE7", notifyServiceId = "", androidNotifyServiceId = "0000FEC8-0000-1000-8000-00805F9B34FB", iosNotifyServiceId = "FEC8", writeServiceId = "", androidWriteServiceId = "0000FEC7-0000-1000-8000-00805F9B34FB", iosWriteServiceId = "FEC7", readServiceId = "", androidReadServiceId = "0000FEC9-0000-1000-8000-00805F9B34FB", iosReadServiceId = "FEC9", device = {}, SetoutTimer = 0,
  16. unavailableServices = true;
  17. function reallyScanConnect(s) {
  18. wx.closeBluetoothAdapter(),
  19. _kcUtils2.default.showLog("/***********Runing :: Do reallyScanConnect() begin *************/"),
  20. foundDevices = [],
  21. wx.openBluetoothAdapter({
  22. success: function (e) {
  23. wx.startBluetoothDevicesDiscovery({
  24. services: [],
  25. success: function (e) {
  26. wx.onBluetoothDeviceFound(function (e) {
  27. for (var i = 0; i < e.devices.length; i++) {
  28. for (var c = !1, t = 0; t < foundDevices.length; t++)
  29. if (e.devices[i].deviceId == foundDevices[t].deviceId) {
  30. c = !0;
  31. break
  32. }
  33. if (_kcUtils2.default.showLog("connectedDeviceName", e.devices[i].name),
  34. 0 == c) {
  35. foundDevices.push(e.devices[i]);
  36. var n = e.devices[i].name;
  37. if (-1 != n.indexOf("ETC-KC-")) {
  38. connectedDeviceId = e.devices[i].deviceId,
  39. _kcUtils2.default.showLog("connectedDeviceId", e.devices);
  40. var o = {};
  41. o.device_name = n,
  42. o.device_id = connectedDeviceId,
  43. wx.stopBluetoothDevicesDiscovery({
  44. success: function (e) {
  45. _kcUtils2.default.showLog("停止扫描,开始连接"),
  46. reallyConnect(o, function (e) {
  47. (void 0 === s ? "undefined" : _typeof(s)) == TAG_FUNCTION && s(e)
  48. })
  49. },
  50. fail: function (e) {
  51. _kcUtils2.default.showError("stopBluetoothDevicesDiscovery fail", e)
  52. }
  53. });
  54. break
  55. }
  56. }
  57. }
  58. })
  59. },
  60. fail: function (e) {
  61. _kcUtils2.default.showError("startBluetoothDevicesDiscovery fail", e)
  62. }
  63. })
  64. },
  65. fail: function (e) {
  66. _kcUtils2.default.showError("openBluetoothAdapter fail", e)
  67. }
  68. }),
  69. wx.onBLEConnectionStateChange(function (e) {
  70. 0 == e.connected && (wx.closeBLEConnection({
  71. deviceId: e.deviceId,
  72. success: function (e) {
  73. _kcUtils2.default.showLog("closeBLEConnection:", e)
  74. }
  75. }),
  76. wx.closeBluetoothAdapter({
  77. success: function (e) {
  78. _kcUtils2.default.showLog("closeBluetoothAdapter", e)
  79. }
  80. })),
  81. console.error("device " + e.deviceId + " state has changed, connected: " + e.connected)
  82. })
  83. }
  84. function reallyConnect(device, callback) {
  85. console.log("WeiXin ble SDK 20240606");
  86. var result = {
  87. code: -2,
  88. msg: '连接失败'
  89. };
  90. var deviceName = device.device_name;
  91. console.log("name", deviceName);
  92. if (deviceName.includes("ETC-KC-") || deviceName.includes("K")) {
  93. connectedDeviceId = device.device_id;
  94. wx.createBLEConnection({
  95. deviceId: connectedDeviceId,
  96. success: function (res) {
  97. console.log("已连接,开始使能服务", connectedDeviceId);
  98. _enableService(function (serviceResult) {
  99. console.log("serviceResult:", serviceResult);
  100. if (serviceResult.serviceCode === 0) {
  101. console.log("2.连接成功,并使能成功");
  102. result.code = 0;
  103. result.msg = "连接成功";
  104. } else {
  105. console.log("2.连接成功,但服务使能失败");
  106. }
  107. if (typeof callback === TAG_FUNCTION) callback(result);
  108. });
  109. },
  110. fail: function (err) {
  111. console.error("createBLEConnection fail", err);
  112. if (typeof callback === TAG_FUNCTION) callback(result);
  113. }
  114. });
  115. } else {
  116. result.msg = "设备验证失败";
  117. if (typeof callback === TAG_FUNCTION) callback(result);
  118. }
  119. }
  120. function _setPlatformServicesId() {
  121. const infoa = wx.getSystemInfoSync();
  122. console.log(`%cplatform ${JSON.stringify(infoa.platform, null, 2)}`, "color: green;");
  123. }
  124. function containsServiceInPrefix(serviceId) {
  125. var ret = false, serviceTemp;
  126. // 确保将字符串转换为大写,以避免大小写敏感问题
  127. serviceTemp = serviceId.toUpperCase();
  128. // 获取前4个字符和前8个字符
  129. const prefix4 = serviceTemp.substring(0, 4);
  130. const prefix8 = serviceTemp.substring(0, 8);
  131. // 判断是否包含 "FEE7"
  132. ret = prefix4.includes("FEE7") || prefix8.includes("FEE7");
  133. if (ret) {
  134. services = serviceId;
  135. }
  136. return ret;
  137. }
  138. function containsNotifyInPrefix(service) {
  139. var ret = false, serviceTemp;
  140. // 确保将字符串转换为大写,以避免大小写敏感问题
  141. serviceTemp = service.toUpperCase();
  142. // 获取前4个字符和前8个字符
  143. const prefix4 = serviceTemp.substring(0, 4);
  144. const prefix8 = serviceTemp.substring(0, 8);
  145. // 判断是否包含 "FEC8"
  146. ret = prefix4.includes("FEC8") || prefix8.includes("FEC8");
  147. if (ret) {
  148. notifyServiceId = service;
  149. }
  150. return ret;
  151. }
  152. function containsWriteInPrefix(service) {
  153. var ret = false, serviceTemp;
  154. // 确保将字符串转换为大写,以避免大小写敏感问题
  155. serviceTemp = service.toUpperCase();
  156. // 获取前4个字符和前8个字符
  157. const prefix4 = serviceTemp.substring(0, 4);
  158. const prefix8 = serviceTemp.substring(0, 8);
  159. // 判断是否包含 "FEC7"
  160. ret = prefix4.includes("FEC7") || prefix8.includes("FEC7");
  161. if (ret) {
  162. writeServiceId = service;
  163. }
  164. return ret;
  165. }
  166. function containsReadInPrefix(service) {
  167. var ret = false, serviceTemp;
  168. // 确保将字符串转换为大写,以避免大小写敏感问题
  169. serviceTemp = service.toUpperCase();
  170. // 获取前4个字符和前8个字符
  171. const prefix4 = serviceTemp.substring(0, 4);
  172. const prefix8 = serviceTemp.substring(0, 8);
  173. // 判断是否包含 "FEC9"
  174. ret = prefix4.includes("FEC9") || prefix8.includes("FEC9");
  175. if (ret) {
  176. readServiceId = service;
  177. }
  178. return ret;
  179. }
  180. function _enableService(callback) {
  181. var result = {};
  182. _setPlatformServicesId();
  183. wx.getBLEDeviceServices({
  184. deviceId: connectedDeviceId,
  185. success: function (res) {
  186. for (var i = 0; i < res.services.length; i++) {
  187. console.log(res.services[i]);
  188. var uuid = res.services[i].uuid;
  189. if (containsServiceInPrefix(uuid)) {
  190. wx.getBLEDeviceCharacteristics({
  191. deviceId: connectedDeviceId,
  192. serviceId: uuid,
  193. success: function (charRes) {
  194. var matchedChars = charRes.characteristics.filter(char => containsNotifyInPrefix(char.uuid) || containsWriteInPrefix(char.uuid)).length;
  195. console.log("matchedChars", matchedChars);
  196. if (matchedChars < 2) {
  197. result.serviceCode = -1;
  198. result.serviceInfo = "getBLEDeviceCharacteristics temp<2!";
  199. if (typeof callback === "function") callback(result);
  200. } else {
  201. wx.notifyBLECharacteristicValueChange({
  202. deviceId: connectedDeviceId,
  203. serviceId: services,
  204. characteristicId: notifyServiceId,
  205. state: true,
  206. success: function (res) {
  207. },
  208. fail: function (res) {
  209. },
  210. complete: function (res) {
  211. console.log("res:", res);
  212. if (res.errno === 0) {
  213. _onBLECharacteristicValueChange();
  214. result.serviceCode = 0;
  215. result.serviceInfo = "enable success!";
  216. } else {
  217. result.serviceCode = -2;
  218. result.serviceInfo = "notifyBLECharacteristicValueChange fail!";
  219. }
  220. if (typeof callback === "function") callback(result);
  221. }
  222. });
  223. }
  224. },
  225. fail: function () {
  226. console.error("getBLEDeviceCharacteristics fail");
  227. result.serviceCode = -3;
  228. result.serviceInfo = "getBLEDeviceCharacteristics fail!";
  229. if (typeof callback === "function") callback(result);
  230. }
  231. });
  232. unavailableServices = false;
  233. break;
  234. }
  235. }
  236. console.log("unavailableServices", unavailableServices);
  237. },
  238. fail: function () {
  239. result.serviceCode = -4;
  240. result.serviceInfo = "getBLEDeviceServices fail!";
  241. if (typeof callback === "function") callback(result);
  242. }
  243. });
  244. }
  245. function reallyDisConnect(callBack) {
  246. console.log("/***********Runing :: Do reallyDisConnect() begin *************/");
  247. var c = {
  248. code: 0,
  249. msg: '已断连'
  250. };
  251. wx.closeBLEConnection({
  252. deviceId: connectedDeviceId,
  253. success: function (e) {
  254. connectedDeviceId = "";
  255. console.log("closeBLEConnection success", e);
  256. },
  257. fail: function (e) {
  258. console.error("closeBLEConnection fail", e);
  259. },
  260. complete: function (e) {
  261. (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
  262. }
  263. });
  264. }
  265. function _writeBLECharacteristicValue(e, i) {
  266. var c = {};
  267. wx.writeBLECharacteristicValue({
  268. deviceId: connectedDeviceId,
  269. serviceId: services,
  270. characteristicId: writeServiceId,
  271. value: e,
  272. success: function (e) {
  273. _kcUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg),
  274. c.serviceCode = 0,
  275. c.serviceInfo = e.errMsg,
  276. (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(c)
  277. }
  278. })
  279. }
  280. function _onBLECharacteristicValueChange() {
  281. wx.onBLECharacteristicValueChange(function (e) {
  282. _kcUtils2.default.showLog("有回复"),
  283. 1 == ListenFlag && (void 0 === DataListenerCallBack ? "undefined" : _typeof(DataListenerCallBack)) == TAG_FUNCTION && DataListenerCallBack(e.value)
  284. })
  285. }
  286. function _SetDataListenerCallBack(e, i) {
  287. 1 == e ? (ListenFlag = !0,
  288. DataListenerCallBack = i) : 0 == e && (ListenFlag = !1)
  289. }
  290. function disconnectDeviceBluetooth(callBack) {
  291. console.log("/***********Runing :: Do disconnectDeviceBluetooth() begin *************/");
  292. console.log("connectedDeviceId是空", connectedDeviceId == "");
  293. var c = {
  294. code: 0,
  295. msg: '已断开'
  296. };
  297. if (connectedDeviceId != "") {
  298. wx.closeBLEConnection({
  299. deviceId: connectedDeviceId,
  300. success: function (e) {
  301. connectedDeviceId = "";
  302. console.log("closeBLEConnection success", e);
  303. wx.closeBluetoothAdapter({
  304. success: function (e) {
  305. console.log("closeBluetoothAdapter success", e);
  306. },
  307. fail: function (e) {
  308. console.error("closeBluetoothAdapter fail", e);
  309. },
  310. complete: function (e) {
  311. (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
  312. }
  313. });
  314. }
  315. });
  316. }else{
  317. wx.closeBluetoothAdapter({
  318. success: function (e) {
  319. console.log("closeBluetoothAdapter success", e);
  320. },
  321. fail: function (e) {
  322. console.error("closeBluetoothAdapter fail", e);
  323. },
  324. complete: function (e) {
  325. (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
  326. }
  327. });
  328. }
  329. }
  330. module.exports = {
  331. reallyConnect: reallyConnect,
  332. reallyScanConnect: reallyScanConnect,
  333. reallyDisConnect: reallyDisConnect,
  334. _writeBLECharacteristicValue: _writeBLECharacteristicValue,
  335. _SetDataListenerCallBack: _SetDataListenerCallBack,
  336. disconnectDeviceBluetooth: disconnectDeviceBluetooth,
  337. };