Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
private var startupError: String? = null
private var startupLatch: CountDownLatch? = null
@Volatile private var targetName: String? = null
/** Set when the saved RNode is a device address: connect to it without scanning. */
@Volatile private var targetAddress: String? = null
/** How messages refer to the RNode. */
@Volatile private var label: String = RNodeBleText.FALLBACK_NAME
@Volatile private var adapter: BluetoothAdapter? = null
@Volatile private var scannedName: String? = null

/** The address and name of the RNode this bridge connected to; null until startup succeeds. */
@Volatile var resolvedDevice: ResolvedBleRNode? = null
private set
@Volatile private var scanner: BluetoothLeScanner? = null
private var serverSocket: ServerSocket? = null
private var clientSocket: Socket? = null
Expand All @@ -58,7 +68,7 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
private var writeInProgress = false
private var writeChunkSize = DEFAULT_WRITE_CHUNK
private var adapterStateReceiverRegistered = false
private val retryScan = Runnable { startScan() }
private val retryConnect = Runnable { if (targetAddress != null) connectToAddress() else startScan() }

private val adapterStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Expand All @@ -73,15 +83,9 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
override fun onScanResult(callbackType: Int, result: ScanResult) {
val deviceName = result.scanRecord?.deviceName
?: runCatching { result.device.name }.getOrNull().orEmpty()
val selectedName = targetName
val matches = if (selectedName.isNullOrBlank()) {
deviceName.startsWith("RNode", ignoreCase = true)
} else {
deviceName.equals(selectedName, ignoreCase = true) ||
result.device.address.equals(selectedName, ignoreCase = true)
}
if (!running || !matches) return
if (!running || !matchesTypedRNodeBleTarget(targetName, deviceName, result.device.address)) return

scannedName = deviceName.takeIf(String::isNotBlank)
stopScan()
runCatching {
val connection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand All @@ -106,15 +110,24 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
failStartup("Android Bluetooth scanning failed (code $errorCode).")
} else if (running) {
closeTcpClient()
scheduleScanRetry()
scheduleReconnect()
}
}
}

