Appearance
初始化
方法名
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" }) } } } })jsmodule.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" }) } } } }) - 参数说明
参数名 参数类型 是否必填 默认值 参数描述 success Function 否 无 执行成功的函数 fail Function 否 无 执行失败的函数 complete Function 否 无 执行完成的函数,不管成功还是失败都会执行
回调
- 示例json
{ "data": { "type": "onInit" }, "message": "", "code": 0 } - 回调说明:
参数名 参数类型 参数描述 message String 消息提示 data Object 数据对象 data.type String 类型,具体可参考类型:
onInit:初始化成功onBluetoothStateChanged:蓝牙开关状态改变回调onDeviceFound:发现新设备回调onDiscoveryStarted:设备搜索开始回调onDiscoveryStopped:设备搜索结束回调onPairingRequest:配对请求回调onPairingCompleted:配对完成回调onConnectionStateChanged:连接状态改变回调onDataReceived:接收到数据回调onDataSent:数据发送成功回调onFileSendProgress:文件发送进度回调onFileSendComplete:文件发送完成回调onFileReceiveRequest:收到文件接收请求回调onFileReceiveProgress:文件接收进度回调onFileReceiveComplete:文件接收完成回调onFileReceiveConfirm:文件接收确认回调onError:错误信息回调data.isEnabled Boolean true表示蓝牙已开启,false表示蓝牙已关闭, onBluetoothStateChanged回调中返回data.device Object 蓝牙设备, 返回该字段的回调 onPairingRequestonDeviceFoundonPairingCompletedonConnectionStateChangeddata.device.name String 蓝牙设备名称 data.device.address String 蓝牙设备地址 data.device.bondState Integer 蓝牙设备绑定状态,10:未配对,11:正在配对,12:已配对 data.device.rssi Integer 信号强度(单位:dBm)仅 onDeviceFound回调会返回data.success Boolean 回调含义: onPairingCompleted:配对是否成功onFileSendComplete:是否发送成功onFileReceiveComplete:是否接收成功data.state Integer 连接状态,0:未连接,1:连接监听中,2.连接中,3:已连接 onConnectionStateChanged回调中返回data.data byte[] 接收或发送的字节数据 onDataReceived和onDataSent回调中返回data.dataString String 接收或发送的字符串数据 onDataReceived和onDataSent回调中返回data.length Integer 接收的字节长度 onDataReceived回调中返回data.fileName String 文件名称,字段返回的回调 onFileSendProgressonFileSendCompleteonFileReceiveRequestonFileReceiveProgressonFileReceiveCompleteonFileReceiveConfirmdata.totalSize Integer 文件总大小(字节),字段返回的回调 onFileSendProgressonFileReceiveRequestonFileReceiveProgressdata.sentSize Integer 已发送大小(字节),字段返回的回调 onFileSendProgressdata.deviceName String 设备名称,字段返回的回调 onFileReceiveRequestdata.receivedSize Integer 已接收大小(字节),字段返回的回调 onFileReceiveProgressdata.filePath String 文件保存路径,字段返回的回调 onFileReceiveCompletedata.confirmed Boolean true表示用户接受,false表示用户拒绝,字段返回的回调 onFileReceiveConfirmdata.errorMessage String 错误信息描述,字段返回的回调 onErrorcode Integer 返回类型,0.成功,其他:失败
