chore(deps): update nordic.ble to v2.0.0-alpha13 (#4534)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
renovate[bot] 2026-02-11 11:14:46 -06:00 committed by GitHub
parent f5eb3387bb
commit 55b17857be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 169 additions and 189 deletions

View file

@ -78,8 +78,8 @@ dependencies {
testImplementation(libs.junit)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.nordic.client.android.mock)
testImplementation(libs.nordic.client.mock)
testImplementation(libs.nordic.core.android.mock)
testImplementation(libs.nordic.client.core.mock)
testImplementation(libs.nordic.core.mock)
testImplementation(libs.nordic.environment.android.mock)
testImplementation(libs.mockk)
}

View file

@ -36,10 +36,9 @@ import no.nordicsemi.kotlin.ble.client.android.Peripheral
import no.nordicsemi.kotlin.ble.client.distinctByPeripheral
import no.nordicsemi.kotlin.ble.core.ConnectionState
import no.nordicsemi.kotlin.ble.core.WriteType
import java.util.UUID
import kotlin.time.Duration.Companion.seconds
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.toKotlinUuid
import kotlin.uuid.Uuid
/**
* BLE transport implementation for ESP32 Unified OTA protocol. Uses Nordic Kotlin-BLE-Library for modern coroutine
@ -49,6 +48,7 @@ import kotlin.uuid.toKotlinUuid
* - OTA Characteristic (Write): 62ec0272-3ec5-11eb-b378-0242ac130005
* - TX Characteristic (Notify): 62ec0272-3ec5-11eb-b378-0242ac130003
*/
@OptIn(ExperimentalUuidApi::class)
class BleOtaTransport(
private val centralManager: CentralManager,
private val address: String,
@ -74,38 +74,33 @@ class BleOtaTransport(
* ESP32 bootloaders may use the original MAC address OR increment the last byte by 1 for OTA mode, so we check both
* addresses.
*/
@OptIn(ExperimentalUuidApi::class)
private suspend fun scanForOtaDevice(): Peripheral? {
// ESP32 OTA bootloader may use MAC address with last byte incremented by 1
val otaAddress = calculateOtaAddress(address)
val targetAddresses = setOf(address, otaAddress)
Logger.i { "BLE OTA: Will match addresses: $targetAddresses" }
Logger.i { "BLE OTA: Will match addresses: $address, $otaAddress" }
repeat(SCAN_RETRY_COUNT) { attempt ->
Logger.i { "BLE OTA: Scanning for device (attempt ${attempt + 1}/$SCAN_RETRY_COUNT)..." }
// Scan without service UUID filter - ESP32 OTA bootloader may not advertise the UUID
// Log all devices found during scan for debugging
val foundDevices = mutableSetOf<String>()
// Use the new scan DSL for better efficiency
val peripheral =
centralManager
.scan(SCAN_TIMEOUT)
.distinctByPeripheral()
.map { it.peripheral }
.onEach { p ->
if (foundDevices.add(p.address)) {
Logger.d { "BLE OTA: Scan found device: ${p.address} (name=${p.name})" }
.scan(SCAN_TIMEOUT) {
Any {
Address(address)
Address(otaAddress)
}
}
.firstOrNull { it.address in targetAddresses }
.distinctByPeripheral()
.map { it.peripheral }
.onEach { p -> Logger.d { "BLE OTA: Scan found matching device: ${p.address} (name=${p.name})" } }
.firstOrNull()
if (peripheral != null) {
Logger.i { "BLE OTA: Found target device at ${peripheral.address}" }
return peripheral
}
Logger.w { "BLE OTA: Target addresses $targetAddresses not in ${foundDevices.size} devices found" }
if (attempt < SCAN_RETRY_COUNT - 1) {
Logger.i { "BLE OTA: Device not found, waiting ${SCAN_RETRY_DELAY_MS}ms before retry..." }
kotlinx.coroutines.delay(SCAN_RETRY_DELAY_MS)
@ -129,20 +124,18 @@ class BleOtaTransport(
}
/** Connect to the device and discover OTA service. */
@OptIn(ExperimentalUuidApi::class)
@Suppress("LongMethod")
override suspend fun connect(): Result<Unit> = runCatching {
Logger.i { "BLE OTA: Waiting ${REBOOT_DELAY_MS}ms for device to reboot into OTA mode..." }
kotlinx.coroutines.delay(REBOOT_DELAY_MS)
Logger.i { "BLE OTA: Connecting to $address using Nordic BLE Library..." }
Logger.i { "BLE OTA: Connecting to $address using Nordic BLE Library v2..." }
// Scan for device by address - device must have rebooted into OTA mode
val p =
scanForOtaDevice()
?: throw OtaProtocolException.ConnectionFailed(
"Device not found at address $address. " +
"Ensure the device has rebooted into OTA mode and is advertising.",
"Device not found. Ensure the device has rebooted into OTA mode and is advertising.",
)
peripheral = p
@ -164,33 +157,30 @@ class BleOtaTransport(
.launchIn(transportScope)
// Wait for connection or failure with timeout
// Don't use drop(1) - we might already be connected by the time we start collecting
val connectionState =
try {
withTimeout(CONNECTION_TIMEOUT_MS) {
p.state.first { it is ConnectionState.Connected || it is ConnectionState.Disconnected }
}
} catch (@Suppress("SwallowedException") e: kotlinx.coroutines.TimeoutCancellationException) {
Logger.w { "BLE OTA: Timed out waiting to connect to ${p.address}. Error: ${e.message}" }
throw OtaProtocolException.Timeout("Timed out connecting to device at address ${p.address}")
try {
withTimeout(CONNECTION_TIMEOUT_MS) {
p.state.first { it is ConnectionState.Connected || it is ConnectionState.Disconnected }
}
} catch (@Suppress("SwallowedException") e: kotlinx.coroutines.TimeoutCancellationException) {
Logger.w { "BLE OTA: Timed out waiting to connect to ${p.address}" }
throw OtaProtocolException.Timeout("Timed out connecting to device at address ${p.address}")
}
if (connectionState is ConnectionState.Disconnected) {
Logger.w { "BLE OTA: Failed to connect to ${p.address} (state=$connectionState)" }
if (p.isConnected != true) {
Logger.w { "BLE OTA: Failed to connect to ${p.address} (state=${p.state.value})" }
throw OtaProtocolException.ConnectionFailed("Failed to connect to device at address ${p.address}")
}
Logger.i { "BLE OTA: Connected to ${p.address}, discovering services..." }
// Discover services
val services = p.services(listOf(SERVICE_UUID.toKotlinUuid())).filterNotNull().first()
// Discover services using kotlin.uuid.Uuid
val services = p.services(listOf(SERVICE_UUID)).filterNotNull().first()
val meshtasticOtaService =
services.find { it.uuid == SERVICE_UUID.toKotlinUuid() }
services.find { it.uuid == SERVICE_UUID }
?: throw OtaProtocolException.ConnectionFailed("ESP32 OTA service not found")
otaCharacteristic =
meshtasticOtaService.characteristics.find { it.uuid == OTA_CHARACTERISTIC_UUID.toKotlinUuid() }
val txChar = meshtasticOtaService.characteristics.find { it.uuid == TX_CHARACTERISTIC_UUID.toKotlinUuid() }
otaCharacteristic = meshtasticOtaService.characteristics.find { it.uuid == OTA_CHARACTERISTIC_UUID }
val txChar = meshtasticOtaService.characteristics.find { it.uuid == TX_CHARACTERISTIC_UUID }
if (otaCharacteristic == null || txChar == null) {
throw OtaProtocolException.ConnectionFailed("Required characteristics not found")
@ -345,9 +335,9 @@ class BleOtaTransport(
companion object {
// Service and Characteristic UUIDs from ESP32 Unified OTA spec
private val SERVICE_UUID = UUID.fromString("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val OTA_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130005")
private val TX_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130003")
private val SERVICE_UUID = Uuid.parse("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val OTA_CHARACTERISTIC_UUID = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130005")
private val TX_CHARACTERISTIC_UUID = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130003")
// Timeouts and retries
private val SCAN_TIMEOUT = 10.seconds

View file

@ -35,7 +35,6 @@ import no.nordicsemi.kotlin.ble.core.Permission
import no.nordicsemi.kotlin.ble.core.and
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.UUID
import kotlin.time.Duration.Companion.milliseconds
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@ -46,17 +45,16 @@ class BleOtaTransportErrorTest {
private val testDispatcher = StandardTestDispatcher()
private val address = "00:11:22:33:44:55"
private val serviceUuid = UUID.fromString("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val otaCharacteristicUuid = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130005")
private val txCharacteristicUuid = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130003")
private fun UUID.toKotlinUuid(): Uuid = Uuid.parse(this.toString())
private val serviceUuid = Uuid.parse("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val otaCharacteristicUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130005")
private val txCharacteristicUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130003")
@Test
fun `startOta fails when device rejects hash`() = runTest(testDispatcher) {
val centralManager = CentralManager.Factory.mock(scope = backgroundScope)
lateinit var otaPeripheral: PeripheralSpec<String>
var txCharHandle: Int = -1
var otaCharHandle: Int = -1
val eventHandler =
object : PeripheralSpecEventHandler {
@ -86,16 +84,17 @@ class BleOtaTransportErrorTest {
CompleteLocalName("ESP32-OTA")
}
connectable(name = "ESP32-OTA", eventHandler = eventHandler, isBonded = true) {
Service(uuid = serviceUuid.toKotlinUuid()) {
Characteristic(
uuid = otaCharacteristicUuid.toKotlinUuid(),
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
Service(uuid = serviceUuid) {
otaCharHandle =
Characteristic(
uuid = otaCharacteristicUuid,
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
txCharHandle =
Characteristic(
uuid = txCharacteristicUuid.toKotlinUuid(),
uuid = txCharacteristicUuid,
property = CharacteristicProperty.NOTIFY,
permission = Permission.READ,
)
@ -120,6 +119,7 @@ class BleOtaTransportErrorTest {
val centralManager = CentralManager.Factory.mock(scope = backgroundScope)
lateinit var otaPeripheral: PeripheralSpec<String>
var txCharHandle: Int = -1
var otaCharHandle: Int = -1
val eventHandler =
object : PeripheralSpecEventHandler {
@ -146,16 +146,17 @@ class BleOtaTransportErrorTest {
CompleteLocalName("ESP32-OTA")
}
connectable(name = "ESP32-OTA", eventHandler = eventHandler, isBonded = true) {
Service(uuid = serviceUuid.toKotlinUuid()) {
Characteristic(
uuid = otaCharacteristicUuid.toKotlinUuid(),
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
Service(uuid = serviceUuid) {
otaCharHandle =
Characteristic(
uuid = otaCharacteristicUuid,
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
txCharHandle =
Characteristic(
uuid = txCharacteristicUuid.toKotlinUuid(),
uuid = txCharacteristicUuid,
property = CharacteristicProperty.NOTIFY,
permission = Permission.READ,
)
@ -192,6 +193,7 @@ class BleOtaTransportErrorTest {
val centralManager = CentralManager.Factory.mock(scope = backgroundScope)
lateinit var otaPeripheral: PeripheralSpec<String>
var txCharHandle: Int = -1
var otaCharHandle: Int = -1
val eventHandler =
object : PeripheralSpecEventHandler {
@ -225,16 +227,17 @@ class BleOtaTransportErrorTest {
CompleteLocalName("ESP32-OTA")
}
connectable(name = "ESP32-OTA", eventHandler = eventHandler, isBonded = true) {
Service(uuid = serviceUuid.toKotlinUuid()) {
Characteristic(
uuid = otaCharacteristicUuid.toKotlinUuid(),
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
Service(uuid = serviceUuid) {
otaCharHandle =
Characteristic(
uuid = otaCharacteristicUuid,
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
txCharHandle =
Characteristic(
uuid = txCharacteristicUuid.toKotlinUuid(),
uuid = txCharacteristicUuid,
property = CharacteristicProperty.NOTIFY,
permission = Permission.READ,
)

View file

@ -33,13 +33,8 @@ import no.nordicsemi.kotlin.ble.client.android.Peripheral
import no.nordicsemi.kotlin.ble.client.android.ScanResult
import no.nordicsemi.kotlin.ble.core.ConnectionState
import org.junit.Test
import java.util.UUID
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.toKotlinUuid
private val SERVICE_UUID = UUID.fromString("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val OTA_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130005")
private val TX_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130003")
import kotlin.uuid.Uuid
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalUuidApi::class)
class BleOtaTransportMtuTest {
@ -57,15 +52,20 @@ class BleOtaTransportMtuTest {
val service: RemoteService = mockk(relaxed = true)
val scanResult: ScanResult = mockk(relaxed = true)
val serviceUuid = Uuid.parse("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
val otaCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130005")
val txCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130003")
every { scanResult.peripheral } returns peripheral
every { centralManager.scan(any(), any()) } returns flowOf(scanResult)
every { peripheral.address } returns address
every { peripheral.state } returns MutableStateFlow(ConnectionState.Connected)
every { peripheral.isConnected } returns true
coEvery { peripheral.services(any()) } returns MutableStateFlow(listOf(service))
every { service.uuid } returns SERVICE_UUID.toKotlinUuid()
every { service.uuid } returns serviceUuid
every { service.characteristics } returns listOf(otaChar, txChar)
every { otaChar.uuid } returns OTA_CHARACTERISTIC_UUID.toKotlinUuid()
every { txChar.uuid } returns TX_CHARACTERISTIC_UUID.toKotlinUuid()
every { otaChar.uuid } returns otaCharUuid
every { txChar.uuid } returns txCharUuid
coEvery { centralManager.connect(any(), any()) } returns Unit
every { txChar.subscribe() } returns MutableSharedFlow()

View file

@ -37,23 +37,16 @@ import no.nordicsemi.kotlin.ble.core.and
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.util.UUID
import kotlin.time.Duration.Companion.milliseconds
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
private val SERVICE_UUID = UUID.fromString("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val OTA_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130005")
private val TX_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130003")
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalUuidApi::class)
class BleOtaTransportNordicMockTest {
private val testDispatcher = kotlinx.coroutines.test.StandardTestDispatcher()
private val address = "00:11:22:33:44:55"
private fun java.util.UUID.toKotlinUuid(): Uuid = Uuid.parse(this.toString())
@Before
fun setup() {
Logger.setLogWriters(
@ -75,6 +68,10 @@ class BleOtaTransportNordicMockTest {
var otaCharHandle: Int = -1
var txCharHandle: Int = -1
val serviceUuid = Uuid.parse("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
val otaCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130005")
val txCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130003")
// Use a property to hold the peripheral since we need it in the event handler
lateinit var otaPeripheral: PeripheralSpec<String>
@ -117,17 +114,17 @@ class BleOtaTransportNordicMockTest {
CompleteLocalName("ESP32-OTA")
}
connectable(name = "ESP32-OTA", eventHandler = eventHandler) {
Service(uuid = SERVICE_UUID.toKotlinUuid()) {
Service(uuid = serviceUuid) {
otaCharHandle =
Characteristic(
uuid = OTA_CHARACTERISTIC_UUID.toKotlinUuid(),
uuid = otaCharUuid,
properties =
CharacteristicProperty.WRITE and CharacteristicProperty.WRITE_WITHOUT_RESPONSE,
permission = Permission.WRITE,
)
txCharHandle =
Characteristic(
uuid = TX_CHARACTERISTIC_UUID.toKotlinUuid(),
uuid = txCharUuid,
property = CharacteristicProperty.NOTIFY,
permission = Permission.READ,
)

View file

@ -32,13 +32,8 @@ import no.nordicsemi.kotlin.ble.client.android.Peripheral
import no.nordicsemi.kotlin.ble.client.android.ScanResult
import no.nordicsemi.kotlin.ble.core.ConnectionState
import org.junit.Test
import java.util.UUID
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.toKotlinUuid
private val SERVICE_UUID = UUID.fromString("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
private val OTA_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130005")
private val TX_CHARACTERISTIC_UUID = UUID.fromString("62ec0272-3ec5-11eb-b378-0242ac130003")
import kotlin.uuid.Uuid
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalUuidApi::class)
class BleOtaTransportTest {
@ -56,6 +51,10 @@ class BleOtaTransportTest {
val service: RemoteService = mockk(relaxed = true)
val scanResult: ScanResult = mockk()
val serviceUuid = Uuid.parse("4FAFC201-1FB5-459E-8FCC-C5C9C331914B")
val otaCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130005")
val txCharUuid = Uuid.parse("62ec0272-3ec5-11eb-b378-0242ac130003")
every { scanResult.peripheral } returns peripheral
// Mock the scan call. It takes a Duration and a lambda.
@ -63,12 +62,13 @@ class BleOtaTransportTest {
every { peripheral.address } returns address
every { peripheral.state } returns MutableStateFlow(ConnectionState.Connected)
every { peripheral.isConnected } returns true
coEvery { peripheral.services(any()) } returns MutableStateFlow(listOf(service))
every { service.uuid } returns SERVICE_UUID.toKotlinUuid()
every { service.uuid } returns serviceUuid
every { service.characteristics } returns listOf(otaChar, txChar)
every { otaChar.uuid } returns OTA_CHARACTERISTIC_UUID.toKotlinUuid()
every { txChar.uuid } returns TX_CHARACTERISTIC_UUID.toKotlinUuid()
every { otaChar.uuid } returns otaCharUuid
every { txChar.uuid } returns txCharUuid
coEvery { centralManager.connect(any(), any()) } returns Unit