Skip to content

Instantly share code, notes, and snippets.

@KingAtoki
Created July 8, 2025 15:37
Show Gist options
  • Select an option

  • Save KingAtoki/15f863860404fffbf86b1661af63db5b to your computer and use it in GitHub Desktop.

Select an option

Save KingAtoki/15f863860404fffbf86b1661af63db5b to your computer and use it in GitHub Desktop.
ER BMP sending
/**
* Performs a single BMP transfer attempt to a peripheral
*
* This is the core BMP transfer protocol implementation using Java-style approach:
* 1. Splits BMP data into 194-byte chunks with proper addressing
* 2. Sends packets sequentially with minimal delays
* 3. Sends end command (0x20, 0x0d, 0x0e)
* 4. Sends CRC verification using standard CRC32
*
* @param peripheral: The target CBPeripheral device
* @param bmpData: Raw BMP image data
* @param attempt: Current attempt number (for logging)
* @returns: Boolean indicating transfer success
*/
private func sendBmpOnce(peripheral: CBPeripheral, bmpData: Data, attempt: Int) async -> Bool {
do {
print("BLE Debug: Starting Java-style BMP transfer, attempt \(attempt)")
// STEP 1: Create packets using Java-style chunking
let packets = createJavaStyleBmpPackets(bmpData: [UInt8](bmpData))
print("BLE Debug: Created \(packets.count) Java-style packets")
// STEP 2: Send all packets sequentially with minimal delays
for (i, packet) in packets.enumerated() {
print("BLE Debug: Sending packet \(i + 1)/\(packets.count), size: \(packet.count)")
BluetoothWriter.writeCharacteristic(
peripheral: peripheral,
serviceUUID: UART_SERVICE_UUID,
characteristicUUID: UART_TX_CHAR_UUID,
data: Data(packet),
withResponse: false,
delegate: bluetoothDelegate
)
// Minimal delay between packets (like Java implementation)
if i < packets.count - 1 {
try await Task.sleep(nanoseconds: 8_000_000) // 8ms
}
}
// STEP 3: Send end command
print("BLE Debug: Sending end command")
let endCommand: [UInt8] = [0x20, 0x0d, 0x0e]
BluetoothWriter.writeCharacteristic(
peripheral: peripheral,
serviceUUID: UART_SERVICE_UUID,
characteristicUUID: UART_TX_CHAR_UUID,
data: Data(endCommand),
withResponse: false,
delegate: bluetoothDelegate
)
// Give time to process (like Java implementation)
try await Task.sleep(nanoseconds: 100_000_000) // 100ms
// STEP 4: Send CRC using standard CRC32 calculation
let crcValue = computeStandardCrc32(bmpData: [UInt8](bmpData))
let crcBytes: [UInt8] = [
0x16, // CRC command
UInt8((crcValue >> 24) & 0xFF),
UInt8((crcValue >> 16) & 0xFF),
UInt8((crcValue >> 8) & 0xFF),
UInt8(crcValue & 0xFF),
]
print("BLE Debug: Sending CRC command, CRC value: \(String(format: "%08x", crcValue))")
BluetoothWriter.writeCharacteristic(
peripheral: peripheral,
serviceUUID: UART_SERVICE_UUID,
characteristicUUID: UART_TX_CHAR_UUID,
data: Data(crcBytes),
withResponse: false,
delegate: bluetoothDelegate
)
print("BLE Debug: Java-style BMP transfer completed successfully")
return true
} catch {
print("BLE Debug: Error during Java-style BMP transfer attempt \(attempt): \(error)")
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment