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

@@ -28,19 +28,20 @@ export class ProtocolManager {
type: COMMAND_TYPES,
data: Uint8Array,
head: FRAME_HEAD = FRAME_CONSTANTS.HEAD_APP_TO_DEVICE,
requireFragmentation: boolean = true
requireFragmentation: boolean = true,
maxDataSize: number = FRAME_CONSTANTS.MAX_DATA_SIZE
): Uint8Array[] {
// Max pages index is 4 bytes, so we can fit up to 65535 pages of data
const maxDataSize = 0xffff * (FRAME_CONSTANTS.HEADER_SIZE + FRAME_CONSTANTS.MAX_DATA_SIZE + FRAME_CONSTANTS.FOOTER_SIZE);
if (data.length > maxDataSize) {
throw new Error(`Data size ${data.length} exceeds max size ${maxDataSize}`);
const maxTotalSize = 0xffff * (FRAME_CONSTANTS.HEADER_SIZE + maxDataSize + FRAME_CONSTANTS.FOOTER_SIZE);
if (data.length > maxTotalSize) {
throw new Error(`Data size ${data.length} exceeds max size ${maxTotalSize}`);
}
if (data.length <= FRAME_CONSTANTS.MAX_DATA_SIZE || !requireFragmentation) {
console.debug(`[ProtocolManager] Frame size ${data.length} is less than max size ${FRAME_CONSTANTS.MAX_DATA_SIZE}, not fragmented`);
if (data.length <= maxDataSize || !requireFragmentation) {
console.debug(`[ProtocolManager] Frame size ${data.length} is less than max size ${maxDataSize}, not fragmented`);
return [this.createSingleFrame(type, data, head)];
} else {
console.debug(`[ProtocolManager] Frame size ${data.length} is greater than max size ${FRAME_CONSTANTS.MAX_DATA_SIZE}, fragmented`);
return this.createFragmentedFrames(type, data, head);
console.debug(`[ProtocolManager] Frame size ${data.length} is greater than max size ${maxDataSize}, fragmented`);
return this.createFragmentedFrames(type, data, head, maxDataSize);
}
}
@@ -62,6 +63,8 @@ export class ProtocolManager {
// dataLen
buffer[offset++] = (data.length >> 8) & 0xff;
buffer[offset++] = data.length & 0xff;
const hexHeader = Array.from(buffer.slice(0, offset)).map(b => b.toString(16).padStart(2, '0')).join(' ');
console.debug(`chunk length = ${data.length}, buffer 8 header = ${hexHeader}`)
// data
buffer.set(data, offset);
@@ -75,10 +78,9 @@ export class ProtocolManager {
return buffer;
}
private static createFragmentedFrames(type: COMMAND_TYPES, data: Uint8Array, head: FRAME_HEAD): Uint8Array[] {
private static createFragmentedFrames(type: COMMAND_TYPES, data: Uint8Array, head: FRAME_HEAD, maxDataSize: number): Uint8Array[] {
const frames: Uint8Array[] = [];
const totalSize = data.length;
const maxDataSize = FRAME_CONSTANTS.MAX_DATA_SIZE;
const totalPages = Math.ceil(totalSize / maxDataSize);
for (let i = 0; i < totalPages; i++) {
@@ -112,8 +114,7 @@ export class ProtocolManager {
offset += chunk.length;
buffer[offset] = this.calculateChecksum(buffer.slice(0, offset));
const verify = this.verifyChecksum(buffer.slice(0, offset), buffer[offset]);
console.debug(`[ProtocolManager] Verify checksum: ${verify}`);
frames.push(buffer);
}