setWifi.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Page({
  6. data: {
  7. wifiName: '',
  8. wifiPassword: '',
  9. _otaUrl: "",
  10. },
  11. getConnectedWifi: function () {
  12. const that = this;
  13. wx.startWifi({
  14. success(res) {
  15. console.log(res.errMsg)
  16. wx.getConnectedWifi({
  17. success: function (res) {
  18. const wifiName = res.wifi.SSID;
  19. that.setData({
  20. wifiName: wifiName
  21. });
  22. },
  23. fail: function (err) {
  24. console.error('获取Wi-Fi信息失败', err);
  25. // wx.showToast({
  26. // title: '获取Wi-Fi信息失败',
  27. // icon: 'none'
  28. // });
  29. }
  30. });
  31. }
  32. })
  33. },
  34. onWifiNameInput: function (e) {
  35. this.setData({
  36. wifiName: e.detail.value
  37. });
  38. },
  39. onWifiPasswordInput: function (e) {
  40. this.setData({
  41. wifiPassword: e.detail.value
  42. });
  43. },
  44. // string转换为List<int>
  45. string2ListInt(text) {
  46. let code = Array.from(text).map(char => char.charCodeAt(0));
  47. console.log("string转换为List<int>", text, code)
  48. return code
  49. },
  50. _int2Hex(num) {
  51. let value = num ?? 0;
  52. let hexStr = value.toString(16).toUpperCase();
  53. hexStr = hexStr.padStart(2, '0');
  54. let hexNum = parseInt(hexStr, 16);
  55. return hexNum;
  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 = this.string2ListInt(wifiName);
  69. // 数字*3 +
  70. let pwdList = this.string2ListInt(pwd);
  71. console.log("wifiList", wifiList, "pwdList", pwdList)
  72. // 16进制
  73. result.push(0x22);
  74. result.push(this._int2Hex(wifiList.length + pwdList.length + 6));
  75. // 账号
  76. result.push(0x33);
  77. result.push(this._int2Hex(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, this._int2Hex(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. }, _this)
  121. },
  122. onLoad: function (options) {
  123. let param = options.param ?? "{}";
  124. let url = JSON.parse(param).url ?? "";
  125. this.data._otaUrl = url;
  126. this.getConnectedWifi();
  127. this.addNotification()
  128. },
  129. onUnload: function () {
  130. EventManager.removeNotification(CmdEvent.eventName)
  131. },
  132. });