deviceConnect2.js 7.3 KB

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