"use strict";
var common_vendor = require("../../common/vendor.js");
var utils_bluetooth = require("../../utils/bluetooth.js");
const _sfc_main = {
  setup(__props) {
    const deviceId = common_vendor.ref("");
    common_vendor.onLoad((option) => {
      deviceId.value = option.deviceId;
    });
    const ssid = common_vendor.ref("");
    const rememberWifi = common_vendor.ref(false);
    const password = common_vendor.ref("");
    const onCheckboxChange = (event) => {
      rememberWifi.value = !rememberWifi.value;
    };
    common_vendor.ref("");
    const serviceUUIDSamll = common_vendor.ref("");
    const notifyCharacteristicId = common_vendor.ref("");
    const writeCharacteristicId = common_vendor.ref("");
    const stringToBytes = (str) => {
      var array = new Uint8Array(str.length);
      for (var i = 0, l = str.length; i < l; i++) {
        array[i] = str.charCodeAt(i);
      }
      console.log(array);
      return array.buffer;
    };
    const connect = async () => {
      ssid.value = ssid.value.trim();
      password.value = password.value.trim();
      if (ssid.value === "") {
        wx.showToast({
          title: "WiFi\u4FE1\u606F\u83B7\u53D6\u5931\u8D25",
          icon: "none",
          duration: 2e3
        });
        return;
      }
      if (password.value === "") {
        wx.showToast({
          title: "\u8BF7\u8F93\u5165\u5BC6\u7801",
          icon: "none",
          duration: 2e3
        });
        return;
      }
      if (rememberWifi.value) {
        common_vendor.index.setStorage({
          key: ssid.value,
          data: password.value
        });
      } else {
        common_vendor.index.removeStorage({
          key: ssid.value
        });
      }
      res = await utils_bluetooth.getBLEDeviceServices(that.data.deviceId);
      for (let i2 = 0; i2 < res.services.length; i2++) {
        if (res.services[i2].uuid.toUpperCase().indexOf(serviceUUIDSamll.value) != -1) {
          console.log("\u670D\u52A1\u5DF2\u627E\u5230");
          serviceId.value = res.services[i2].uuid;
          break;
        }
      }
      console.log("\u5DF2\u83B7\u53D6\u8FDE\u63A5\u8BBE\u5907\u7684service\u670D\u52A1");
      res = await utils_bluetooth.getBLEDeviceCharacteristics(deviceId.value, serviceId.value);
      for (var i = 0; i < res.characteristics.length; i++) {
        console.log("\u83B7\u53D6\u5230\u7684\u7279\u5F81\u503Cid:" + res.characteristics[i].uuid);
        if (res.characteristics[i].properties.notify) {
          console.log("\u5F00\u542Fnotify\u7684characteristicId:" + res.characteristics[i].uuid);
          notifyCharacteristicId.value = res.characteristics[i].uuid;
        }
        if (res.characteristics[i].properties.write) {
          console.log("\u5F00\u542Fwrite\u7684characteristicId:" + res.characteristics[i].uuid);
          writeCharacteristicId.value = res.characteristics[i].uuid;
        }
      }
      console.log("\u5DF2\u83B7\u53D6\u8FDE\u63A5\u8BBE\u5907\u5177\u6709\u8BFB\u5199\u529F\u80FD\u670D\u52A1\u7684\u6240\u6709\u7279\u5F81\u503C");
      res = await utils_bluetooth.notifyBLECharacteristicValueChange(deviceId.value, serviceId.value, notifyCharacteristicId.value);
      console.log("\u5DF2\u542F\u52A8\u84DD\u7259\u7279\u5F81\u503C\u53D8\u5316");
      const data = {
        type: 0,
        data: {
          ssid: ssid.value,
          passwd: password.value
        }
      };
      let buffer = stringToBytes(JSON.stringify(data));
      res = await utils_bluetooth.writeBLECharacteristicValue(deviceId.value, serviceId.value, writeCharacteristicId.value, buffer);
      console.log("\u5DF2\u53D1\u9001\u6570\u636E");
      res = await utils_bluetooth.closeBluetoothAdapter();
    };
    common_vendor.onMounted(() => {
      wx.startWifi({
        success(res2) {
          wx.getConnectedWifi({
            partialInfo: true,
            success(res3) {
              ssid.value = res3.wifi.SSID;
              common_vendor.index.getStorage({
                key: ssid.value,
                success: function(res4) {
                  password.value = res4.data;
                }
              });
            },
            fail(res3) {
              wx.showToast({
                title: "WiFi\u4FE1\u606F\u83B7\u53D6\u5931\u8D25",
                icon: "none",
                duration: 2e3
              });
              return;
            }
          });
        },
        fail() {
          wx.showToast({
            title: "WiFi\u6A21\u5757\u521D\u59CB\u5316\u5931\u8D25",
            icon: "none",
            duration: 2e3
          });
          return;
        }
      });
    });
    return (_ctx, _cache) => {
      return {
        a: common_vendor.t(ssid.value),
        b: password.value,
        c: common_vendor.o(($event) => password.value = $event.detail.value),
        d: common_vendor.o(onCheckboxChange),
        e: common_vendor.n(rememberWifi.value ? "radio iconfont icon-gou" : "radio"),
        f: common_vendor.o(connect)
      };
    };
  }
};
var MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-1665ba40"], ["__file", "C:/Users/bzm15/Desktop/ix/mini_web-develop/pages/bluetooth/connect.vue"]]);
wx.createPage(MiniProgramPage);
