[Android HiveBtle] Migrate connection managers to Rust implementations via UniFFI
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
- Update UniFFI bindings - Regenerate Kotlin bindings from
hive-btlewith the new exports - Replace ReconnectionManager in
HiveBtle.ktwith the Rust version - Replace PeerLifetimeManager with the Rust version (may be part of existing peer cleanup logic)
- Replace AddressRotationHandler with the Rust version (handles WearOS MAC rotation)
- Update tests to use the new API (signatures are similar but not identical)
- 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
Mutexwrappers internally - All tests pass in the Rust codebase
Related
- hive-btle commit: (pending commit after this issue)
- See
src/uniffi_bindings.rslines 1233-1650+ for the exports
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