Skip to content

使用方法

介绍

安卓wifi操作UTS原生插件集成了常用的wifi开关,wifi连接,扫描wifi,发送数据,wifi文件传输等多项功能,插件支持uniapp和uniapp x

插件说明

  • 如果您在使用插件的过程中有任何问题,可以联系作者,作者将全力协助您只用插件
  • 本文档同时提供了uniapp的用法示例和uniappx的用法示例,插件市场导入的示例项目仅为uniapp的用法示例,如果您需要uniappx的示例项目可以通过下方的链接下载示例

    https://pan.baidu.com/s/1uKFp9q7h7RiS3xpNyToivg?pwd=g4d8

联系作者

关注微信公众号可联系作者

插件地址

https://ext.dcloud.net.cn/plugin?id=28095

演示程序

扫描下方二维码下载演示程序(目前仅提供apk)

uniapp版本

uniappx版本

权限

  • android.permission.ACCESS_WIFI_STATE
  • android.permission.CHANGE_WIFI_STATE
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.WRITE_EXTERNAL_STORAGE
  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.FOREGROUND_SERVICE
  • android.permission.FOREGROUND_SERVICE_DATA_SYNC
  • android.permission.MANAGE_EXTERNAL_STORAGE

用法

在需要使用插件的页面加载以下代码

js
import * as module from "@/uni_modules/leven-uts-wifi"
js
import * as module from "@/uni_modules/leven-uts-wifi"

页面内容

