deviceConnect2.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. isOnBluetooth: false,
  26. imageUrl: "./../../../img/f.png",
  27. connectDevice: {},
  28. },
  29. onLoad(options) {
  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. ///2AXD
  97. // return v.name == "AIrSMArT_d0311004155e";
  98. ///X4
  99. // return v.name == "AIrSMArT_861210052356337";
  100. });
  101. if (!strings.isEmpty(devicesList)) {
  102. ///过滤乐鑫设备
  103. that.setData({
  104. islanya: true,
  105. devicesList: devicesList,
  106. });
  107. }
  108. }
  109. break;
  110. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
  111. // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
  112. ///主动去连接连接
  113. case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
  114. wx.hideLoading();
  115. if (options.result) {
  116. if (that.data.isOnBluetooth) {
  117. return;
  118. };
  119. that.setData({
  120. isOnBluetooth: true,
  121. });
  122. setTimeout(() => {
  123. that.setData({
  124. islanya: false,
  125. devicesList: [],
  126. isOnBluetooth: false,
  127. });
  128. var p = JSON.stringify(that.data.connectDevice);
  129. var param = '?deviceId=' + options.data.deviceId + '&name=' + options.data.name + "&param=" + p;
  130. routeUtil.jumpParam(routePath.deviceConnect3, param);
  131. }, 200);
  132. } else {
  133. wx.showModal({
  134. title: '提示',
  135. content: '连接失败',
  136. showCancel: false
  137. });
  138. }
  139. break;
  140. ///停止发现设备列表回调
  141. case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
  142. if (options.result) {
  143. //蓝牙停止搜索ok
  144. console.log('蓝牙停止搜索ok')
  145. } else {
  146. //蓝牙停止搜索失败
  147. console.log('蓝牙停止搜索失败')
  148. }
  149. that.setData({
  150. searching: false
  151. });
  152. break;
  153. }
  154. },
  155. // [{"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":{}}]
  156. async Connect(e) {
  157. var that = this;
  158. const devicesList = that.data.devicesList;
  159. if (devicesList.length < 1) {
  160. wx.showToast({
  161. title: '请短按设备配对键',
  162. icon: 'none',
  163. duration: 2000
  164. });
  165. return;
  166. };
  167. if (that.data.isSubmit) {
  168. return;
  169. };
  170. that.data.isSubmit = true;
  171. // 停止搜索
  172. that.stopSearch();
  173. var name = devicesList[0].name;
  174. var deviceId = devicesList[0].deviceId;
  175. var isJungzheng1 = junzhengUtil.isJungzheng1(name);
  176. var isJungzheng2 = junzhengUtil.isJungzheng2(name);
  177. if (isJungzheng1 || isJungzheng2) {
  178. var param = '?deviceId=' + deviceId + '&name=' + name + "&param=" + JSON.stringify(that.data.connectDevice);
  179. routeUtil.jumpParam(routePath.deviceConnect4, param);
  180. return;
  181. }
  182. //开始连接
  183. var name = devicesList[0].name;
  184. // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
  185. xBlufi.notifyConnectBle({
  186. isStart: true,
  187. name: name,
  188. deviceId: deviceId,
  189. });
  190. wx.showLoading({
  191. title: '请稍后',
  192. });
  193. },
  194. getBluetoothStatus() {
  195. app.getBluetoothStatus();
  196. },
  197. notRoter() {
  198. wx.navigateBack({
  199. delta: 1
  200. });
  201. },
  202. onShow: function () {
  203. var that = this;
  204. if (that.data.isSubmit) {
  205. that.data.isSubmit = false;
  206. that.search();
  207. }
  208. },
  209. onUnload: function () {
  210. var that = this;
  211. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  212. },
  213. })