deviceConnect2.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const app = getApp();
  2. let xBlufi = require("../../../utils/blufi/xBlufi.js");
  3. import strings from '../../../utils/strings.js';
  4. import lexin_devices from '../../../utils/lexin/devices';
  5. import junzhengUtil from '../../../utils/junzheng/util';
  6. import routeUtil from '../../../utils/routeUtil.js';
  7. import routePath from '../../../utils/routePath.js';
  8. Page({
  9. data: {
  10. nvabarData: {
  11. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: '连接配网', //导航栏 中间的标题
  13. },
  14. devicesList: [],
  15. searching: false,
  16. version: '2.0',
  17. name: '',
  18. connectedDeviceId: '',
  19. connected: true,
  20. deviceInfo: null,
  21. isInitOK: false,
  22. customData: '',
  23. isSubmit: false,
  24. islanya: false,
  25. imageUrl: "./../../../img/f.png",
  26. connectDevice: {},
  27. },
  28. onLoad(options) {
  29. console.log("开始搜索")
  30. const that = this;
  31. var param = options.param;
  32. if (!strings.isEmpty(param)) {
  33. param = JSON.parse(options.param);
  34. that.data.connectDevice = param;
  35. if (!strings.isEmpty(param.typeList)) {
  36. var typeList = param.typeList;
  37. typeList = typeList.filter((v) => v.connectType == 3);
  38. if (!strings.isEmpty(typeList)) {
  39. that.setData({
  40. imageUrl: typeList[0].deviceLinkResp.icon2,
  41. });
  42. }
  43. }
  44. }
  45. if (app.globalData.oneInitBluetooth) {
  46. app.globalData.oneInitBluetooth = false;
  47. xBlufi.initXBlufi(1);
  48. };
  49. xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
  50. that.search();
  51. },
  52. ///停止搜索
  53. stopSearch: function () {
  54. xBlufi.notifyStartDiscoverBle({
  55. 'isStart': false,
  56. });
  57. },
  58. ///开始或结束,也可以过滤名称
  59. search: function () {
  60. xBlufi.notifyStartDiscoverBle({
  61. 'isStart': true,
  62. });
  63. },
  64. // {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}}
  65. // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
  66. // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}},{"deviceId":"42:E0:22:C2:6E:25","name":"","RSSI":-92,"connectable":true,"advertisData":"4c001007221fa8d7a84ba8","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
  67. funListenDeviceMsgEvent: function (options) {
  68. var that = this;
  69. switch (options.type) {
  70. //发现设备列表回调开始
  71. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
  72. if (!options.result) {
  73. console.log("蓝牙未开启 fail =》", options)
  74. wx.showToast({
  75. title: '蓝牙未开启',
  76. icon: 'none'
  77. })
  78. } else {
  79. //蓝牙搜索开始
  80. that.setData({
  81. searching: true
  82. });
  83. }
  84. break;
  85. // [{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0","RSSI":-62,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fd3af0","serviceData":{}}]
  86. ///发现设备列表回调
  87. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
  88. // 搜索到蓝牙
  89. if (options.result) {
  90. var devicesList = options.data.filter((v) => {
  91. var name = v.name;
  92. var isLexin = name.indexOf(lexin_devices.leXin) !== -1;
  93. var isJungzheng1 = junzhengUtil.isJungzheng1(name);
  94. var isJungzheng2 = junzhengUtil.isJungzheng2(name);
  95. var localName = v.localName;
  96. var isLexin1 = localName.indexOf(lexin_devices.leXin) !== -1;
  97. var isJungzheng3 = junzhengUtil.isJungzheng1(localName);
  98. var isJungzheng4 = junzhengUtil.isJungzheng2(localName);
  99. return isLexin || isJungzheng1 || isJungzheng2 || isLexin1 || isJungzheng3 || isJungzheng4;
  100. ///2AXD
  101. // return v.name == "AIrSMArT_d0311004155e";
  102. ///X4
  103. // return v.name == "AIrSMArT_861210052356337";
  104. });
  105. if (!strings.isEmpty(devicesList)) {
  106. console.log("打印4", JSON.stringify(devicesList))
  107. ///过滤乐鑫设备
  108. that.setData({
  109. islanya: true,
  110. devicesList: devicesList,
  111. });
  112. }
  113. }
  114. break;
  115. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
  116. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
  117. ///主动去连接连接
  118. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  119. wx.hideLoading();
  120. if (options.result) {
  121. that.setData({
  122. islanya: false,
  123. devicesList: [],
  124. });
  125. var p = JSON.stringify(that.data.connectDevice);
  126. var param = '?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "&param=" + p;
  127. routeUtil.jumpParam(routePath.deviceConnect3, param);
  128. } else {
  129. wx.showModal({
  130. title: '提示',
  131. content: '连接失败',
  132. showCancel: false
  133. });
  134. }
  135. break;
  136. ///停止发现设备列表回调
  137. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  138. if (options.result) {
  139. //蓝牙停止搜索ok
  140. console.log('蓝牙停止搜索ok')
  141. } else {
  142. //蓝牙停止搜索失败
  143. console.log('蓝牙停止搜索失败')
  144. }
  145. that.setData({
  146. searching: false
  147. });
  148. break;
  149. }
  150. },
  151. // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}]
  152. async Connect(e) {
  153. var that = this;
  154. const devicesList = that.data.devicesList;
  155. if (devicesList.length < 1) {
  156. wx.showToast({
  157. title: '请短按设备配对键',
  158. icon: 'none',
  159. duration: 2000
  160. });
  161. return;
  162. };
  163. if (that.data.isSubmit) {
  164. return;
  165. };
  166. that.data.isSubmit = true;
  167. // 停止搜索
  168. that.stopSearch();
  169. var name = devicesList[0].name;
  170. var deviceId = devicesList[0].deviceId;
  171. console.log("")
  172. var isJungzheng1 = junzhengUtil.isJungzheng1(name);
  173. var isJungzheng2 = junzhengUtil.isJungzheng2(name);
  174. if (isJungzheng1 || isJungzheng2) {
  175. setTimeout(() => {
  176. wx.hideLoading();
  177. that.setData({
  178. islanya: false,
  179. devicesList: [],
  180. });
  181. var param = '?deviceId=' + deviceId + '&name=' + name + "&param=" + JSON.stringify(that.data.connectDevice);
  182. routeUtil.jumpParam(routePath.deviceConnect4, param);
  183. }, 500);
  184. return;
  185. }
  186. //开始连接
  187. var name = devicesList[0].name;
  188. // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
  189. xBlufi.notifyConnectBle({
  190. isStart: true,
  191. name: name,
  192. deviceId: deviceId,
  193. });
  194. wx.showLoading({
  195. title: '请稍后',
  196. });
  197. },
  198. getBluetoothStatus() {
  199. app.getBluetoothStatus();
  200. },
  201. notRoter() {
  202. wx.navigateBack({
  203. delta: 1
  204. });
  205. },
  206. onShow: function () {
  207. var that = this;
  208. if (that.data.isSubmit) {
  209. that.data.isSubmit = false;
  210. that.search();
  211. }
  212. },
  213. onUnload: function () {
  214. var that = this;
  215. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  216. },
  217. })