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