expo-ble模块测试demo 联调BLE模块 take 10 新增log 打印ani文件前后512字节

This commit is contained in:
Yudi Xiao
2025-12-11 16:42:41 +08:00
parent f816165779
commit 83cdf722ac
8 changed files with 32 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
import {BleClient} from '../core/BleClient';
import {ProtocolManager} from '../protocol/ProtocolManager';
import {BLE_UUIDS, COMMAND_TYPES, FRAME_CONSTANTS} from '../protocol/Constants';
import {BLE_UUIDS, APP_COMMAND_TYPES, FRAME_CONSTANTS} from '../protocol/Constants';
import {ProtocolFrame} from '../protocol/types';
import {Buffer} from 'buffer';
import {Subscription} from 'react-native-ble-plx';
@@ -139,13 +139,16 @@ export class BleProtocolService {
}
}
public async send(deviceId: string, type: COMMAND_TYPES, data: object | ArrayBuffer | Uint8Array, onProgress?: (progress: number) => void): Promise<void> {
public async send(deviceId: string, type: APP_COMMAND_TYPES, data: object | ArrayBuffer | Uint8Array, onProgress?: (progress: number) => void): Promise<void> {
let payload: Uint8Array;
if (data instanceof ArrayBuffer) {
console.debug("[BleProtocolService] Sending ArrayBuffer");
payload = new Uint8Array(data);
} else if (data instanceof Uint8Array) {
console.debug("[BleProtocolService] Sending Uint8Array");
payload = data;
} else {
console.debug("[BleProtocolService] Sending JSON payload");
const jsonStr = JSON.stringify(data);
payload = new Uint8Array(Buffer.from(jsonStr));
}
@@ -158,8 +161,10 @@ export class BleProtocolService {
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 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);
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;
console.debug(`Sending ${total} frames`);
for (let i = 0; i < total; i++) {
@@ -171,8 +176,7 @@ export class BleProtocolService {
if (onProgress) {
onProgress((i + 1) / total);
}
console.debug("Wrote frame", result);
// console.debug("Wrote frame", result);
}
}
}