|
Download agents/ble_file_transfer_code_cleanup.md from Anirudh1950/MentraOS: direct link, hf CLI and curl.
- Browser
- Download file 4.63 kB
-
https://huggingface.co/Anirudh1950/MentraOS/resolve/main/agents/ble_file_transfer_code_cleanup.md
- Command line
-
hf download hf://Anirudh1950/MentraOS/agents/ble_file_transfer_code_cleanup.md
-
curl -L -o ble_file_transfer_code_cleanup.md https://huggingface.co/Anirudh1950/MentraOS/resolve/main/agents/ble_file_transfer_code_cleanup.md
4.63 kB
BLE File Transfer Code Cleanup Guide
Summary of Current Implementation
The BLE file transfer is working perfectly with the BES chip's auto-acknowledgment system. Here's what can be cleaned up to make the code production-ready.
1. Phone-Side ACK Code (Can Remove)
In MentraLiveSGC.java:
// REMOVE THIS METHOD - NOT NEEDED
private void sendFileTransferAck(int state, int index) {
// BES chip handles ACKs automatically
}
// REMOVE THIS CALL in processFilePacket():
// sendFileTransferAck(1, packetInfo.packIndex);
In K900ProtocolUtils.java:
// KEEP but mark as "For testing/simulation only"
public static String createFileTransferAck(int state, int index) {
// Only needed if simulating BES behavior for testing
}
2. Glasses-Side Simplifications
In K900BluetoothManager.java:
Can simplify or remove:
FilePacketStateclass - might not need retry trackingpendingPacketsmap - if BES is reliablecheckFilePacketAck()method - complex timeout logicFILE_TRANSFER_ACK_TIMEOUT_MSconstantFILE_TRANSFER_MAX_RETRIESconstant
Must keep:
- BES ACK handling with index+1 behavior
- Fast mode switching
- Basic packet sending logic
3. Debug/Test Code to Clean
Remove excessive logging:
// Change from:
Log.e(TAG, "📦 JSON DATA BEFORE C-WRAPPING: " + originalData);
// To production-appropriate:
Log.d(TAG, "Wrapping JSON data");
Make test method conditional:
@DebugOnly // or similar annotation
public boolean sendTestImageFromAssets(String assetFileName) {
// Keep for development, remove from production
}
4. Byte Order Consolidation
Create utility methods:
public class ByteOrderUtils {
// Consolidate all big-endian conversions
public static int readBigEndianInt16(byte[] data, int offset) { }
public static int readBigEndianInt32(byte[] data, int offset) { }
public static void writeBigEndianInt16(byte[] data, int offset, int value) { }
// etc.
}
Remove little-endian file packet code:
- Keep little-endian only for phone-to-glasses JSON messages
- All file packets use big-endian exclusively
5. Essential Code to Keep
K900ProtocolUtils.java:
packFilePacket()- exactly as isextractFilePacket()- exactly as isCMD_TYPE_PHOTOand related constantsFilePacketInfoclass
K900BluetoothManager.java:
sendImageFile()core logicsendNextFilePacket()methodhandleFileTransferAck()with BES index+1 logicFileTransferSessionclass- Fast mode switching
MentraLiveSGC.java:
- File packet detection (0x31 type)
FileTransferSessionfor reassemblyprocessFilePacket()method- File saving logic
- Duplicate packet detection
ComManager.java:
sendFile()methodsetFastMode()and fast/normal sleep modes
6. Recommended Refactoring
Create dedicated file transfer classes:
// New class for cleaner separation
public class BleFileTransferManager {
// Move all file transfer logic here
// Keep BluetoothManager focused on connection management
}
// New class for protocol handling
public class FileTransferProtocol {
// Move packet creation/parsing here
// Separate from general K900 protocol utils
}
Add proper callbacks:
public interface FileTransferListener {
void onTransferStarted(String fileName, int totalPackets);
void onPacketSent(int packetIndex, int totalPackets);
void onTransferComplete(String fileName);
void onTransferFailed(String fileName, String reason);
}
7. Production Checklist
- Remove all
Log.e()calls used for debugging - Add proper error handling with user-friendly messages
- Make logging level configurable
- Add file size limits and validation
- Implement transfer cancellation
- Add unit tests for packet creation/parsing
- Document BES chip behavior clearly
- Add integration tests with timeout handling
- Consider memory usage for large files
- Add progress notification to UI
8. Keep for Documentation
Critical knowledge to preserve:
- BES chip auto-ACKs with index = packet_index + 1
- Big-endian byte order for file packets
- 72FF characteristic for file transfer
- Fast mode (5ms) critical for performance
- No phone ACKs needed - BES handles everything
This cleanup will reduce code complexity while maintaining the working implementation.