[Android HiveBtle] Migrate connection managers to Rust implementations via UniFFI

Resolved 💬 2 comments Opened Feb 5, 2026 by kitplummer Closed Feb 5, 2026

Summary

The Android HiveBtle SDK (Kotlin) contains native implementations for:

  • ReconnectionManager - Auto-reconnection with exponential backoff
  • PeerLifetimeManager - Stale peer cleanup
  • AddressRotationHandler - BLE address rotation handling for WearOS

These now have Rust implementations in hive-btle with UniFFI exports available. The Android SDK should migrate to use the Rust implementations for:

  • Consistent behavior across platforms
  • Single source of truth
  • Reduced maintenance burden

UniFFI Types Available

ReconnectionManager

\\\`kotlin
// Config
ReconnectionConfig(
baseDelayMs: Long = 2000,
maxDelayMs: Long = 60000,
maxAttempts: UInt = 10,
checkIntervalMs: Long = 5000
)

// Status enum
sealed class ReconnectionStatus {
object Ready
data class Waiting(val remainingMs: Long)
data class Exhausted(val attempts: UInt)
object NotTracked
}

// Manager
class ReconnectionManager {
companion object {
fun new(config: ReconnectionConfig): ReconnectionManager
fun withDefaults(): ReconnectionManager
}
fun trackDisconnection(address: String)
fun isTracked(address: String): Boolean
fun getStatus(address: String): ReconnectionStatus
fun getPeersToReconnect(): List<String>
fun recordAttempt(address: String)
fun onConnectionSuccess(address: String)
fun stopTracking(address: String)
fun clear()
fun trackedCount(): UInt
fun getPeerStats(address: String): PeerReconnectionStats?
fun checkIntervalMs(): Long
}
\\\`

PeerLifetimeManager

\\\`kotlin
// Config
PeerLifetimeConfig(
disconnectedTimeoutMs: Long = 30000,
connectedTimeoutMs: Long = 60000,
cleanupIntervalMs: Long = 10000
)

// Stale reason enum
enum class StaleReason {
DisconnectedTimeout,
ConnectedTimeout
}

// Manager
class PeerLifetimeManager {
companion object {
fun new(config: PeerLifetimeConfig): PeerLifetimeManager
fun withDefaults(): PeerLifetimeManager
}
fun onPeerActivity(address: String, connected: Boolean)
fun onPeerDisconnected(address: String)
fun isTracked(address: String): Boolean
fun isConnected(address: String): Boolean
fun getStalePeers(): List<StalePeerInfo>
fun getStalePeerAddresses(): List<String>
fun removePeer(address: String): Boolean
fun cleanupStalePeers(): List<StalePeerInfo>
fun stats(): PeerLifetimeStats
fun getPeerInfo(address: String): PeerInfo?
fun clear()
fun trackedCount(): UInt
fun cleanupIntervalMs(): Long
}
\\\`

AddressRotationHandler

\\\`kotlin
// Device pattern enum
enum class DevicePattern {
WearTak,
WearOs,
Hive,
Unknown
}

// Manager
class AddressRotationHandler {
companion object {
fun new(): AddressRotationHandler
}
fun registerDevice(name: String, address: String, nodeId: UInt)
fun lookupByName(name: String): UInt?
fun lookupByAddress(address: String): UInt?
fun getAddress(nodeId: UInt): String?
fun getName(nodeId: UInt): String?
fun onDeviceDiscovered(name: String, address: String): DeviceLookupResult?
fun updateAddress(name: String, newAddress: String): Boolean
fun updateName(nodeId: UInt, newName: String)
fun removeDevice(nodeId: UInt)
fun clear()
fun deviceCount(): UInt
fun stats(): AddressRotationStats
}

// Helper functions
fun detectDevicePattern(name: String): DevicePattern
fun isWeartakDevice(name: String): Boolean
fun normalizeWeartakName(name: String): String
fun devicePatternRotatesAddresses(pattern: DevicePattern): Boolean
\\\`

Migration Steps

  1. Update UniFFI bindings - Regenerate Kotlin bindings from hive-btle with the new exports
  2. Replace ReconnectionManager in HiveBtle.kt with the Rust version
  3. Replace PeerLifetimeManager with the Rust version (may be part of existing peer cleanup logic)
  4. Replace AddressRotationHandler with the Rust version (handles WearOS MAC rotation)
  5. Update tests to use the new API (signatures are similar but not identical)
  6. Remove deprecated Kotlin implementations

Notes

  • The Rust implementations have been tested and match the Android behavior
  • Duration values are exposed as milliseconds (u64) for FFI compatibility
  • The managers use thread-safe Mutex wrappers internally
  • All tests pass in the Rust codebase

Related

  • hive-btle commit: (pending commit after this issue)
  • See src/uniffi_bindings.rs lines 1233-1650+ for the exports

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