setWifi.js 3.2 KB

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