expo-ble模块测试demo 联调BLE模块 take 8 新增空数据包测试

This commit is contained in:
Yudi Xiao
2025-12-11 13:48:45 +08:00
parent b612335d95
commit 675036f07c
6 changed files with 62 additions and 22 deletions

View File

@@ -148,7 +148,16 @@ export class BleProtocolService {
payload = new Uint8Array(Buffer.from(jsonStr));
}
const frames = ProtocolManager.createFrame(type, payload);
const device = this.client.getConnectedDevice();
const mtu = device?.mtu || 23;
// MTU - 3 bytes (ATT overhead) - Protocol Header - Protocol Footer
const maxPayloadSize = mtu - 3 - FRAME_CONSTANTS.HEADER_SIZE - FRAME_CONSTANTS.FOOTER_SIZE;
// Ensure reasonable bounds (at least 1 byte, max constrained by protocol constant)
const safeMaxDataSize = Math.max(1, Math.min(maxPayloadSize, FRAME_CONSTANTS.MAX_DATA_SIZE));
console.debug(`[BleProtocolService] Sending with MTU=${mtu}, maxDataSize=${safeMaxDataSize}`);
const frames = ProtocolManager.createFrame(type, payload, FRAME_CONSTANTS.HEAD_APP_TO_DEVICE, true, safeMaxDataSize);
const total = frames.length;
console.debug(`Sending ${total} frames`);
for (let i = 0; i < total; i++) {
@@ -160,6 +169,7 @@ export class BleProtocolService {
if (onProgress) {
onProgress((i + 1) / total);
}
console.debug("Wrote frame", result);
}
}

View File

@@ -1,12 +1,13 @@
import { BleProtocolService } from './BleProtocolService';
import { COMMAND_TYPES } from '../protocol/Constants';
import {BleProtocolService} from './BleProtocolService';
import {COMMAND_TYPES} from '../protocol/Constants';
export class FileTransferService {
private static instance: FileTransferService;
private protocol = BleProtocolService.getInstance();
private constructor() {}
private constructor() {
}
public static getInstance(): FileTransferService {
if (!FileTransferService.instance) {
@@ -22,21 +23,27 @@ export class FileTransferService {
throw new Error(`Failed to load file: ${response.statusText}`);
}
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);
} catch (e) {
console.error("File transfer failed", e);
throw e;
}
}
public async transferTestPackage(deviceId: string, onProgress?: (progress: number) => void) {
const arrayBuffer = new Uint8Array(512);
console.debug(`test package size = ${arrayBuffer.byteLength}`)
await this.protocol.send(deviceId, COMMAND_TYPES.TRANSFER_ANI_VIDEO, arrayBuffer, onProgress);
}
public async transferOta(deviceId: string, filePath: string) {
return this.transferFile(deviceId, filePath, COMMAND_TYPES.OTA_PACKAGE);
}