deviceConnect3.js 6.1 KB

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