setWifi.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // pages/setWifi/setWifi.js
  2. const { BtHelper } = require("../../devices/bt_helper");
  3. Page({
  4. data: {
  5. wifiName: '',
  6. wifiPassword: '',
  7. _otaUrl: "",
  8. },
  9. getConnectedWifi: function () {
  10. const that = this;
  11. wx.getConnectedWifi({
  12. success: function (res) {
  13. const wifiName = res.wifi.SSID;
  14. that.setData({
  15. wifiName: wifiName
  16. });
  17. },
  18. fail: function (err) {
  19. console.error('获取Wi-Fi信息失败', err);
  20. // wx.showToast({
  21. // title: '获取Wi-Fi信息失败',
  22. // icon: 'none'
  23. // });
  24. }
  25. });
  26. },
  27. onWifiNameInput: function (e) {
  28. this.setData({
  29. wifiName: e.detail.value
  30. });
  31. },
  32. onWifiPasswordInput: function (e) {
  33. this.setData({
  34. wifiPassword: e.detail.value
  35. });
  36. },
  37. // string转换为List<int>
  38. string2ListInt(text) {
  39. let code = Array.from(text).map(char => char.charCodeAt(0));
  40. console.log("string转换为List<int>", code)
  41. return code
  42. },
  43. sendWiFiInfo(wifiName, pwd) {
  44. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  45. if (!wifiName || !pwd) {
  46. wx.showToast({
  47. title: '请输入正确的账号密码',
  48. })
  49. wx.hideLoading()
  50. return;
  51. }
  52. let result = [];
  53. // 字母*6 +
  54. let wifiList = this.string2ListInt(wifiName);
  55. // 数字*3 +
  56. let pwdList = this.string2ListInt(pwd);
  57. // 16进制
  58. result.push(0x22);
  59. result.push(this.int2Hex(wifiList.length + pwdList.length + 6));
  60. // 账号
  61. result.push(0x33);
  62. result.push(this.int2Hex(wifiList.length));
  63. let p = result[3] + 4;
  64. let j = 0;
  65. for (let i = 4; i < p; i++) {
  66. result.splice(i, 0, wifiList[j++]);
  67. }
  68. // 密码
  69. result.splice(p, 0, 0x44);
  70. result.splice(++p, 0, this.int2Hex(pwdList.length));
  71. p++;
  72. j = 0;
  73. // for (let i = p; i < p + pwdList.length; i++) {
  74. // result.splice(i, 0, pwdList[j++]);
  75. // }
  76. result.push(...pwdList)
  77. console.log("发送wifi账号密码:", result.toString());
  78. // _ble.send({ cmd: result });
  79. BtHelper.getInstance().send(result)
  80. },
  81. onConfirm: function () {
  82. const { wifiName, wifiPassword } = this.data;
  83. if (!wifiName || !wifiPassword) {
  84. wx.showToast({
  85. title: '请输入完整的Wi-Fi信息',
  86. icon: 'none'
  87. });
  88. return;
  89. }
  90. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  91. this.sendWiFiInfo(wifiName, wifiPassword);
  92. },
  93. addNotification() {
  94. let _this = this;
  95. EventManager.addNotification(CmdEvent.eventName, function (event) {
  96. let name = event.cmdEvent;
  97. console.log("OTA页0:", event)
  98. let kind = event.heiJiaoKind;
  99. switch (name) {
  100. case EnumCmdEvent.otaCmd:
  101. let otaCmd = event.otaCmd;
  102. console.log("OTA页:", otaCmd, kind)
  103. if (otaCmd === 1 && kind == 1) {
  104. wx.hideLoading();
  105. // 设备收到开启OTA的回复,发送url
  106. _this.sendUrlData()
  107. } else if (otaCmd === 2 && kind == 1) {
  108. wx.hideLoading();
  109. // 去设置wifi界面
  110. _this.goToWifi()
  111. }
  112. else if (otaCmd === 0 && kind == 1) {
  113. // 设备回收到url,OTA结束了
  114. // _this.sendOtaCmd(0)
  115. wx.hideLoading()
  116. wx.showModal({
  117. title: '等待设备升级中',
  118. showCancel: false
  119. })
  120. } else if (kind == 0) {
  121. wx.hideLoading()
  122. wx.showModal({
  123. title: 'WIFI连接失败了',
  124. showCancel: false
  125. })
  126. }
  127. break;
  128. case EnumCmdEvent.otaUrl:
  129. let otaUrl = event.otaUrl;
  130. if (otaUrl === 1) {
  131. // 开始发送url
  132. BtHelper.getInstance().otaUrl(_this.string2ListInt(_this.data.otaData.url))
  133. } else {
  134. wx.hideLoading()
  135. // wifi失败
  136. wx.showModal({
  137. title: 'OTA升级失败了',
  138. showCancel: false
  139. })
  140. }
  141. break;
  142. }
  143. }, _this)
  144. },
  145. onLoad: function (options) {
  146. let param = options.param;
  147. let url = JSON.parse(param).url ?? "";
  148. this.data._otaUrl = url;
  149. this.getConnectedWifi();
  150. this.addNotification()
  151. },
  152. onUnload: function () {
  153. EventManager.removeNotification(CmdEvent.eventName)
  154. },
  155. });