setWifi.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = BtCmd.sendWiFiInfo(wifiName, pwd)
  67. // _ble.send({ cmd: result });
  68. BtHelper.getInstance().otaSetWifi(result)
  69. },
  70. onConfirm: function () {
  71. const { wifiName, wifiPassword } = this.data;
  72. if (!wifiName || !wifiPassword) {
  73. wx.showToast({
  74. title: '请输入完整的Wi-Fi信息',
  75. icon: 'none'
  76. });
  77. return;
  78. }
  79. // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息
  80. this.sendWiFiInfo(wifiName, wifiPassword);
  81. },
  82. addNotification() {
  83. let _this = this;
  84. EventManager.addNotification(CmdEvent.eventName, function (event) {
  85. let name = event.cmdEvent;
  86. let otaCmd = event.otaCmd
  87. console.log("设置wifi0:", event)
  88. let kind = event.heiJiaoKind;
  89. console.log("设置wifi:", name, otaCmd, kind)
  90. let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind
  91. wx.showToast({
  92. title: toast,
  93. icon: 'none'
  94. })
  95. if (name === EnumCmdEvent.otaWifi && value === 1) {
  96. store.setStore("wifiInfo", {
  97. wifiName: _this.data.wifiName,
  98. wifiPassword: _this.data.wifiPassword
  99. })
  100. }
  101. }, _this)
  102. },
  103. onLoad: function (options) {
  104. let param = options.param ?? "{}";
  105. let url = JSON.parse(param).url ?? "";
  106. this.data._otaUrl = url;
  107. this.getConnectedWifi();
  108. this.addNotification()
  109. },
  110. onUnload() {
  111. EventManager.removeNotification(CmdEvent.eventName)
  112. },
  113. });