deviceConnect3.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 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. },
  27. onLoad: function (options) {
  28. var that = this;
  29. that.setData({
  30. name: options.name,
  31. connectedDeviceId: options.deviceId,
  32. })
  33. xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
  34. ///通知初始化获取设备的服务列表等信息
  35. xBlufi.notifyInitBleEsp32({
  36. deviceId: options.deviceId,
  37. });
  38. percentIn = setInterval(() => {
  39. if (that.data.percent === 100) {
  40. clearInterval(percentIn);
  41. ///失败
  42. that.setData({
  43. ruterStatus: 2
  44. });
  45. return;
  46. };
  47. that.data.percent++;
  48. that.setData({
  49. percent: that.data.percent
  50. });
  51. }, 600);
  52. },
  53. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB","characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  54. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  55. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  56. funListenDeviceMsgEvent: function (options) {
  57. var that = this;
  58. switch (options.type) {
  59. // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB",
  60. // "characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}}
  61. case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
  62. wx.hideLoading();
  63. if (options.result) {
  64. console.log('初始化成功');
  65. that.OnClickStart();
  66. } else {
  67. console.log('初始化失败')
  68. that.setData({
  69. connected: false
  70. })
  71. that.setData({
  72. ruterStatus: 2
  73. });
  74. }
  75. break;
  76. // {"type":"4","result":false,"data":{"progress":0,"ssid":""}}
  77. // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}}
  78. ///连接结果
  79. case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
  80. wx.hideLoading();
  81. if (!options.result)
  82. errTi = setTimeout(() => {
  83. that.setData({
  84. ruterStatus: 2
  85. });
  86. clearTimeout(errTi);
  87. clearInterval(percentIn);
  88. }, 5000);
  89. else {
  90. // 配网成功
  91. if (options.data.progress == 100) {
  92. clearTimeout(errTi);
  93. clearInterval(percentIn);
  94. // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
  95. getCurrentPages()[0].addWifiDevice({
  96. deviceId: that.data.name,
  97. });
  98. that.setData({
  99. percent: 100,
  100. ruterStatus: 1
  101. });
  102. // 记住密码
  103. wx.setStorage({
  104. key: "wifiInfo",
  105. data: JSON.stringify({
  106. "password": app.globalData.pwdData,
  107. "ssid": app.globalData.ssid
  108. })
  109. });
  110. }
  111. }
  112. break;
  113. ///设备连接状态回调
  114. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  115. that.setData({
  116. connected: options.result
  117. });
  118. if (!options.result) {
  119. wx.showModal({
  120. title: '很抱歉提醒你!',
  121. content: '小程序与设备异常断开',
  122. showCancel: false, //是否显示取消按钮
  123. success: function (res) {
  124. wx.navigateBack({
  125. delta: 1
  126. });
  127. },
  128. })
  129. }
  130. break;
  131. }
  132. },
  133. ///校验wifi账号密码
  134. OnClickStart: function () {
  135. if (!app.globalData.ssid) {
  136. wx.showToast({
  137. title: 'SSID不能为空',
  138. icon: 'none'
  139. })
  140. return
  141. }
  142. if (!app.globalData.pwdData) {
  143. wx.showToast({
  144. title: '密码不能为空',
  145. icon: 'none'
  146. })
  147. return;
  148. }
  149. xBlufi.notifySendRouterSsidAndPassword({
  150. ssid: app.globalData.ssid,
  151. password: app.globalData.pwdData
  152. });
  153. },
  154. bindPasswordInput: function (e) {
  155. var that = this;
  156. that.setData({
  157. password: e.detail.value
  158. })
  159. },
  160. bindCustomDataInput: function (e) {
  161. var that = this;
  162. that.setData({
  163. customData: e.detail.value
  164. })
  165. },
  166. egen() {
  167. wx.navigateBack({
  168. delta: 1
  169. })
  170. },
  171. goIndex() {
  172. route_util.goBackRoute(route_constant.indexRoot);
  173. },
  174. onUnload: function () {
  175. // 关闭蓝牙连接
  176. var that = this
  177. xBlufi.notifyConnectBle({
  178. isStart: false,
  179. deviceId: that.data.connectedDeviceId,
  180. name: that.data.name,
  181. });
  182. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  183. clearTimeout(errTi);
  184. clearInterval(percentIn);
  185. },
  186. })