deviceConnect3.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. that.setData({
  96. percent: 100,
  97. ruterStatus: 1,
  98. });
  99. // 记住密码
  100. wx.setStorage({
  101. key: "wifiInfo",
  102. data: JSON.stringify({
  103. "password": app.globalData.pwdData,
  104. "ssid": app.globalData.ssid
  105. }),
  106. });
  107. var deviceId = that.data.name;
  108. var device = {
  109. connectType: 3,
  110. devName: "",
  111. state: "online",
  112. name: deviceId,
  113. deviceId: deviceId,
  114. mac: deviceId,
  115. imageUrl: "./../../img/min.png",
  116. }
  117. getCurrentPages()[0].addWifiDevice(device);
  118. }
  119. }
  120. break;
  121. ///设备连接状态回调
  122. case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
  123. that.setData({
  124. connected: options.result
  125. });
  126. if (!options.result) {
  127. wx.showModal({
  128. title: '很抱歉提醒你!',
  129. content: '小程序与设备异常断开',
  130. showCancel: false, //是否显示取消按钮
  131. success: function (res) {
  132. wx.navigateBack({
  133. delta: 1
  134. });
  135. },
  136. })
  137. }
  138. break;
  139. }
  140. },
  141. ///校验wifi账号密码
  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. var that = this;
  164. that.setData({
  165. password: e.detail.value
  166. })
  167. },
  168. bindCustomDataInput: function (e) {
  169. var that = this;
  170. that.setData({
  171. customData: e.detail.value
  172. })
  173. },
  174. egen() {
  175. wx.navigateBack({
  176. delta: 1
  177. })
  178. },
  179. goIndex() {
  180. route_util.goBackRoute(route_constant.indexRoot);
  181. },
  182. onUnload: function () {
  183. // 关闭蓝牙连接
  184. var that = this
  185. xBlufi.notifyConnectBle({
  186. isStart: false,
  187. deviceId: that.data.connectedDeviceId,
  188. name: that.data.name,
  189. });
  190. xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
  191. clearTimeout(errTi);
  192. clearInterval(percentIn);
  193. },
  194. })