vue
<template>
  <view class="content">
    <view style="display: flex; flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text>wifi开关</text></view>
      <view>
        <switch :checked="isChecked" style="transform:scale(0.7)" @change="changeWifi"></switch>
      </view>
    </view>
    <view style="margin-bottom: 8px;"><text>{{connectedWifi}}</text></view>
    <view style="margin-bottom: 8px;"><text>{{localIp}}</text></view>
    <view style="display: flex; flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text>目标ip:</text></view>
      <view><uni-easyinput v-model="targetIp"></uni-easyinput></view>
    </view>
    <view style="display: flex; flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text>发送内容:</text></view>
      <view><uni-easyinput v-model="textInput" placeholder="请输入发送的文本"></uni-easyinput></view>
    </view>
    <view style="display: flex; flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view style="margin-right: 8px;"><button type="primary" size="mini" @click="startScan">扫描wifi</button></view>
      <view style="margin-right: 8px;"><button type="primary" size="mini" @click="sendText">发送文本</button></view>
      <view><button type="primary" size="mini" @click="sendFile">发送文件</button></view>
    </view>
    <!-- 设备列表 -->
    <view style="display: flex; flex-direction: column; border: 1px solid #F2F2F2;">
      <view style="height: 40px; display: flex; align-items: center; background-color: #F2F2F2; padding: 0 8px;">
        <text style="font-size: 16px;">附近wifi列表</text>
      </view>
      <scroll-view class="scroll-Y" scroll-y="true" direction="vertical">
        <view v-for="(item, index) in wifiList" :key="index"
          style="display: flex; flex-direction: row; justify-content: space-between; align-items: center; padding: 8px; font-size: 12px;">
          <view>
            <view>{{item.SSID}}</view>
          </view>
          <view><button type="primary" size="mini" @click="connect(item)">连接</button></view>
        </view>
      </scroll-view>
    </view>
    <!-- 日志 -->
    <view style="display: flex; flex-direction: column; border: 1px solid #F2F2F2;">
      <view style="height: 40px; display: flex; align-items: center; background-color: #F2F2F2; padding: 0 8px;">
        <text style="font-size: 16px;">运行日志</text>
      </view>
      <scroll-view class="scroll-Y" scroll-y="true" direction="vertical">
        <view v-for="(item, index) in logs" :key="index" style=" padding: 8px; font-size: 12px; word-wrap: break-word;">
          <view>{{item}}</view>
        </view>
      </scroll-view>
    </view>

    <!-- 接收文件请求弹窗 -->
    <uni-popup ref="refFileRequestPop" background-color="#fff" border-radius="8px">
      <view style=" width: 600rpx;">
        <!-- 标题 -->
        <view
          style="height: 40px; border-bottom: 1px solid #F2F2F2; display: flex; flex-direction: row; justify-content: center; align-items: center;">
          接收文件请求</view>
        <!-- 内容 -->
        <view style="padding: 8px;">
          <view>文件名:{{sendFileInfo.fileName}}</view>
          <view>文件大小:{{getFormatFileSize(sendFileInfo.fileSize)}}</view>
        </view>
        <!-- 按钮 -->
        <view
          style="display: flex; flex-direction: row; justify-content: space-between; align-items: center; padding: 8px 8px 16px 8px;">
          <button type="primary" size="mini" @click="confirmFileReceive(false)">拒绝</button>
          <button type="primary" size="mini" @click="confirmFileReceive(true)">接受</button>
        </view>
      </view>
    </uni-popup>

    <!-- 文件发送或接收进度弹窗 -->
    <uni-popup ref="refFileProgressPop" background-color="#fff" border-radius="8px" :isMaskClick="false">
      <view style="width: 600rpx;">
        <!-- 标题 -->
        <view
          style="height: 40px; border-bottom: 1px solid #F2F2F2; display: flex; flex-direction: row; justify-content: center; align-items: center; margin-bottom: 16px;">
          {{filePop.title}}
        </view>
        <!-- 进度条 -->
        <view style="padding: 8px;">
          <view style="border-radius: 10px; overflow: hidden;">
            <progress :percent="filePop.percent" :stroke-width="20"></progress>
          </view>
        </view>
        <!-- 进度信息展示 -->
        <view
          style="display: flex; flex-direction: row; justify-content: space-between; align-items: center; padding: 8px;">
          <view style="font-size: 14px;">{{getFormatFileSize(filePop.size)}} / {{getFormatFileSize(filePop.totalSize)}}
          </view>
          <view style="font-size: 14px;">{{filePop.percent}}%</view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
  import * as module from "@/uni_modules/leven-uts-wifi"
  import {
    formatFileSize
  } from "@/utils/index.js"
  export default {
    data() {
      return {
        //wifi是否打开
        isChecked: false,
        //当前连接信息
        connectedWifi: "当前连接:未连接",
        //本机ip
        localIp: "本机IP: 等待获取...",
        //目标ip地址
        targetIp: "192.168.1.100",
        //发送的文本内容
        textInput: "",
        //附近wifi列表
        wifiList: [],
        //运行日志
        logs: [],
        //收到文件发送请求数据
        sendFileInfo: {},
        //发送或接收文件
        filePop: {
          //标题
          title: "文件接收中...",
          //进度
          percent: 0,
          //已接收/发送的大小
          size: 0,
          //文件总大小
          totalSize: 0
        },
        //文件弹窗是否展示
        filePopIsOpen: false,
        //是否是发送端
        isSendDevice: false,
        //服务是否已开启
        serviceConnect: false
      }
    },
    onUnload() {
      module.release({
        complete: (res) => {
          console.log("释放资源" + JSON.stringify(res))
        }
      })
    },
    mounted() {
      this.$nextTick(() => {
        this.requestPermissions();
      })
    },
    methods: {
      //展示消息
      showMessage(message) {
        uni.showToast({
          title: message,
          icon: "none"
        })
      },
      getFormatFileSize(size) {
        return formatFileSize(size)
      },
      changeWifi(data) {
        console.log(data.detail)
        let value = data.detail.value;
        module.setWifiEnabled({
          params: {
            enable: value
          },
          complete: (res) => {
            this.logs.push(JSON.stringify(res))
          }
        })
      },
      //连接wifi
      connect(item) {
        module.connect({
          params: {
            ssid: item.SSID,
            password: ""
          },
          complete: (res) => {
            console.log(res)
          }
        })
      },
      //发送文件
      sendFile() {
        if (this.targetIp == "") {
          this.showMessage("请输入目标ip");
          return
        }
        this.isSendDevice = true;
        module.sendSelectFile({
          params: {
            ip: this.targetIp
          },
          complete: (res) => {
            console.log(res)
          }
        })
      },
      //发送文本
      sendText() {
        if (this.textInput == "" || this.targetIp == "") {
          this.showMessage("请输入目标ip和发送的文本");
          return
        }
        module.sendText({
          params: {
            text: this.textInput,
            ip: this.targetIp
          },
          complete: (res) => {
            console.log(res)
          }
        })
      },
      //确认接受文件
      confirmFileReceive(accept) {
        this.isSendDevice = false
        module.confirmFileReceive({
          params: {
            accept: accept
          },
          complete: (res) => {
            this.$refs.refFileRequestPop.close();
          }
        })
      },
      //扫描wifi
      startScan() {
        module.startScan({
          complete: (res) => {
            console.log("扫描wifi:" + JSON.stringify(res))
          }
        })
      },
      //获取本机ip
      getLocalIpAddress() {
        if (this.isChecked && this.serviceConnect) {
          module.getLocalIpAddress({
            complete: (res) => {
              if (res.code == 0) {
                let data = res.data;
                let ip = data.ip;
                this.localIp = "本机IP:" + ip;
              } else {
                this.localIp = "本机IP: 等待获取...";
                this.showMessage("获取本机ip失败:" + res.message)
              }
            }
          })
        }
      },
      //初始化wifi
      initWifi() {
        console.log("初始化wifi")
        module.init({
          params: {
            //前台服务通知标题
            notificationTitle: "安卓WIFI操作UTS原生插件",
            //前台服务通知内容
            notificationContent: "正在监听插件的数据传输...",
            //前台服务通知描述
            notificationDes: "用于接收插件数据传输的通知通道",
            //接收文件的保存目录
            // receiveFileDir:""
          },
          complete: (res) => {
            console.log(res)
            if (res.code == 0) {
              let data = res.data || {};
              let type = data.type || "";
              if (type == "onScan") {
                //扫描结果
                let scanResults = data.scanResults || [];
                this.wifiList = scanResults.map(item => {
                  return {
                    SSID: item.SSID
                  }
                });

              } else if (type == "onWifiState") {
                //wifi状态
                let state = data.state;
                switch (state) {
                  case "WIFI_STATE_ENABLED":
                    //wifi已开启
                    this.isChecked = true;
                    break
                  case "WIFI_STATE_DISABLED":
                    //wifi已开启
                    this.isChecked = false;
                    this.wifiList = [];
                    break
                }
              } else if (type == "onServiceConnected") {
                //服务已连接,此时可以进行wifi操作
                this.serviceConnect = true;
                //获取本机ip
                this.getLocalIpAddress();
              } else if (type == "onWifiConnectState") {
                //wifi已连接
                let state = data.state;
                let wifiInfo = data.wifiInfo;
                if (state) {
                  this.logs.push("wifi已连接");
                  this.logs.push("wifi信息:" + JSON.stringify(wifiInfo))
                  this.connectedWifi = "当前连接:" + wifiInfo.SSID;
                  //获取本机ip
                  this.getLocalIpAddress();
                } else {
                  this.localIp = "本机IP: 等待获取...";
                  this.connectedWifi = "当前连接:未连接";
                  this.logs.push("wifi已断开")
                  this.logs.push("wifi信息:" + JSON.stringify(wifiInfo))
                }
              } else if (type == "onTextReceived") {
                //收到发送的消息
                let text = data.text;
                this.showMessage("收到消息:" + text)
              } else if (type == "onReceiveRequest") {
                //收到文件请求
                this.sendFileInfo = data;
                this.$refs.refFileRequestPop.open()
              } else if (type == "onTransferProgress") {
                //传输进度
                if (!this.filePopIsOpen) {
                  this.$refs.refFileProgressPop.open();
                  this.filePopIsOpen = true;
                }
                this.filePop.percent = ((data.transferredSize / data.totalSize) * 100).toFixed(2);
                this.filePop.size = data.transferredSize;
                this.filePop.totalSize = data.totalSize;
              } else if (type == "onTransferComplete") {
                //传输完成
                this.$refs.refFileProgressPop.close();
                this.filePopIsOpen = false;
                let success = data.success;
                if (success) {
                  if (!this.isSendDevice) {
                    //文件路径
                    let path = data.path;
                    this.showMessage("文件保存路径:" + path)
                  } else {
                    this.showMessage("发送完成")
                  }
                } else {
                  let message = data.message;
                  this.showMessage("传输出错:" + message)
                }
                this.isSendDevice = false
              }
            }
          }
        })
      },
      //跳转到所有文件访问权限页面
      toAllFilesPermissionPage() {
        module.toAllFilesPermissionPage({
          complete: (res) => {
            if (res.code == 0) {
              let data = res.data;
              if (!data.status) {
                this.showMessage("所有文件访问权限未打开")
              } else {
                this.initWifi();
              }
            } else {
              this.showMessage("跳转到所有文件访问权限页面失败")
            }
          }
        })
      },
      //检测是否有所有文件访问权限
      checkAllFilesPermission() {
        module.checkAllFilesPermission({
          complete: (res) => {
            if (res.code == 0) {
              let data = res.data;
              if (!data.status) {
                this.toAllFilesPermissionPage();
              } else {
                this.initWifi();
              }
            } else {
              this.showMessage("查询文件权限失败")
            }
          }
        })
      },
      requestPermissions() {
        //申请权限
        module.requestPermissions({
          params: {
            permissions: [
              'android.permission.ACCESS_FINE_LOCATION',
              'android.permission.READ_EXTERNAL_STORAGE',
              'android.permission.WRITE_EXTERNAL_STORAGE'
            ]
          },
          complete: (res) => {
            if (res.code == 0) {
              this.checkAllFilesPermission()
            } else {
              this.showMessage("权限申请未通过")
            }
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .content {
    padding: 16px;

    text {
      font-size: 14px;
    }
  }

  .scroll-Y {
    height: 200px;
  }
</style>
vue
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1; padding: 8px;">
  <!-- #endif -->
    <view style="display: flex; flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text class="text-style">wifi开关</text></view>
      <view>
        <rice-switch v-model="isChecked" :custom-style="{transform:'scale(0.82)'}" @change="changeWifi"></rice-switch>
      </view>
    </view>
    <view style="margin-bottom: 8px;"><text class="text-style">{{connectedWifi}}</text></view>
    <view style="margin-bottom: 8px;"><text class="text-style">{{localIp}}</text></view>
    <view style="flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text class="text-style">目标ip:</text></view>
      <view><rice-input v-model="targetIp" placeholder="请输入目标ip地址"></rice-input></view>
    </view>
    <view style="flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view><text class="text-style">发送内容:</text></view>
      <view><rice-input v-model="textInput" placeholder="请输入发送的文本"></rice-input></view>
    </view>
    <view style="flex-direction: row; align-items: center; margin-bottom: 8px;">
      <view style="margin-right: 8px;"><button type="primary" size="mini" @click="startScan">扫描wifi</button></view>
      <view style="margin-right: 8px;"><button type="primary" size="mini" @click="sendText">发送文本</button></view>
      <view><button type="primary" size="mini" @click="sendFile">发送文件</button></view>
    </view>
    <!-- 设备列表 -->
    <view style="flex-direction: column; border: 1px solid #F2F2F2;">
      <view style="height: 40px; justify-content: center; background-color: #F2F2F2; padding: 0 8px;">
        <text style="font-size: 16px;">附近wifi列表</text>
      </view>
      <scroll-view direction="vertical" class="scroll-Y" :show-scrollbar="false">
        <view v-for="(item, index) in wifiList" :key="index"
          style="flex-direction: row; justify-content: space-between; align-items: center; padding: 8px;">
          <view>
            <view><text class="text-style">{{item.SSID}}</text></view>
          </view>
          <view><button type="primary" size="mini">连接</button></view>
        </view>
      </scroll-view>
    </view>
    <!-- 日志 -->
    <view style="flex-direction: column; border: 1px solid #F2F2F2;">
      <view style="height: 40px; justify-content: center; background-color: #F2F2F2; padding: 0 8px;">
        <text style="font-size: 16px;">日志</text>
      </view>
      <scroll-view class="scroll-Y" direction="vertical" :show-scrollbar="false">
        <view v-for="(item, index) in logs" :key="index" style="padding: 8px;">
          <text class="text-style">{{item}}</text>
        </view>
      </scroll-view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->

  <!-- 接收文件请求弹窗 -->
  <rice-popup v-model:show="fileRequestPopShow" position="center" :closeable="false" bgColor="#FFFFFF" radius="8px"
    :closeOnClickOverlay="false">
    <view class="popup-center">
      <!-- 标题 -->
      <view
        style="height: 40px; border-bottom: 1px solid #F2F2F2; flex-direction: row; justify-content: center; align-items: center;">
        <text class="text-title-style">接收文件请求</text>
      </view>
      <!-- 内容 -->
      <view style="padding: 8px;">
        <view><text class="text-style">文件名:{{sendFileInfo.fileName}}</text></view>
        <view><text class="text-style">文件大小:{{getFormatFileSize(sendFileInfo.fileSize)}}</text></view>
      </view>
      <!-- 按钮 -->
      <view
        style="flex-direction: row; justify-content: space-between; align-items: center; padding: 8px 8px 16px 8px;">
        <button type="primary" size="mini" @click="confirmFileReceive(false)">拒绝</button>
        <button type="primary" size="mini" @click="confirmFileReceive(true)">接受</button>
      </view>
    </view>
  </rice-popup>

  <!-- 文件发送或接收进度弹窗 -->
  <rice-popup v-model:show="fileProgressPopShow" position="center" :closeable="false" bgColor="#FFFFFF" radius="8px"
    :closeOnClickOverlay="false">
    <view class="popup-center">
      <!-- 标题 -->
      <view
        style="height: 40px; border-bottom: 1px solid #F2F2F2; flex-direction: row; justify-content: center; align-items: center; margin-bottom: 16px;">
        {{filePop.title}}
      </view>
      <!-- 进度条 -->
      <view style="padding: 8px;">
        <rice-progress :percentage="filePop.percent" :show-text="false" stroke-width="16px"></rice-progress>
      </view>
      <!-- 进度信息展示 -->
      <view style="flex-direction: row; justify-content: space-between; align-items: center; padding: 8px;">
        <view>
          <text class="text-style">{{getFormatFileSize(filePop.size)}} / {{getFormatFileSize(filePop.totalSize)}}</text>
        </view>
        <view><text class="text-style">{{filePop.percent}}%</text></view>
      </view>
    </view>
  </rice-popup>
</template>

<script setup>
  import * as module from "@/uni_modules/leven-uts-wifi"
  import { formatFileSize } from "@/utils/wifi/index.uts"
  import { LevenResult, LevenOptions } from "@/uni_modules/leven-uts-wifi"
  //设备列表属性
  type wifiItem = {
    //设备名称
    SSID ?: string
  }
  //文件属性
  type fileItem = {
    //文件名称
    fileName ?: string,
    //文件大小
    fileSize ?: number
  };
  //发送或接收文件属性
  type filePopItem = {
    //标题
    title : string,
    //进度
    percent : number,
    //已接收/发送的大小
    size : number,
    //文件总大小
    totalSize : number
  }
  //wifi是否打开
  const isChecked = ref(false)
  //当前连接信息
  const connectedWifi = ref("当前连接:未连接")
  //本机ip
  const localIp = ref("本机IP: 等待获取...")
  //目标ip地址
  const targetIp = ref("192.168.1.100")
  //发送的文本内容
  const textInput = ref("这是测试的内容")
  //wifi列表
  const wifiList = ref<wifiItem[]>([])
  //日志列表
  const logs = ref<string[]>([])
  //收到文件发送请求数据
  const sendFileInfo = ref<fileItem>({})
  //发送或接收文件
  const filePop = ref<filePopItem>({
    title: "文件接收中...",
    percent: 0,
    size: 0,
    totalSize: 0
  })
  //是否是发送端
  const isSendDevice = ref(false)
  //服务是否已开启
  const serviceConnect = ref(false)
  //接收文件请求弹窗
  const fileRequestPopShow = ref(false)
  //文件发送或接收进度弹窗
  const fileProgressPopShow = ref(false)

  //转换文件大小
  function getFormatFileSize(size : number | null) {
    return formatFileSize(size)
  }

  //显示消息
  function showMessage(message : string) {
    uni.showToast({
      title: message,
      icon: "none"
    })
  }

  function sendFile() {
    if (targetIp.value == "") {
      showMessage("请输入目标ip");
      return
    }
    isSendDevice.value = true;
    module.sendSelectFile({
      params: {
        ip: targetIp.value
      },
      complete: (res : LevenResult) => {
        console.log(res)
      }
    } as LevenOptions)
  }

  //拒绝或接受文件接收
  function confirmFileReceive(accept : boolean) {
    module.confirmFileReceive({
      params: {
        accept: accept
      },
      complete: (res : LevenResult) => {
        fileRequestPopShow.value = false
        console.log(res)
      }
    } as LevenOptions)
  }

  function changeWifi(value : boolean) {
    module.setWifiEnabled({
      params: {
        enable: value
      },
      complete: (res : LevenResult) => {
        logs.value.push(JSON.stringify(res))
      }
    } as LevenOptions)
  }

  //获取本机ip
  function getLocalIpAddress() {
    if (isChecked.value && serviceConnect.value) {
      module.getLocalIpAddress({
        complete: (res : LevenResult) => {
          if (res.code == 0) {
            let data = res.data;
            let ip = data.getString("ip");
            localIp.value = "本机IP:" + ip;
          } else {
            localIp.value = "本机IP: 等待获取...";
            showMessage("获取本机ip失败:" + res.message)
          }
        }
      } as LevenOptions)
    }
  }

  //发送文本
  function sendText() {
    if (textInput.value == "" || targetIp.value == "") {
      showMessage("请输入目标ip和发送的文本");
      return
    }
    module.sendText({
      params: {
        text: textInput.value,
        ip: targetIp.value
      },
      complete: (res : LevenResult) => {
        console.log(res)
      }
    } as LevenOptions)
  }

  //开始扫描
  function startScan() {
    module.startScan({
      complete: (res : LevenResult) => {
        console.log("扫描wifi:" + JSON.stringify(res))
      }
    } as LevenOptions)
  }

  //初始化wifi
  function initWifi() {
    console.log("初始化wifi")
    module.init({
      params: {
        //前台服务通知标题
        notificationTitle: "安卓WIFI操作UTS原生插件",
        //前台服务通知内容
        notificationContent: "正在监听插件的数据传输...",
        //前台服务通知描述
        notificationDes: "用于接收插件数据传输的通知通道",
        //接收文件的保存目录
        // receiveFileDir:""
      },
      complete: (res) => {
        console.log(res)
        if (res.code == 0) {
          let data = res.data;
          let type = data.getString("type");
          if (type == "onScan") {
            //扫描结果
            let scanResults : UTSJSONObject[] = data.getArray("scanResults") as UTSJSONObject[];
            wifiList.value = scanResults.map((item : UTSJSONObject) => {
              return {
                SSID: item.getString("SSID")
              } as wifiItem
            });
          } else if (type == "onWifiState") {
            //wifi状态
            let state = data.getString("state");
            switch (state) {
              case "WIFI_STATE_ENABLED":
                //wifi已开启
                isChecked.value = true;
                break
              case "WIFI_STATE_DISABLED":
                //wifi已开启
                isChecked.value = false;
                wifiList.value = [];
                break
            }
          } else if (type == "onServiceConnected") {
            //服务已连接,此时可以进行wifi操作
            serviceConnect.value = true;
            //获取本机ip
            getLocalIpAddress();
          } else if (type == "onWifiConnectState") {
            //wifi已连接
            let state = data.getBoolean("state")!;
            let wifiInfo = data.getJSON("wifiInfo");
            if (state) {
              logs.value.push("wifi已连接");
              logs.value.push("wifi信息:" + JSON.stringify(wifiInfo))
              connectedWifi.value = "当前连接:" + wifiInfo?.getString("SSID");
              //获取本机ip
              getLocalIpAddress();
            } else {
              localIp.value = "本机IP: 等待获取...";
              connectedWifi.value = "当前连接:未连接";
              logs.value.push("wifi已断开")
              logs.value.push("wifi信息:" + JSON.stringify(wifiInfo))
            }
          } else if (type == "onTextReceived") {
            //收到发送的消息
            let text = data.getString("text");
            showMessage("收到消息:" + text)
          } else if (type == "onReceiveRequest") {
            //收到文件请求
            sendFileInfo.value = {
              fileName: data.getString("fileName"),
              fileSize: data.getNumber("fileSize")
            } as fileItem;
            fileRequestPopShow.value = true
          } else if (type == "onTransferProgress") {
            //传输进度
            if (!fileProgressPopShow.value) {
              fileProgressPopShow.value = true
            }
            let transferredSize : number = data.getNumber("transferredSize")!
            let totalSize : number = data.getNumber("totalSize")!
            filePop.value.percent = parseFloat(((transferredSize / totalSize) * 100).toFixed(2));
            filePop.value.size = transferredSize;
            filePop.value.totalSize = totalSize;
          } else if (type == "onTransferComplete") {
            //传输完成
            fileProgressPopShow.value = false;
            let success = data.getBoolean("success")!;
            if (success) {
              if (!isSendDevice.value) {
                //文件路径
                let path = data.getString("path");
                showMessage("文件保存路径:" + path)
              } else {
                showMessage("发送完成")
              }
            } else {
              let message = data.getString("message");
              showMessage("传输出错:" + message)
            }
            isSendDevice.value = false
          }
        }
      }
    })
  }

  //跳转到所有文件访问权限页面
  function toAllFilesPermissionPage() {
    module.toAllFilesPermissionPage({
      complete: (res : LevenResult) => {
        if (res.code == 0) {
          let data = res.data;
          let status : boolean = data.getBoolean("status")!
          if (!status) {
            showMessage("所有文件访问权限未打开")
          } else {
            initWifi();
          }
        } else {
          showMessage("跳转到所有文件访问权限页面失败")
        }
      }
    } as LevenOptions)
  }

  //检测是否有所有文件访问权限
  function checkAllFilesPermission() {
    module.checkAllFilesPermission({
      complete: (res : LevenResult) => {
        if (res.code == 0) {
          let data = res.data;
          let status : boolean = data.getBoolean("status")!
          if (!status) {
            toAllFilesPermissionPage();
          } else {
            initWifi();
          }
        } else {
          showMessage("查询文件权限失败")
        }
      }
    } as LevenOptions)
  }

  //申请权限
  function requestPermissions() {
    module.requestPermissions({
      params: {
        permissions: [
          'android.permission.ACCESS_FINE_LOCATION',
          'android.permission.READ_EXTERNAL_STORAGE',
          'android.permission.WRITE_EXTERNAL_STORAGE'
        ]
      },
      complete: (res : LevenResult) => {
        console.log(res)
        if (res.code == 0) {
          checkAllFilesPermission()
        } else {
          showMessage("权限申请未通过")
        }
      }
    } as LevenOptions)
  }

  //页面加载
  onMounted(() => {
    //申请权限
    requestPermissions();
  })

  //页面卸载
  onUnload(() => {
    module.release({
      complete: (res : LevenResult) => {
        console.log("释放资源" + JSON.stringify(res))
      }
    } as LevenOptions)
  })
</script>

<style>
  .text-style {
    font-size: 14px;
  }

  .scroll-Y {
    height: 200px;
  }
</style>