deviceConnect3.js 6.2 KB

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