diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/ble_demo_app_central/ble/PeripheralManager.kt b/app/src/main/java/com/example/ble_demo_app_central/ble/PeripheralManager.kt index ecf0a8b..b5ba3f8 100644 --- a/app/src/main/java/com/example/ble_demo_app_central/ble/PeripheralManager.kt +++ b/app/src/main/java/com/example/ble_demo_app_central/ble/PeripheralManager.kt @@ -172,8 +172,8 @@ class PeripheralManager( nextSeq = 0 credits = BleSpec.WINDOW_CREDITS fileCrc = CRC32() - outFile = File(context.cacheDir, "incoming_image_${System.currentTimeMillis()}.bin") - statusText.value = "Ready to receive → ${outFile?.absolutePath}" + outFile = null //File(context.cacheDir, "incoming_image_${System.currentTimeMillis()}.bin") + statusText.value = "Ready to receive" //"Ready to receive → ${outFile?.absolutePath}" } /** value = [ seq(u32) | len(u16) | crc32(u32) | payload(len) ] */ @@ -197,22 +197,50 @@ class PeripheralManager( // Start frame if (seq == 0xFFFFFFFFL) { - if (len == 8 && payload.size == 8) { - val s = ByteBuffer.wrap(payload).order(ByteOrder.LITTLE_ENDIAN) - val totalLen = s.long - totalExpected.value = totalLen - bytesReceived.value = 0L - nextSeq = 0 - statusText.value = "Start: total=$totalLen" - sendCredits(force = true) - return true - } else { - // 다른 len 값으로 온 Start는 거부 + if (len != 10 || payload.size != 10) { statusText.value = "Malformed start: len=$len" return false } + val ver = payload[0].toInt() and 0xFF + if (ver != 1) { + statusText.value = "Unsupported ver=$ver" + return false + } + // totalLen24 (LE, 3 bytes) + val total = (payload[1].toInt() and 0xFF) or + ((payload[2].toInt() and 0xFF) shl 8) or + ((payload[3].toInt() and 0xFF) shl 16) + + val extLen = payload[4].toInt() and 0xFF + if (extLen > 5) { + statusText.value = "Bad extLen=$extLen" + return false + } + val ext = buildString { + for (i in 0 until extLen) { + val ch = payload[5 + i].toInt() and 0xFF + if (ch == 0) break + append(ch.toChar()) + } + }.lowercase().filter { it.isLetterOrDigit() } + + totalExpected.value = total.toLong() + bytesReceived.value = 0L + nextSeq = 0 + fileCrc = CRC32() + credits = BleSpec.WINDOW_CREDITS + + // 파일 이름/확장자 생성 + val safeExt = if (ext.isNotEmpty()) ext else "bin" + val base = "incoming_${System.currentTimeMillis()}" + outFile = File(context.cacheDir, "$base.$safeExt") + + statusText.value = "Start: total=$total, ext=$safeExt → ${outFile?.name}" + sendCredits(force = true) + return true } + if (seq != nextSeq) { notifyStatus(code = 2, arg = nextSeq) statusText.value = "Seq mismatch: got=$seq expected=$nextSeq"