private val gattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
if (status != BluetoothGatt.GATT_SUCCESS) {
handleGattDisconnect(gatt, "BLE connection failed (status $status).")
handleGattDisconnect(
gatt,
when {
isAuthenticationFailure(status) -> RNodeBleText.notPairedAndroid(label)
// The RNode ended the connection itself, as it does while another device holds it.
targetAddress != null && status == GATT_CONN_TERMINATE_PEER_USER -> RNodeBleText.heldElsewhere(label)
targetAddress != null -> RNodeBleText.notFound(label)
else -> "BLE connection failed (status $status)."
},
)
return
}
when (newState) {
Expand All @@ -140,7 +153,11 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {

override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
if (status != BluetoothGatt.GATT_SUCCESS) {
handleGattDisconnect(gatt, "Could not inspect the BLE RNode services (status $status).")
handleGattDisconnect(
gatt,
if (isAuthenticationFailure(status)) RNodeBleText.notPairedAndroid(label)
else "Could not inspect the BLE RNode services (status $status).",
)
return
}
val service = gatt.getService(SERVICE_UUID)
Expand Down Expand Up @@ -195,13 +212,21 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
if (descriptor.uuid != CCCD_UUID) return
if (status != BluetoothGatt.GATT_SUCCESS) {
handleGattDisconnect(gatt, "The BLE RNode rejected UART notifications (status $status).")
handleGattDisconnect(
gatt,
if (isAuthenticationFailure(status)) RNodeBleText.notPairedAndroid(label)
else "The BLE RNode rejected UART notifications (status $status).",
)
return
}
if (!openTcpServer(gatt)) {
handleGattDisconnect(gatt, "Could not open the local port for the BLE RNode bridge.")
return
}
if (resolvedDevice == null) {
val name = scannedName ?: runCatching { gatt.device.name }.getOrNull().orEmpty()
resolvedDevice = ResolvedBleRNode(gatt.device.address, name)
}
synchronized(lock) {
startupResolved = true
startupError = null
Expand Down Expand Up @@ -239,16 +264,30 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
}
}

/** Returns null after BLE notifications and the loopback listener are ready. */
fun start(targetName: String): String? {
/**
* Returns null after BLE notifications and the loopback listener are ready.
*
* A [target] that is a device address is connected to directly. A device
* name, or an empty target for the first RNode found, is located by
* scanning. [pairedByStarling] makes a missing system pairing an error
* before any connection is attempted.
*/
fun start(target: String, displayName: String? = null, pairedByStarling: Boolean = false): String? {
val trimmedTarget = target.trim()
val address = trimmedTarget.uppercase().takeIf { BluetoothAdapter.checkBluetoothAddress(it) }
val targetName = if (address == null) trimmedTarget else ""
val latch = CountDownLatch(1)
synchronized(lock) {
if (running) return "An Android BLE RNode connection is already active."
running = true
startupResolved = false
startupError = null
startupLatch = latch
this.targetName = targetName.trim().takeIf(String::isNotEmpty)
this.targetName = targetName.takeIf(String::isNotEmpty)
targetAddress = address
label = rnodeBleLabel(RNODE_BLE_PREFIX + trimmedTarget, displayName)
scannedName = null
resolvedDevice = null
}

try {
Expand All @@ -259,20 +298,42 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
val adapter: BluetoothAdapter = manager.adapter
?: return stopWithError("Bluetooth is not available on this device.")
if (!adapter.isEnabled) return stopWithError("Turn on Bluetooth to connect to a BLE RNode.")
scanner = adapter.bluetoothLeScanner
?: return stopWithError("Android could not start Bluetooth LE scanning.")
val filter = ScanFilter.Builder().setServiceUuid(ParcelUuid(SERVICE_UUID)).build()
val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
scanner?.startScan(listOf(filter), settings, scanCallback)
this.adapter = adapter
if (address != null) {
val device = adapter.getRemoteDevice(address)
if (displayName.isNullOrBlank()) {
runCatching { device.name }.getOrNull()?.takeIf(String::isNotBlank)?.let { label = it }
}
if (pairedByStarling && device.bondState != BluetoothDevice.BOND_BONDED) {
return stopWithError(RNodeBleText.pairingLost(label))
}
val connection = device.connectGatt(appContext, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
?: return stopWithError(RNodeBleText.notFound(label))
synchronized(lock) { gatt = connection }
} else {
scanner = adapter.bluetoothLeScanner
?: return stopWithError("Android could not start Bluetooth LE scanning.")
val filter = ScanFilter.Builder().setServiceUuid(ParcelUuid(SERVICE_UUID)).build()
val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
scanner?.startScan(listOf(filter), settings, scanCallback)
}
} catch (error: SecurityException) {
return stopWithError("Allow Starling Bluetooth access to connect to the RNode.")
return stopWithError(
if (address != null) RNodeBleText.permission(label)
else "Allow Starling Bluetooth access to connect to the RNode.",
)
} catch (error: Exception) {
return stopWithError("Could not start the BLE RNode bridge: ${error.message.orEmpty()}")
}

val ready = runCatching { latch.await(STARTUP_TIMEOUT_SECONDS, TimeUnit.SECONDS) }
.getOrDefault(false)
if (!ready) return stopWithError("Timed out finding the BLE RNode. Keep Starling open and check that the radio is nearby.")
if (!ready) {
return stopWithError(
if (address != null) RNodeBleText.notFound(label)
else "Timed out finding the BLE RNode. Keep Starling open and check that the radio is nearby.",
)
}
val error = synchronized(lock) { startupError }
if (error != null) stop()
return error
Expand All @@ -291,13 +352,14 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
startupLatch = null
}
running = false
mainHandler.removeCallbacks(retryScan)
mainHandler.removeCallbacks(retryConnect)
if (adapterStateReceiverRegistered) {
adapterStateReceiverRegistered = false
runCatching { appContext.unregisterReceiver(adapterStateReceiver) }
}
runCatching { scanner?.stopScan(scanCallback) }
scanner = null
adapter = null
runCatching { gatt?.disconnect() }
runCatching { gatt?.close() }
gatt = null
Expand Down Expand Up @@ -338,13 +400,39 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
failStartup(message)
} else if (running) {
closeTcpClient()
scheduleScanRetry()
scheduleReconnect()
}
}

private fun scheduleScanRetry() {
mainHandler.removeCallbacks(retryScan)
mainHandler.postDelayed(retryScan, RECONNECT_SCAN_DELAY_MS)
/** Opens a new connection to the saved address, at startup or after the link drops. */
private fun connectToAddress() {
if (!running) return
val address = targetAddress ?: return
try {
val device = adapter?.getRemoteDevice(address) ?: throw IOException("Bluetooth is unavailable.")
val connection = device.connectGatt(appContext, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
?: throw IOException("Android did not create a BLE connection.")
synchronized(lock) { gatt = connection }
} catch (error: SecurityException) {
handleReconnectFailure(RNodeBleText.permission(label))
} catch (error: Exception) {
handleReconnectFailure(RNodeBleText.notFound(label))
}
}

private fun handleReconnectFailure(message: String) {
val isStarting = synchronized(lock) { !startupResolved }
if (isStarting) {
failStartup(message)
} else if (running) {
closeTcpClient()
scheduleReconnect()
}
}

private fun scheduleReconnect() {
mainHandler.removeCallbacks(retryConnect)
mainHandler.postDelayed(retryConnect, RECONNECT_SCAN_DELAY_MS)
}

private fun stopScan() {
Expand All @@ -368,8 +456,8 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {

/**
* Android does not reliably report a GATT disconnect when Bluetooth is
* switched off. Drop the link the same way as a lost RNode; the scan retry
* then keeps failing until the adapter is back on and the RNode is found.
* switched off. Drop the link the same way as a lost RNode; the reconnect
* retry then keeps failing until the adapter is back on and the RNode is found.
*/
private fun handleAdapterOff() {
if (!running) return
Expand All @@ -379,7 +467,7 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
handleGattDisconnect(activeGatt, message)
} else {
stopScan()
handleScanRestartFailure(message)
handleReconnectFailure(message)
}
}

Expand All @@ -400,7 +488,8 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
*/
private fun handleGattDisconnect(disconnectedGatt: BluetoothGatt, message: String) {
val wasCurrentGatt = synchronized(lock) {
if (gatt !== disconnectedGatt) {
// A direct connection can fail before start() has stored it.
if (gatt != null && gatt !== disconnectedGatt) {
false
} else {
gatt = null
Expand All @@ -417,9 +506,19 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
closeTcpClient()
if (!running) return
val isStillStarting = synchronized(lock) { !startupResolved }
if (isStillStarting) failStartup(message) else startScan()
when {
isStillStarting -> failStartup(message)
targetAddress != null -> scheduleReconnect()
else -> startScan()
}
}

/** Insufficient authentication or encryption, or a failed authentication: the link needs a pairing. */
private fun isAuthenticationFailure(status: Int): Boolean =
status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION ||
status == BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION ||
status == GATT_AUTH_FAIL

private fun onRNodeData(bytes: ByteArray?) {
if (bytes == null || bytes.isEmpty()) return
val destination = synchronized(lock) {
Expand Down Expand Up @@ -621,6 +720,8 @@ internal class AndroidRNodeBluetoothBridge(context: Context) {
private const val TCP_READ_BUFFER_BYTES = 4096
private const val WRITE_RETRY_DELAY_MS = 30L
private const val RECONNECT_SCAN_DELAY_MS = 2_000L
private const val GATT_CONN_TERMINATE_PEER_USER = 0x13
private const val GATT_AUTH_FAIL = 0x89
private val SERVICE_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
private val RX_UUID = UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
private val TX_UUID = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ class AndroidReticulumNodeController(
context: Context,
private val requestBluetoothPermissions: suspend () -> Boolean = { false },
private val requestBlePeeringPermissions: suspend () -> Boolean = { false },
override val rnodeBlePicker: RNodeBlePicker? = null,
) : ReticulumNodeController {
private val appContext = context.applicationContext
private val _state = MutableStateFlow<ReticulumNodeState>(ReticulumNodeState.Stopped)
override val state: StateFlow<ReticulumNodeState> = _state.asStateFlow()
override val isSupported: Boolean = true
override val supportsRNodeInterfaces: Boolean = true
override val supportsBleRNodes: Boolean = true
private val _resolvedBleRNode = MutableStateFlow<ResolvedBleRNode?>(null)
override val resolvedBleRNode: StateFlow<ResolvedBleRNode?> = _resolvedBleRNode.asStateFlow()
override val supportsBlePeering: Boolean =
appContext.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
private var pendingStateQuery: CompletableDeferred<ReticulumNodeState>? = null
Expand All @@ -38,6 +42,13 @@ class AndroidReticulumNodeController(
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action != ReticulumNodeService.ACTION_STATE) return
val state = stateFromIntent(intent)
_resolvedBleRNode.value = if (state is ReticulumNodeState.Running) {
val id = intent.getStringExtra(ReticulumNodeService.EXTRA_RNODE_BLE_ID).orEmpty()
val name = intent.getStringExtra(ReticulumNodeService.EXTRA_RNODE_BLE_NAME).orEmpty()
if (id.isBlank()) null else ResolvedBleRNode(id, name)
} else {
null
}
_state.value = state
pendingStateQuery?.complete(state)
if (state is ReticulumNodeState.Starting || state is ReticulumNodeState.Running) {
Expand Down Expand Up @@ -94,6 +105,10 @@ class AndroidReticulumNodeController(
}

override suspend fun start(config: ReticulumNodeConfig) {
if (config.rnodeInterfaces.any { it.port.isBlank() }) {
_state.value = ReticulumNodeState.Failed(RNodeBleText.CHOOSE_BEFORE_START)
return
}
val bleInterfaces = config.rnodeInterfaces.filter { it.port.startsWith(BLE_PREFIX, ignoreCase = true) }
if (bleInterfaces.size > 1) {
_state.value = ReticulumNodeState.Failed(
Expand Down Expand Up @@ -128,7 +143,7 @@ class AndroidReticulumNodeController(
return
}

config.validationError()?.let {
config.validationError(supportsBleRNodes)?.let {
_state.value = ReticulumNodeState.Failed(it)
return
}
Expand All @@ -139,9 +154,8 @@ class AndroidReticulumNodeController(
}

if (bleInterfaces.isNotEmpty() && !requestBluetoothPermissions()) {
_state.value = ReticulumNodeState.Failed(
"Bluetooth permission is required to connect to a BLE RNode. Grant it and try again.",
)
val radio = bleInterfaces.first()
_state.value = ReticulumNodeState.Failed(RNodeBleText.permission(rnodeBleLabel(radio.port, radio.bleName)))
return
}

Expand Down Expand Up @@ -176,7 +190,9 @@ class AndroidReticulumNodeController(
.put("bandwidth_hz", radio.bandwidthHz)
.put("tx_power_dbm", radio.txPowerDbm)
.put("spreading_factor", radio.spreadingFactor)
.put("coding_rate", radio.codingRate),
.put("coding_rate", radio.codingRate)
.put(ReticulumNodeService.RNODE_BLE_NAME_KEY, radio.bleName.orEmpty())
.put(ReticulumNodeService.RNODE_PAIRED_BY_STARLING_KEY, radio.pairedByStarling),
)
}
val intent = Intent(appContext, ReticulumNodeService::class.java).apply {
Expand Down
Loading
Loading