deviceConnect2.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. return isLexin || isJungzheng1 || isJungzheng2;
  96. // var localName = v.localName;
  97. // var isLexin1 = localName.indexOf(lexin_devices.leXin) !== -1;
  98. // var isJungzheng3 = junzhengUtil.isJungzheng1(localName);
  99. // var isJungzheng4 = junzhengUtil.isJungzheng2(localName);
  100. // return isLexin || isJungzheng1 || isJungzheng2 || isLexin1 || isJungzheng3 || isJungzheng4;
  101. ///2AXD
  102. // return v.name == "AIrSMArT_d0311004155e";
  103. ///X4
  104. // return v.name == "AIrSMArT_861210052356337";
  105. });
  106. if (!strings.isEmpty(devicesList)) {
  107. console.log("打印4", JSON.stringify(devicesList))
  108. ///过滤乐鑫设备
  109. that.setData({
  110. islanya: true,
  111. devicesList: devicesList,
  112. });
  113. }
  114. }
  115. break;
  116. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
  117. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
  118. ///主动去连接连接
  119. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  120. wx.hideLoading();
  121. if (options.result) {
  122. that.setData({
  123. islanya: false,
  124. devicesList: [],
  125. });
  126. var p = JSON.stringify(that.data.connectDevice);
  127. var param = '?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "&param=" + p;
  128. routeUtil.jumpParam(routePath.deviceConnect3, param);
  129. } else {
  130. wx.showModal({
  131. title: '提示',
  132. content: '连接失败',
  133. showCancel: false
  134. });
  135. }
  136. break;
  137. ///停止发现设备列表回调
  138. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  139. if (options.result) {
  140. //蓝牙停止搜索ok
  141. console.log('蓝牙停止搜索ok')
  142. } else {
  143. //蓝牙停止搜索失败
  144. console.log('蓝牙停止搜索失败')
  145. }
  146. that.setData({
  147. searching: false
  148. });
  149. break;
  150. }
  151. },
  152. // [{"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":{}}]
  153. async Connect(e) {
  154. var that = this;
  155. const devicesList = that.data.devicesList;
  156. if (devicesList.length < 1) {
  157. wx.showToast({
  158. title: '请短按设备配对键',
  159. icon: 'none',
  160. duration: 2000
  161. });
  162. return;
  163. };
  164. if (that.data.isSubmit) {
  165. return;
  166. };
  167. that.data.isSubmit = true;
  168. // 停止搜索
  169. that.stopSearch();
  170. var name = devicesList[0].name;
  171. var deviceId = devicesList[0].deviceId;
  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. })