Skip to content

初始化

方法名

init

用法

  • 用法如下:
    js
    module.init({
      complete: (res) => {
        console.log(res)
        let data = res.data || {};
        let type = data.type;
        if (type == "onDeviceFound") {
          //发现新设备
          let device = data.device;
          this.devices.push(device);
          this.logs.push("发现新设备:" + (device.name || "未知名称") + ":" + device.address)
        } else if (type == "onDiscoveryStopped") {
          //停止扫描
          this.logs.push("停止扫描")
        } else if (type == "onPairingCompleted") {
          //配对或取消配对完成
          this.getPairedDevices();
          this.closeDialog();
        } else if (type == "onConnectionStateChanged") {
          //连接状态更新
          this.closeDialog();
          let device = data.device;
          let state = data.state;
          let stateStr = "";
          switch (state) {
            case 0:
              stateStr = "已断开";
              break
            case 1:
              stateStr = "连接中";
              break
            case 2:
              stateStr = "连接监听中";
              break
            case 3:
              stateStr = "已连接";
              break
            default:
              stateStr = "未知";
          }
          this.logs.push("连接状态: " + stateStr)
          this.logs.push("设备: " + device.name)
          if (state == 3 && device) {
            this.connectDeviceAddress = device.address
          }
        } else if (type == "onDataReceived") {
          //收到数据
          let dataString = data.dataString || ""
          this.logs.push("接收到数据:" + dataString)
        } else if (type == "onFileReceiveRequest") {
          //收到发送文件的请求
          this.sendFileInfo = data;
          this.$refs.refFileRequestPop.open()
        } else if (type == "onFileSendProgress") {
          this.filePop.title = "文件发送中";
          if (!this.filePopIsOpen) {
            this.$refs.refFileProgressPop.open();
            this.filePopIsOpen = true;
          }
          this.filePop.percent = ((data.sentSize / data.totalSize) * 100).toFixed(2);
          this.filePop.size = data.sentSize;
          this.filePop.totalSize = data.totalSize;
        } else if (type == "onFileReceiveProgress") {
          this.filePop.title = "文件接收中";
          if (!this.filePopIsOpen) {
            this.$refs.refFileProgressPop.open();
            this.filePopIsOpen = true;
          }
          this.filePop.percent = ((data.receivedSize / data.totalSize) * 100).toFixed(2);
          this.filePop.size = data.receivedSize;
          this.filePop.totalSize = data.totalSize;
        } else if (type == "onFileReceiveComplete" || type == "onFileSendComplete") {
          this.$refs.refFileProgressPop.close();
          this.filePopIsOpen = false;
          if (type == "onFileReceiveComplete") {
            uni.showToast({
              title: "文件接收完毕",
              icon: "none"
            })
          } else {
            uni.showToast({
              title: "文件发送完毕",
              icon: "none"
            })
          }
        }
      }
    })
    js
    module.init({
      complete: (res) => {
        console.log(res)
        let data = res.data;
        let type = data.getString("type");
        if (type == "onInit") {
          if (res.code == 0) {
            logs.value.push("初始化成功")
          } else {
            logs.value.push("初始化失败")
          }
        } else if (type == "onDeviceFound") {
          //发现新设备
          let device = data.getJSON("device");
          devices.value.push({
            name: device?.getString("name"),
            address: device?.getString("address"),
            bondState: device?.getNumber("bondState")
          } as deviceItem)
          logs.value.push("发现新设备:" + (device?.getString("name") ?? "未知名称") + ":" + device?.getString("address"))
        } else if (type == "onDiscoveryStopped") {
          //停止扫描
          logs.value.push("停止扫描")
        } else if (type == "onPairingCompleted") {
          //配对或取消配对完成
          getPairedDevices()
          connectPopShow.value = false;
        } else if (type == "onConnectionStateChanged") {
          //连接状态更新
          connectPopShow.value = false;
          let deviceInfo = data.getJSON("device");
          let state = data.getNumber("state");
          let stateStr = "";
          switch (state) {
            case 0:
              stateStr = "已断开";
              break
            case 1:
              stateStr = "连接中";
              break
            case 2:
              stateStr = "连接监听中";
              break
            case 3:
              stateStr = "已连接";
              break
            default:
              stateStr = "未知";
          }
          logs.value.push("连接状态: " + stateStr)
          logs.value.push("设备: " + deviceInfo?.getString("name"))
          if (state == 3 && deviceInfo != null) {
            connectDeviceAddress.value = deviceInfo?.getString("address") ?? ""
          }
        } else if (type == "onDataReceived") {
          //收到数据
          let dataString = data.getString("dataString")
          logs.value.push("接收到数据:" + dataString)
        } else if (type == "onFileReceiveRequest") {
          //收到发送文件的请求
          sendFileInfo.value = {
            fileName: data.getString("fileName"),
            deviceName: data.getString("deviceName"),
            fileSize: data.getNumber("fileSize")
          } as fileItem;
          fileRequestPopShow.value = true
        } else if (type == "onFileSendProgress") {
          filePop.value.title = "文件发送中";
          if (!fileProgressPopShow.value) {
            fileProgressPopShow.value = true
          }
          let sentSize : number = data.getNumber("sentSize")!
          let totalSize : number = data.getNumber("totalSize")!
          filePop.value.percent = parseFloat(((sentSize / totalSize) * 100).toFixed(2));
          filePop.value.size = sentSize;
          filePop.value.totalSize = totalSize;
        } else if (type == "onFileReceiveProgress") {
          filePop.value.title = "文件接收中";
          if (!fileProgressPopShow.value) {
            fileProgressPopShow.value = true
          }
          let receivedSize : number = data.getNumber("receivedSize")!;
          let totalSize : number = data.getNumber("totalSize")!
          filePop.value.percent = parseFloat(((receivedSize / totalSize) * 100).toFixed(2));
          filePop.value.size = receivedSize;
          filePop.value.totalSize = totalSize;
        } else if (type == "onFileReceiveComplete" || type == "onFileSendComplete") {
          fileProgressPopShow.value = false;
          if (type == "onFileReceiveComplete") {
            uni.showToast({
              title: "文件接收完毕",
              icon: "none"
            })
          } else {
            uni.showToast({
              title: "文件发送完毕",
              icon: "none"
            })
          }
        }
      }
    })
  • 参数说明
    参数名参数类型是否必填默认值参数描述
    successFunction执行成功的函数
    failFunction执行失败的函数
    completeFunction执行完成的函数,不管成功还是失败都会执行

