From 72b9eaa61dcd724d2f6307710058ea224a2a2d78 Mon Sep 17 00:00:00 2001 From: Joe Kearney Date: Thu, 27 Feb 2025 19:43:52 -0600 Subject: [PATCH] Fixed Hello packet scanning. --- .../clubk/ktag/bletool/ScannerActivity.kt | 22 +++++++++++++++++-- gradle/libs.versions.toml | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/club/clubk/ktag/bletool/ScannerActivity.kt b/app/src/main/java/club/clubk/ktag/bletool/ScannerActivity.kt index 48625d4..ee890cf 100644 --- a/app/src/main/java/club/clubk/ktag/bletool/ScannerActivity.kt +++ b/app/src/main/java/club/clubk/ktag/bletool/ScannerActivity.kt @@ -370,7 +370,16 @@ fun parseKTagPacket(data: ByteArray): KTagPacket { 0x04 -> ConsolePacket( type = type, eventNumber = eventNumber, - consoleString = data.slice(6..26).toByteArray().toString(Charsets.US_ASCII).trim(0.toChar()) + consoleString = data.slice(6..26).toByteArray().let { bytes -> + // Find the position of the first null byte if any + val nullPos = bytes.indexOfFirst { it == 0.toByte() } + // If there's a null byte, take only up to that position, otherwise take all bytes + if (nullPos >= 0) { + bytes.slice(0 until nullPos).toByteArray() + } else { + bytes + }.toString(Charsets.US_ASCII).trim() + } ) 0x05 -> StatusPacket( @@ -405,7 +414,16 @@ fun parseKTagPacket(data: ByteArray): KTagPacket { minorVersion = data[7].toInt(), deviceType = bytesToInt(data, 8, 2), teamId = data[10].toInt(), - deviceName = data.slice(11..26).toByteArray().toString(Charsets.US_ASCII).trim(0.toChar()) + deviceName = data.slice(11..26).toByteArray().let { bytes -> + // Find the position of the first null byte if any + val nullPos = bytes.indexOfFirst { it == 0.toByte() } + // If there's a null byte, take only up to that position, otherwise take all bytes + if (nullPos >= 0) { + bytes.slice(0 until nullPos).toByteArray() + } else { + bytes + }.toString(Charsets.US_ASCII).trim() + } ) else -> StatusPacket( diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a7e2e03..be644ff 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -agp = "8.8.1" +agp = "8.8.2" kotlin = "2.0.0" coreKtx = "1.15.0" junit = "4.13.2"