deviceConnect3.js 5.9 KB

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