setWifi.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // pages/setWifi/setWifi.js
  2. const { BtHelper } = require("../../devices/bt_helper");
  3. import EventManager from '../../utils/event_bus'
  4. import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
  5. import store from '../../utils/store';
  6. import { BtCmd } from '../../devices/bluetooth/bt_cmd';
  7. Page({
  8. data: {
  9. wifiName: '',
  10. wifiPassword: '',
  11. _otaUrl: "",
  12. },
  13. getConnectedWifi: function () {
  14. const that = this;
  15. let wifiDic = store.getStore("wifiInfo")
  16. let wifiName = wifiDic.wifiName
  17. let pwd = wifiDic.wifiPassword
  18. if (wifiName && pwd) {
  19. console.log("获取Wi-Fi信息", wifiName, pwd);
  20. that.setData({
  21. wifiName: wifiName,
  22. wifiPassword: pwd
  23. });
  24. return;
  25. }
  26. wx.startWifi({
  27. success(res) {
  28. console.log(res.errMsg)
  29. wx.getConnectedWifi({
  30. success: function (res) {
  31. const wifiName = res.wifi.SSID;
  32. that.setData({
  33. wifiName: wifiName
  34. });
  35. },
  36. fail: function (err) {
  37. console.error('获取Wi-Fi信息失败', err);
  38. // wx.showToast({
  39. // title: '获取Wi-Fi信息失败',
  40. // icon: 'none'
  41. // });
  42. }
  43. });
  44. }
  45. })
  46. },
  47. onWifiNameInput: function (e) {
  48. this.setData({
  49. wifiName: e.detail.value
  50. });
  51. },
  52. onWifiPasswordInput: function (e) {
  53. this.setData({
  54. wifiPassword: e.detail.value
  55. });
  56. },
  57. sendWiFiInfo(wifiName, pwd) {
  58. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  59. if (!wifiName || !pwd) {
  60. wx.showToast({
  61. title: '请输入正确的账号密码',
  62. })
  63. wx.hideLoading()
  64. return;
  65. }
  66. let result = [];
  67. // 字母*6 +
  68. let wifiList = BtCmd.stringToUint8Array(wifiName);
  69. // 数字*3 +
  70. let pwdList = BtCmd.stringToUint8Array(pwd);
  71. console.log("wifiList", wifiList, "pwdList", pwdList)
  72. // 16进制
  73. result.push(0x22);
  74. result.push(BtCmd.intToHex(wifiList.length + pwdList.length + 6));
  75. // 账号
  76. result.push(0x33);
  77. result.push(BtCmd.intToHex(wifiList.length));
  78. let p = result[3] + 4;
  79. let j = 0;
  80. for (let i = 4; i < p; i++) {
  81. result.splice(i, 0, wifiList[j++]);
  82. }
  83. // 密码
  84. result.splice(p, 0, 0x44);
  85. result.splice(++p, 0, BtCmd.intToHex(pwdList.length));
  86. p++;
  87. j = 0;
  88. for (let i = p; i < p + pwdList.length; i++) {
  89. result.splice(i, 0, pwdList[j++]);
  90. }
  91. // result.push(...pwdList)
  92. console.log("发送wifi账号密码:", result.toString());
  93. // _ble.send({ cmd: result });
  94. BtHelper.getInstance().otaSetWifi(result)
  95. },
  96. onConfirm: function () {
  97. const { wifiName, wifiPassword } = this.data;
  98. if (!wifiName || !wifiPassword) {
  99. wx.showToast({
  100. title: '请输入完整的Wi-Fi信息',
  101. icon: 'none'
  102. });
  103. return;
  104. }
  105. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  106. this.sendWiFiInfo(wifiName, wifiPassword);
  107. },
  108. addNotification() {
  109. let _this = this;
  110. EventManager.addNotification(CmdEvent.eventName, function (event) {
  111. let name = event.cmdEvent;
  112. console.log("设置wifi0:", event)
  113. let kind = event.heiJiaoKind;
  114. console.log("设置wifi:", name, otaCmd, kind)
  115. let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
  116. wx.showToast({
  117. title: toast,
  118. icon: 'none'
  119. })
  120. if (name === EnumCmdEvent.otaWifi && value === 1) {
  121. store.setStore("wifiInfo", {
  122. wifiName: _this.data.wifiName,
  123. wifiPassword: _this.data.wifiPassword
  124. })
  125. }
  126. }, _this)
  127. },
  128. onLoad: function (options) {
  129. let param = options.param ?? "{}";
  130. let url = JSON.parse(param).url ?? "";
  131. this.data._otaUrl = url;
  132. this.getConnectedWifi();
  133. this.addNotification()
  134. },
  135. onUnload: function () {
  136. EventManager.removeNotification(CmdEvent.eventName)
  137. },
  138. });