deviceConnect3.js 5.0 KB

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