回调

  • 示例
    json
    {
        "data": {
            "type": "onInit"
        },
        "message": "",
        "code": 0
    }
  • 回调说明:
    参数名参数类型参数描述
    messageString消息提示
    dataObject数据对象
    data.typeString类型,具体可参考类型:
    onInit:初始化成功
    onBluetoothStateChanged:蓝牙开关状态改变回调
    onDeviceFound:发现新设备回调
    onDiscoveryStarted:设备搜索开始回调
    onDiscoveryStopped:设备搜索结束回调
    onPairingRequest:配对请求回调
    onPairingCompleted:配对完成回调
    onConnectionStateChanged:连接状态改变回调
    onDataReceived:接收到数据回调
    onDataSent:数据发送成功回调
    onFileSendProgress:文件发送进度回调
    onFileSendComplete:文件发送完成回调
    onFileReceiveRequest:收到文件接收请求回调
    onFileReceiveProgress:文件接收进度回调
    onFileReceiveComplete:文件接收完成回调
    onFileReceiveConfirm:文件接收确认回调
    onError:错误信息回调
    data.isEnabledBooleantrue表示蓝牙已开启,false表示蓝牙已关闭, onBluetoothStateChanged回调中返回
    data.deviceObject蓝牙设备, 返回该字段的回调
    onPairingRequest
    onDeviceFound
    onPairingCompleted
    onConnectionStateChanged
    data.device.nameString蓝牙设备名称
    data.device.addressString蓝牙设备地址
    data.device.bondStateInteger蓝牙设备绑定状态,10:未配对,11:正在配对,12:已配对
    data.device.rssiInteger信号强度(单位:dBm)仅onDeviceFound回调会返回
    data.successBoolean回调含义:
    onPairingCompleted:配对是否成功
    onFileSendComplete:是否发送成功
    onFileReceiveComplete:是否接收成功
    data.stateInteger连接状态,0:未连接,1:连接监听中,2.连接中,3:已连接 onConnectionStateChanged回调中返回
    data.databyte[]接收或发送的字节数据 onDataReceivedonDataSent回调中返回
    data.dataStringString接收或发送的字符串数据 onDataReceivedonDataSent回调中返回
    data.lengthInteger接收的字节长度 onDataReceived回调中返回
    data.fileNameString文件名称,字段返回的回调
    onFileSendProgress
    onFileSendComplete
    onFileReceiveRequest
    onFileReceiveProgress
    onFileReceiveComplete
    onFileReceiveConfirm
    data.totalSizeInteger文件总大小(字节),字段返回的回调
    onFileSendProgress
    onFileReceiveRequest
    onFileReceiveProgress
    data.sentSizeInteger已发送大小(字节),字段返回的回调
    onFileSendProgress
    data.deviceNameString设备名称,字段返回的回调
    onFileReceiveRequest
    data.receivedSizeInteger已接收大小(字节),字段返回的回调
    onFileReceiveProgress
    data.filePathString文件保存路径,字段返回的回调
    onFileReceiveComplete
    data.confirmedBooleantrue表示用户接受,false表示用户拒绝,字段返回的回调
    onFileReceiveConfirm
    data.errorMessageString错误信息描述,字段返回的回调
    onError
    codeInteger返回类型,0.成功,其他:失败