expo-ble模块测试demo 联调BLE模块, 文件发送通过校验,去除多余的ANI文件缓存步骤

This commit is contained in:
Yudi Xiao
2025-12-11 18:33:28 +08:00
parent eab4d172e6
commit e3ee0f607c
7 changed files with 82 additions and 82 deletions

View File

@@ -162,7 +162,7 @@ export class BleProtocolService {
console.debug(`[BleProtocolService] Sending with MTU=${mtu}, maxDataSize=${safeMaxDataSize}`);
const rawPayloadHex = payload.reduce((acc, val) => acc + val.toString(16).padStart(2, '0') + ' ', '');
const formattedRawPayloadHex = rawPayloadHex.substring(0, 512) + "\n......\n" + rawPayloadHex.substring(rawPayloadHex.length - 512);
const formattedRawPayloadHex = rawPayloadHex.substring(0, 512 * 2) + "\n......\n" + rawPayloadHex.substring(rawPayloadHex.length - (512 * 2));
console.debug(`[BleProtocolService] Sending payload size=${payload.byteLength}, raw payload hex=\n${formattedRawPayloadHex}`);
const frames = ProtocolManager.createFrame(type, payload, FRAME_CONSTANTS.HEAD_APP_TO_DEVICE, safeMaxDataSize);
const total = frames.length;

View File

@@ -1,5 +1,5 @@
import {BleProtocolService} from './BleProtocolService';
import {APP_COMMAND_TYPES, COMMAND_TYPES, RESPONSE_TYPES} from '../protocol/Constants';
import {COMMAND_TYPES, RESPONSE_TYPES} from '../protocol/Constants';
import {DeviceInfo, ActivationStatus} from '../protocol/types';
import {Buffer} from 'buffer';

View File

@@ -16,22 +16,28 @@ export class FileTransferService {
return FileTransferService.instance;
}
public async transferFile(deviceId: string, filePath: string, type: APP_COMMAND_TYPES, onProgress?: (progress: number) => void): Promise<void> {
public async transferFile(deviceId: string, file: string | ArrayBuffer, type: APP_COMMAND_TYPES, onProgress?: (progress: number) => void): Promise<void> {
try {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error(`Failed to load file: ${response.statusText}`);
const startAt = Date.now();
let arrayBuffer: ArrayBuffer;
if (file instanceof ArrayBuffer) {
arrayBuffer = file
} else {
const response = await fetch(file);
if (!response.ok) {
throw new Error(`Failed to load file: ${response.statusText}`);
}
const blob = await response.blob();
const reader = new FileReader();
arrayBuffer = await new Promise<ArrayBuffer>((resolve, reject) => {
reader.onload = () => resolve(reader.result as ArrayBuffer);
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
}
const blob = await response.blob();
const reader = new FileReader();
const arrayBuffer = await new Promise<ArrayBuffer>((resolve, reject) => {
reader.onload = () => resolve(reader.result as ArrayBuffer);
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
await this.protocol.send(deviceId, type, arrayBuffer, onProgress);
const transferredAt = Date.now();
console.debug(`File transferred in ${(transferredAt - startAt) / 1000} s`);
} catch (e) {
console.error("File transfer failed", e);
throw e;