// pages/OTA/ota.js Page({ /** * 页面的初始数据 */ data: { nvabarData: { showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示 title: '固件信息', //导航栏 中间的标题 }, device: getApp().globalData.device, otaData: {}, btHelper: null, }, startOtaTap() { let connect = getApp().globalData.device.connect ?? false if (!connect) { wx.showToast({ title: '设备已经掉线了', }) return; } let _this = this wx.showModal({ title: '有新固件可升级', content: _this.otaData.content ?? '提升体验,操作更流畅', cancelText: '以后再说', confirmText: '升级', complete: (res) => { if (res.cancel) { } if (res.confirm) { _this.downloadFile(_this.otaData.url) } } }) // const btHelper = BtHelper.getInstance(); // btHelper.ota(this.data.otaData.file_url) }, downloadFile(url) { urlPath = updateData.url ?? ""; content = updateData.content ?? "暂无更新"; var list = urlPath.split("/"); // let cachePath = await PathUtil.getDownloadPath(fileName: list.last ?? 'OTAUpgrade.OTA'); const fs = wx.getFileSystemManager(); // 构建本地文件路径 const localFilePath = list[list.length - 1]; let _this = this; // 判断文件是否存在 fs.access(localFilePath, (err) => { if (err) { // 文件不存在,开始下载 wx.downloadFile({ url: url, // 下载资源的URL success: (res) => { if (res.statusCode === 200) { // 下载成功,保存文件路径 const filePath = res.tempFilePath; // 将下载的文件移动到本地路径 fs.rename({ oldPath: filePath, newPath: localFilePath, success: () => { // this.setData({ // localFilePath: localFilePath // }); // 调用 OTA 更新方法 // const btHelper = BtHelper.getInstance(); // btHelper.ota(localFilePath); wx.showToast({ title: '文件下载成功', }); _this.readFile(localFilePath); }, fail: (err) => { wx.showToast({ title: '文件移动失败', }); console.error('文件移动失败:', err); } }); } else { wx.showToast({ title: '文件下载失败', }); } }, fail: (err) => { wx.showToast({ title: '文件下载失败', }); console.error('下载文件失败:', err); } }); } else { _this.readFile(localFilePath); wx.showToast({ title: '文件已存在,跳过下载', }); } }); }, readFile(localFilePath) { if (!localFilePath) { wx.showToast({ title: '下载文件为空', }); return; } const fs = wx.getFileSystemManager(); fs.readFile({ filePath: localFilePath, encoding: 'base64', // 可以选择 'utf8' 或 'base64' success: (res) => { // const btHelper = BtHelper.getInstance(); // btHelper.ota(localFilePath); // console.log('文件内容:', res.data); }, fail: (err) => { wx.showToast({ title: 'OTA文件下载失败', }); console.error('文件读取失败:', err); } }); }, sendDataToBluetooth(data) { const chunkSize = 20; // 每次发送的数据块大小 const chunks = []; for (let i = 0; i < data.length; i += chunkSize) { chunks.push(data.slice(i, i + chunkSize)); } const sendNextChunk = (index) => { if (index >= chunks.length) { wx.showToast({ title: '文件发送完成', }); return; } const chunk = chunks[index]; // wx.writeBLECharacteristicValue({ // deviceId: deviceId, // serviceId: 'yourServiceId', // 替换为实际的服务ID // characteristicId: 'yourCharacteristicId', // 替换为实际的特征值ID // value: new Uint8Array(Buffer.from(chunk, 'base64')), // success: () => { // setTimeout(() => { // sendNextChunk(index + 1); // }, 100); // 延迟100毫秒发送下一个数据块 // }, // fail: (err) => { // wx.showToast({ // title: '文件发送失败', // }); // console.error('文件发送失败:', err); // } // }); } }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let otaData = JSON.parse(options.param) this.setData({ otaData: otaData }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })