deviceConnect3.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. const app = getApp();
  2. let xBlufi = require("../../../utils/blufi/xBlufi.js");
  3. import strings from '../../../utils/strings.js';
  4. import routeUtil from '../../../utils/routeUtil.js';
  5. import routeRoot from '../../../utils/routeRoot.js'
  6. let percentIn = null;
  7. let errTi = null;
  8. Page({
  9. data: {
  10. nvabarData: {
  11. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: '连接配网', //导航栏 中间的标题
  13. },
  14. scopeBluetooth: app.globalData.scopeBluetooth,
  15. ssid: app.globalData.ssid,
  16. password: app.globalData.pwdData,
  17. version: '2.0',
  18. name: '',
  19. connectedDeviceId: '',
  20. connected: true,
  21. deviceInfo: null,
  22. isInitOK: false,
  23. customData: '',
  24. percent: 0,
  25. ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
  26. imageUrl: "./../../../img/g.png",
  27. },
  28. onLoad: function (options) {
  29. var that = this;
  30. var param = options.param;
  31. if (!strings.isEmpty(param)) {
  32. param = JSON.parse(param);
  33. that.setData({
  34. imageUrl: param.img,
  35. });
  36. }
  37. // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
  38. that.setData({
  39. name: options.name,
  40. connectedDeviceId: options.deviceId,
  41. })
  42. xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
  43. ///通知初始化获取设备的服务列表等信息
  44. xBlufi.notifyInitBleEsp32({
  45. deviceId: options.deviceId,
  46. });
  47. percentIn = setInterval(() => {
  48. if (that.data.percent === 100) {
  49. clearInterval(percentIn);
  50. ///失败
  51. that.setData({
  52. ruterStatus: 2
  53. });
  54. return;
  55. };
  56. that.data.percent++;
  57. that.setData({
  58. percent: that.data.percent
  59. });
  60. }, 600);
  61. },
  62. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB","characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  63. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  64. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  65. funListenDeviceMsgEvent: function (options) {
  66. var that = this;
  67. console.log("gadfqwerqwerqr==1111==" + options.type + "===" + JSON.stringify(options));
  68. switch (options.type) {
  69. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB",
  70. // "characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  71. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  72. if (options.result) {
  73. console.log('初始化成功');
  74. that.OnClickStart();
  75. } else {
  76. console.log('初始化失败')
  77. that.setData({
  78. connected: false
  79. })
  80. that.setData({
  81. ruterStatus: 2
  82. });
  83. }
  84. break;
  85. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  86. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  87. ///连接结果
  88. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  89. if (!options.result) {
  90. errTi = setTimeout(() => {
  91. that.setData({
  92. ruterStatus: 2
  93. });
  94. clearTimeout(errTi);
  95. clearInterval(percentIn);
  96. }, 5000);
  97. } else {
  98. // 配网成功
  99. if (options.data.progress == 100) {
  100. var deviceId = that.data.name;
  101. var device = {
  102. connectType: 3,
  103. devName: "",
  104. state: "online",
  105. name: deviceId,
  106. deviceId: deviceId,
  107. mac: deviceId,
  108. imageUrl: that.data.imageUrl,
  109. }
  110. getCurrentPages()[0].addWifiDevice(device);
  111. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  112. // 记住密码
  113. wx.setStorage({
  114. key: "wifiInfo",
  115. data: JSON.stringify({
  116. "password": app.globalData.pwdData,
  117. "ssid": app.globalData.ssid
  118. }),
  119. });
  120. setTimeout(() => {
  121. if (percentIn != null) {
  122. clearTimeout(errTi);
  123. clearInterval(percentIn);
  124. that.setData({
  125. percent: 100,
  126. ruterStatus: 1,
  127. });
  128. }
  129. }, 500);
  130. }
  131. }
  132. break;
  133. ///设备连接状态回调
  134. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  135. that.setData({
  136. connected: options.result
  137. });
  138. if (!options.result) {
  139. wx.showModal({
  140. title: '很抱歉提醒你!',
  141. content: '小程序与设备异常断开',
  142. showCancel: false, //是否显示取消按钮
  143. success: function (res) {
  144. wx.navigateBack({
  145. delta: 1
  146. });
  147. },
  148. });
  149. }
  150. break;
  151. }
  152. },
  153. ///校验wifi账号密码
  154. OnClickStart: function () {
  155. if (!app.globalData.ssid) {
  156. wx.showToast({
  157. title: 'SSID不能为空',
  158. icon: 'none'
  159. })
  160. return
  161. }
  162. // if (!app.globalData.pwdData) {
  163. // wx.showToast({
  164. // title: '密码不能为空',
  165. // icon: 'none'
  166. // })
  167. // return;
  168. // }
  169. xBlufi.notifySendRouterSsidAndPassword({
  170. ssid: app.globalData.ssid,
  171. password: app.globalData.pwdData
  172. });
  173. },
  174. bindPasswordInput: function (e) {
  175. var that = this;
  176. that.setData({
  177. password: e.detail.value
  178. })
  179. },
  180. bindCustomDataInput: function (e) {
  181. var that = this;
  182. that.setData({
  183. customData: e.detail.value
  184. })
  185. },
  186. egen() {
  187. wx.navigateBack({
  188. delta: 1
  189. })
  190. },
  191. goIndex() {
  192. routeUtil.goBackRoute(routeRoot.indexRoot);
  193. },
  194. onUnload: function () {
  195. // 关闭蓝牙连接
  196. var that = this
  197. xBlufi.notifyConnectBle({
  198. isStart: false,
  199. deviceId: that.data.connectedDeviceId,
  200. name: that.data.name,
  201. });
  202. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  203. clearTimeout(errTi);
  204. clearInterval(percentIn);
  205. },
  206. })