Add helper for HEX string to byte array conversion

This commit introduces a new helper function `fromHex()` that converts a HEX string (with optional separators like spaces or hyphens) into a byte array. It also adds a corresponding `toHex()` function for converting a byte array to a HEX string.

These helpers are now used in various parts of the codebase, including:
- Fake BLE data generation
- AirPod key input dialog
- RPA checker tests
- BaseAirPodsTest

Additionally, a unit test for these conversion functions has been added.
This commit is contained in:
darken
2025-06-06 06:03:41 +02:00
committed by Matthias Urhahn
parent eda81822aa
commit 11a19da0b8
6 changed files with 65 additions and 35 deletions
@@ -10,7 +10,9 @@ import dagger.hilt.android.AndroidEntryPoint
import eu.darken.capod.R
import eu.darken.capod.common.WebpageTool
import eu.darken.capod.common.bluetooth.ScannerMode
import eu.darken.capod.common.fromHex
import eu.darken.capod.common.preferences.PercentSliderPreference
import eu.darken.capod.common.toHex
import eu.darken.capod.common.uix.PreferenceFragment3
import eu.darken.capod.common.upgrade.UpgradeRepo
import eu.darken.capod.main.core.GeneralSettings
@@ -52,8 +54,8 @@ class GeneralSettingsFragment : PreferenceFragment3() {
mainDeviceIdentityKeyPref.setOnPreferenceClickListener {
AirPodKeyInputDialog(requireContext()).create(
mode = AirPodKeyInputDialog.Mode.IRK,
current = generalSettings.mainDeviceIdentityKey.value?.toHumanReadable() ?: "",
onKey = { generalSettings.mainDeviceIdentityKey.value = it.fromHumanReadable() },
current = generalSettings.mainDeviceIdentityKey.value?.toHex() ?: "",
onKey = { generalSettings.mainDeviceIdentityKey.value = it.fromHex() },
onGuide = { webpageTool.open("https://github.com/d4rken-org/capod/wiki/airpod-Keys") }
).show()
true
@@ -61,8 +63,8 @@ class GeneralSettingsFragment : PreferenceFragment3() {
mainDeviceEncryptionKeyPref.setOnPreferenceClickListener {
AirPodKeyInputDialog(requireContext()).create(
mode = AirPodKeyInputDialog.Mode.ENC,
current = generalSettings.mainDeviceEncryptionKey.value?.toHumanReadable() ?: "",
onKey = { generalSettings.mainDeviceEncryptionKey.value = it.fromHumanReadable() },
current = generalSettings.mainDeviceEncryptionKey.value?.toHex() ?: "",
onKey = { generalSettings.mainDeviceEncryptionKey.value = it.fromHex() },
onGuide = { webpageTool.open("https://github.com/d4rken-org/capod/wiki/airpod-Keys") }
).show()
true
@@ -71,10 +73,6 @@ class GeneralSettingsFragment : PreferenceFragment3() {
super.onPreferencesCreated()
}
private fun ByteArray.toHumanReadable(): String = joinToString("-") { "%02X".format(it) }
private fun String.fromHumanReadable(): ByteArray = split("-").map { it.toInt(16).toByte() }.toByteArray()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
vm.state.observe2 { state ->
mainDeviceAddressPref.setOnPreferenceClickListener {