From 2484d255bd6afc3b9ca333ed664e58c25ea90d7e Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 16:44:45 +0100 Subject: [PATCH 1/6] fix: connect bluetooth nearby payments at close range A paying phone connected only at -60 dBm or stronger, and Android advertised at ADVERTISE_TX_POWER_LOW (-15 dBm). In paired QA an iPhone saw a Galaxy S22 at -75 to -63 dBm side by side, connecting only at -58 dBm with the top corners pressed together. Accept -75 dBm, as on iOS, and advertise at high power. A Bluetooth request still opens the send review and never QuickPay. Verified: none (needs two physical phones) --- app/src/main/java/to/bitkit/nearby/NearbyAdvertiser.kt | 2 +- app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt | 2 +- app/src/test/java/to/bitkit/nearby/NearbyAdvertiserTest.kt | 5 ++++- changelog.d/next/2223.fixed.md | 1 + docs/nearby-payments.md | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 changelog.d/next/2223.fixed.md diff --git a/app/src/main/java/to/bitkit/nearby/NearbyAdvertiser.kt b/app/src/main/java/to/bitkit/nearby/NearbyAdvertiser.kt index 262a125b8f..bb912e8f2a 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyAdvertiser.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyAdvertiser.kt @@ -145,7 +145,7 @@ class NearbyAdvertiser( val advertiser = bluetoothManager.adapter?.bluetoothLeAdvertiser ?: return onFailed() val settings = AdvertiseSettings.Builder() .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) - .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_LOW) + .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .setConnectable(true) .build() val data = AdvertiseData.Builder() diff --git a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt index 745cdf95e8..460595e2af 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt @@ -19,7 +19,7 @@ object NearbyProtocol { const val CHUNK_SIZE = 512 const val CHUNK_COUNT = 8 const val MAX_REQUEST_BYTES = CHUNK_SIZE * CHUNK_COUNT - const val MIN_RSSI = -60 + const val MIN_RSSI = -75 /** The request as served, or null when it is empty or too large to serve. */ fun encodeRequest(request: String): ByteArray? { diff --git a/app/src/test/java/to/bitkit/nearby/NearbyAdvertiserTest.kt b/app/src/test/java/to/bitkit/nearby/NearbyAdvertiserTest.kt index 087afb34fd..19f41a44d4 100644 --- a/app/src/test/java/to/bitkit/nearby/NearbyAdvertiserTest.kt +++ b/app/src/test/java/to/bitkit/nearby/NearbyAdvertiserTest.kt @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattServer import android.bluetooth.BluetoothGattServerCallback import android.bluetooth.BluetoothManager +import android.bluetooth.le.AdvertiseSettings import android.bluetooth.le.BluetoothLeAdvertiser import android.content.Context import android.os.Looper @@ -58,7 +59,9 @@ class NearbyAdvertiserTest { reportServiceAdded(BluetoothGatt.GATT_SUCCESS) - verify(advertiser).startAdvertising(any(), any(), any()) + val settings = argumentCaptor() + verify(advertiser).startAdvertising(settings.capture(), any(), any()) + assertEquals(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH, settings.lastValue.txPowerLevel) assertEquals(0, failures) } diff --git a/changelog.d/next/2223.fixed.md b/changelog.d/next/2223.fixed.md new file mode 100644 index 0000000000..0ddf5c3921 --- /dev/null +++ b/changelog.d/next/2223.fixed.md @@ -0,0 +1 @@ +Bluetooth nearby payments now connect with the phones held close, without pressing them together. diff --git a/docs/nearby-payments.md b/docs/nearby-payments.md index f91d6f7f77..9c27d797ca 100644 --- a/docs/nearby-payments.md +++ b/docs/nearby-payments.md @@ -44,7 +44,7 @@ advertiser and reads the request. - The request is the same URI as the NFC tag. - Advertising runs only while the Receive screen is in the foreground and the nearby setting is on. - Central: scans for the service UUID while a payment scanner is open, connects to the strongest - advertiser at or above -60 dBm, reads meta and the needed chunks, then disconnects. + advertiser at or above -75 dBm, reads meta and the needed chunks, then disconnects. - A request received over Bluetooth always opens the normal send review; QuickPay does not apply, because Bluetooth range lets a device that is not the merchant's answer. - Only `bitcoin:`, `lightning:` BOLT11 and USDT `ethereum:` requests are taken over Bluetooth. LNURL and From 29e519a2bdd2138ea303e13e19326476690d9aa6 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 16:58:11 +0100 Subject: [PATCH 2/6] chore: name the changelog fragment after this pr Verified: none (file rename) --- changelog.d/next/{2223.fixed.md => 2225.fixed.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog.d/next/{2223.fixed.md => 2225.fixed.md} (100%) diff --git a/changelog.d/next/2223.fixed.md b/changelog.d/next/2225.fixed.md similarity index 100% rename from changelog.d/next/2223.fixed.md rename to changelog.d/next/2225.fixed.md From eec5a62bb51483306d4a4985215e4cbf204d37bd Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 17:45:38 +0100 Subject: [PATCH 3/6] test: cover the nearby scanner signal gate Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.nearby.*' detekt --- app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt | 3 +++ app/src/main/java/to/bitkit/nearby/NearbyScanner.kt | 2 +- app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt index 460595e2af..e4446fa899 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt @@ -21,6 +21,9 @@ object NearbyProtocol { const val MAX_REQUEST_BYTES = CHUNK_SIZE * CHUNK_COUNT const val MIN_RSSI = -75 + /** Whether a phone seen at [rssi] dBm is close enough to read. */ + fun isInRange(rssi: Int): Boolean = rssi >= MIN_RSSI + /** The request as served, or null when it is empty or too large to serve. */ fun encodeRequest(request: String): ByteArray? { val bytes = request.toByteArray(Charsets.UTF_8) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt index d7abee3008..51292c4c15 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt @@ -98,7 +98,7 @@ class NearbyScanner( } private fun onCandidate(device: BluetoothDevice, rssi: Int) { - if (!isStarted || session != null || rssi < NearbyProtocol.MIN_RSSI) return + if (!isStarted || session != null || !NearbyProtocol.isInRange(rssi)) return val until = skippedUntil[device.address] if (until != null && nowMillis() < until) return val isFirst = candidates.isEmpty() diff --git a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt index b2981746d8..f22f1b4cd7 100644 --- a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt +++ b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt @@ -2,6 +2,7 @@ package to.bitkit.nearby import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test @@ -68,4 +69,11 @@ class NearbyProtocolTest { fun `decode fails when chunks do not match the length`() { assertNull(NearbyProtocol.decode(listOf("abc".toByteArray()), 4)) } + + @Test + fun `reads phones down to -75 dBm`() { + assertTrue(NearbyProtocol.isInRange(-74)) + assertTrue(NearbyProtocol.isInRange(-75)) + assertFalse(NearbyProtocol.isInRange(-76)) + } } From 71250fa44d8931194d64be59509f81885b75a8a2 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 19:39:00 +0100 Subject: [PATCH 4/6] fix: read the closest nearby phone by its median signal Gate and rank each phone on its lower median RSSI over a 500 ms window (at least 3 readings) instead of its strongest single reading, so one spike from a phone 10 m away no longer opens its request. Keep the per-phone 10 s cooldown across scanner restarts, so Back from the Send sheet review does not read the same phone again at once. Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.nearby.*' detekt lintDevDebug; two-phone run (Pixel 10 Pro, iPhone Air): 10 m+ not read, side by side read both ways, Send sheet Back stays on Send --- .../java/to/bitkit/nearby/NearbyProtocol.kt | 15 +++++ .../java/to/bitkit/nearby/NearbyScanner.kt | 55 ++++++++++++------- .../to/bitkit/nearby/NearbyProtocolTest.kt | 13 +++++ .../java/to/bitkit/nearby/NearbySkipsTest.kt | 16 ++++++ docs/nearby-payments.md | 5 +- 5 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 app/src/test/java/to/bitkit/nearby/NearbySkipsTest.kt diff --git a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt index e4446fa899..df44b6f42c 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt @@ -21,9 +21,24 @@ object NearbyProtocol { const val MAX_REQUEST_BYTES = CHUNK_SIZE * CHUNK_COUNT const val MIN_RSSI = -75 + const val MIN_READINGS = 3 + /** Whether a phone seen at [rssi] dBm is close enough to read. */ fun isInRange(rssi: Int): Boolean = rssi >= MIN_RSSI + /** + * The phone to read among [readings] (each phone's RSSI values over the selection window): the one with the + * strongest median, if that median is in range. A phone needs [MIN_READINGS] values, so one spike never wins. + */ + fun closest(readings: Map>): K? = readings + .mapNotNull { (key, values) -> + if (values.size < MIN_READINGS) return@mapNotNull null + val median = values.sorted()[(values.size - 1) / 2] + if (isInRange(median)) key to median else null + } + .maxByOrNull { it.second } + ?.first + /** The request as served, or null when it is empty or too large to serve. */ fun encodeRequest(request: String): ByteArray? { val bytes = request.toByteArray(Charsets.UTF_8) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt index 51292c4c15..3382dca5ef 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt @@ -28,19 +28,20 @@ import kotlin.time.Duration.Companion.seconds /** * Finds the closest nearby Bitkit wallet that serves a payment request and reads that request. * - * While started it scans for the nearby service. Once an advertiser at or above - * [NearbyProtocol.MIN_RSSI] is seen, it waits [CANDIDATE_WINDOW] for others, connects to the - * strongest one and reads the request. Only payment requests ([nearbyPaymentRequest]) reach - * [onRequest], on the main thread. A device is then skipped for [RETRY_AFTER], so the scanner keeps listening for - * others, and reads the device again if the screen stayed open after rejecting its request. + * While started it scans for the nearby service. From the first advertiser seen it collects + * readings for [CANDIDATE_WINDOW], connects to the one with the strongest median signal at or above + * [NearbyProtocol.MIN_RSSI] ([NearbyProtocol.closest]) and reads the request. + * Only payment requests ([nearbyPaymentRequest]) reach [onRequest], on the main thread. A device is then + * skipped for [RETRY_AFTER], so the scanner keeps listening for others, and reads the device again if the + * screen stayed open after rejecting its request. */ class NearbyScanner( private val context: Context, private val onRequest: (String) -> Unit, ) { private val handler = Handler(Looper.getMainLooper()) - private val candidates = mutableMapOf>() - private val skippedUntil = mutableMapOf() + private val devices = mutableMapOf() + private val readings = mutableMapOf>() private var isScanning = false private var isStarted = false private var session: ReadSession? = null @@ -72,8 +73,8 @@ class NearbyScanner( handler.removeCallbacksAndMessages(null) session?.close() session = null - candidates.clear() - skippedUntil.clear() + devices.clear() + readings.clear() } @SuppressLint("MissingPermission") // Scheduled only by a started scanner. @@ -98,20 +99,20 @@ class NearbyScanner( } private fun onCandidate(device: BluetoothDevice, rssi: Int) { - if (!isStarted || session != null || !NearbyProtocol.isInRange(rssi)) return - val until = skippedUntil[device.address] - if (until != null && nowMillis() < until) return - val isFirst = candidates.isEmpty() - val previous = candidates[device.address]?.second - if (previous == null || rssi > previous) candidates[device.address] = device to rssi + if (!isStarted || session != null) return + if (NearbySkips.isSkipped(device.address, nowMillis())) return + val isFirst = readings.isEmpty() + devices[device.address] = device + readings.getOrPut(device.address) { mutableListOf() } += rssi if (isFirst) handler.postDelayed(::connectToClosest, CANDIDATE_WINDOW.inWholeMilliseconds) } @SuppressLint("MissingPermission") // Scheduled only by a started scanner. private fun connectToClosest() { - val closest = candidates.values.maxByOrNull { it.second }?.first ?: return - candidates.clear() - if (!isStarted) return + val closest = NearbyProtocol.closest(readings)?.let(devices::get) + devices.clear() + readings.clear() + if (closest == null || !isStarted) return stopScan() session = ReadSession(closest).also { it.open() } } @@ -125,7 +126,7 @@ class NearbyScanner( if (request != null && payment == null) { Logger.warn("Ignored nearby request that is not a payment", context = TAG) } - skippedUntil[device.address] = nowMillis() + RETRY_AFTER.inWholeMilliseconds + NearbySkips.skip(device.address, nowMillis() + RETRY_AFTER.inWholeMilliseconds) if (!isStarted) return if (payment != null) onRequest(payment) startScan() @@ -229,9 +230,23 @@ class NearbyScanner( private companion object { const val TAG = "NearbyScanner" const val MTU = 517 - val CANDIDATE_WINDOW: Duration = 400.milliseconds + val CANDIDATE_WINDOW: Duration = 500.milliseconds val READ_TIMEOUT: Duration = 8.seconds val RETRY_AFTER: Duration = 10.seconds val SCAN_RETRY_DELAY: Duration = 5.seconds } } + +/** + * When each device may be read again. It outlives a [NearbyScanner], since screens stop and recreate theirs when + * they come back (Back from the send review), and would otherwise read the same phone again at once. + */ +internal object NearbySkips { + private val skippedUntil = mutableMapOf() + + fun skip(address: String, untilMillis: Long) { + skippedUntil[address] = untilMillis + } + + fun isSkipped(address: String, nowMillis: Long): Boolean = (skippedUntil[address] ?: 0) > nowMillis +} diff --git a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt index f22f1b4cd7..ebf324f7b8 100644 --- a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt +++ b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt @@ -76,4 +76,17 @@ class NearbyProtocolTest { assertTrue(NearbyProtocol.isInRange(-75)) assertFalse(NearbyProtocol.isInRange(-76)) } + + @Test + fun `picks the phone with the strongest median in range`() { + val readings = mapOf( + "near" to listOf(-60, -62, -90), + "peak" to listOf(-50, -70, -72), + "spike" to listOf(-84, -75, -83, -82), + "few" to listOf(-40, -40), + ) + + assertEquals("near", NearbyProtocol.closest(readings)) + assertNull(NearbyProtocol.closest(readings - "near" - "peak")) + } } diff --git a/app/src/test/java/to/bitkit/nearby/NearbySkipsTest.kt b/app/src/test/java/to/bitkit/nearby/NearbySkipsTest.kt new file mode 100644 index 0000000000..2e6e93746e --- /dev/null +++ b/app/src/test/java/to/bitkit/nearby/NearbySkipsTest.kt @@ -0,0 +1,16 @@ +package to.bitkit.nearby + +import org.junit.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class NearbySkipsTest { + @Test + fun `skips a device until its deadline, across scanners`() { + NearbySkips.skip("AA:BB", untilMillis = 10_000) + + assertTrue(NearbySkips.isSkipped("AA:BB", nowMillis = 9_999)) + assertFalse(NearbySkips.isSkipped("AA:BB", nowMillis = 10_000)) + assertFalse(NearbySkips.isSkipped("CC:DD", nowMillis = 0)) + } +} diff --git a/docs/nearby-payments.md b/docs/nearby-payments.md index 9c27d797ca..370ed5848b 100644 --- a/docs/nearby-payments.md +++ b/docs/nearby-payments.md @@ -43,8 +43,9 @@ advertiser and reads the request. - Maximum request size: 4096 bytes. - The request is the same URI as the NFC tag. - Advertising runs only while the Receive screen is in the foreground and the nearby setting is on. -- Central: scans for the service UUID while a payment scanner is open, connects to the strongest - advertiser at or above -75 dBm, reads meta and the needed chunks, then disconnects. +- Central: scans for the service UUID while a payment scanner is open, collects each advertiser's + RSSI for 500 ms, connects to the one with the strongest median (at least 3 readings) at or above + -75 dBm, reads meta and the needed chunks, then disconnects. - A request received over Bluetooth always opens the normal send review; QuickPay does not apply, because Bluetooth range lets a device that is not the merchant's answer. - Only `bitcoin:`, `lightning:` BOLT11 and USDT `ethereum:` requests are taken over Bluetooth. LNURL and From 7b69f70c78acc22c42571581c6132e5160b52804 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 20:02:49 +0100 Subject: [PATCH 5/6] fix: keep collecting nearby readings for a slow phone When the 500 ms window ends with no phone in range and one still has fewer than 3 readings, keep collecting for another window, up to 2 s, so a close phone whose readings arrive slowly (Battery Saver) still gets read. Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.nearby.*' detekt lintDevDebug --- .../main/java/to/bitkit/nearby/NearbyProtocol.kt | 3 +++ .../main/java/to/bitkit/nearby/NearbyScanner.kt | 15 +++++++++++++-- .../java/to/bitkit/nearby/NearbyProtocolTest.kt | 6 ++++++ docs/nearby-payments.md | 5 +++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt index df44b6f42c..27f2977217 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt @@ -39,6 +39,9 @@ object NearbyProtocol { .maxByOrNull { it.second } ?.first + /** Whether a phone in [readings] still has fewer than [MIN_READINGS] values, so the window should go on. */ + fun needsMoreReadings(readings: Map>): Boolean = readings.values.any { it.size < MIN_READINGS } + /** The request as served, or null when it is empty or too large to serve. */ fun encodeRequest(request: String): ByteArray? { val bytes = request.toByteArray(Charsets.UTF_8) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt index 3382dca5ef..8df66e808d 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt @@ -30,7 +30,8 @@ import kotlin.time.Duration.Companion.seconds * * While started it scans for the nearby service. From the first advertiser seen it collects * readings for [CANDIDATE_WINDOW], connects to the one with the strongest median signal at or above - * [NearbyProtocol.MIN_RSSI] ([NearbyProtocol.closest]) and reads the request. + * [NearbyProtocol.MIN_RSSI] ([NearbyProtocol.closest]) and reads the request. While a phone has too few + * readings to judge, the window goes on, up to [MAX_CANDIDATE_WINDOW]. * Only payment requests ([nearbyPaymentRequest]) reach [onRequest], on the main thread. A device is then * skipped for [RETRY_AFTER], so the scanner keeps listening for others, and reads the device again if the * screen stayed open after rejecting its request. @@ -43,6 +44,7 @@ class NearbyScanner( private val devices = mutableMapOf() private val readings = mutableMapOf>() private var isScanning = false + private var windowStartedAt = 0L private var isStarted = false private var session: ReadSession? = null @@ -104,12 +106,20 @@ class NearbyScanner( val isFirst = readings.isEmpty() devices[device.address] = device readings.getOrPut(device.address) { mutableListOf() } += rssi - if (isFirst) handler.postDelayed(::connectToClosest, CANDIDATE_WINDOW.inWholeMilliseconds) + if (isFirst) { + windowStartedAt = nowMillis() + handler.postDelayed(::connectToClosest, CANDIDATE_WINDOW.inWholeMilliseconds) + } } @SuppressLint("MissingPermission") // Scheduled only by a started scanner. private fun connectToClosest() { val closest = NearbyProtocol.closest(readings)?.let(devices::get) + val isWindowOpen = nowMillis() - windowStartedAt < MAX_CANDIDATE_WINDOW.inWholeMilliseconds + if (closest == null && isStarted && isWindowOpen && NearbyProtocol.needsMoreReadings(readings)) { + handler.postDelayed(::connectToClosest, CANDIDATE_WINDOW.inWholeMilliseconds) + return + } devices.clear() readings.clear() if (closest == null || !isStarted) return @@ -231,6 +241,7 @@ class NearbyScanner( const val TAG = "NearbyScanner" const val MTU = 517 val CANDIDATE_WINDOW: Duration = 500.milliseconds + val MAX_CANDIDATE_WINDOW: Duration = 2.seconds val READ_TIMEOUT: Duration = 8.seconds val RETRY_AFTER: Duration = 10.seconds val SCAN_RETRY_DELAY: Duration = 5.seconds diff --git a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt index ebf324f7b8..e7274e66e2 100644 --- a/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt +++ b/app/src/test/java/to/bitkit/nearby/NearbyProtocolTest.kt @@ -89,4 +89,10 @@ class NearbyProtocolTest { assertEquals("near", NearbyProtocol.closest(readings)) assertNull(NearbyProtocol.closest(readings - "near" - "peak")) } + + @Test + fun `keeps collecting while a phone has too few readings`() { + assertTrue(NearbyProtocol.needsMoreReadings(mapOf("slow" to listOf(-40, -41), "far" to listOf(-90, -91, -92)))) + assertFalse(NearbyProtocol.needsMoreReadings(mapOf("far" to listOf(-90, -91, -92)))) + } } diff --git a/docs/nearby-payments.md b/docs/nearby-payments.md index 370ed5848b..3d550e6c73 100644 --- a/docs/nearby-payments.md +++ b/docs/nearby-payments.md @@ -44,8 +44,9 @@ advertiser and reads the request. - The request is the same URI as the NFC tag. - Advertising runs only while the Receive screen is in the foreground and the nearby setting is on. - Central: scans for the service UUID while a payment scanner is open, collects each advertiser's - RSSI for 500 ms, connects to the one with the strongest median (at least 3 readings) at or above - -75 dBm, reads meta and the needed chunks, then disconnects. + RSSI for 500 ms (up to 2 s while one has fewer than 3 readings), connects to the one with the + strongest median (at least 3 readings) at or above -75 dBm, reads meta and the needed chunks, + then disconnects. - A request received over Bluetooth always opens the normal send review; QuickPay does not apply, because Bluetooth range lets a device that is not the merchant's answer. - Only `bitcoin:`, `lightning:` BOLT11 and USDT `ethereum:` requests are taken over Bluetooth. LNURL and From 4cf623fb1bc7f396af85ee04a7e6d2cecd0b3d20 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Tue, 1 Dec 2026 22:00:29 +0100 Subject: [PATCH 6/6] docs: say the nearby window extends only while no phone qualifies Verified: ./gradlew detekt --- app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt | 2 +- app/src/main/java/to/bitkit/nearby/NearbyScanner.kt | 5 +++-- docs/nearby-payments.md | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt index 27f2977217..f854f9c612 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyProtocol.kt @@ -39,7 +39,7 @@ object NearbyProtocol { .maxByOrNull { it.second } ?.first - /** Whether a phone in [readings] still has fewer than [MIN_READINGS] values, so the window should go on. */ + /** Whether a phone in [readings] has fewer than [MIN_READINGS] values; the window goes on while none qualifies. */ fun needsMoreReadings(readings: Map>): Boolean = readings.values.any { it.size < MIN_READINGS } /** The request as served, or null when it is empty or too large to serve. */ diff --git a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt index 8df66e808d..7ce8b42a8f 100644 --- a/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt +++ b/app/src/main/java/to/bitkit/nearby/NearbyScanner.kt @@ -30,8 +30,9 @@ import kotlin.time.Duration.Companion.seconds * * While started it scans for the nearby service. From the first advertiser seen it collects * readings for [CANDIDATE_WINDOW], connects to the one with the strongest median signal at or above - * [NearbyProtocol.MIN_RSSI] ([NearbyProtocol.closest]) and reads the request. While a phone has too few - * readings to judge, the window goes on, up to [MAX_CANDIDATE_WINDOW]. + * [NearbyProtocol.MIN_RSSI] ([NearbyProtocol.closest]) and reads the request. While no phone qualifies yet + * and some phone has fewer than [NearbyProtocol.MIN_READINGS], the window goes on in [CANDIDATE_WINDOW] + * steps, up to [MAX_CANDIDATE_WINDOW]. * Only payment requests ([nearbyPaymentRequest]) reach [onRequest], on the main thread. A device is then * skipped for [RETRY_AFTER], so the scanner keeps listening for others, and reads the device again if the * screen stayed open after rejecting its request. diff --git a/docs/nearby-payments.md b/docs/nearby-payments.md index 3d550e6c73..a72946ee3e 100644 --- a/docs/nearby-payments.md +++ b/docs/nearby-payments.md @@ -44,9 +44,9 @@ advertiser and reads the request. - The request is the same URI as the NFC tag. - Advertising runs only while the Receive screen is in the foreground and the nearby setting is on. - Central: scans for the service UUID while a payment scanner is open, collects each advertiser's - RSSI for 500 ms (up to 2 s while one has fewer than 3 readings), connects to the one with the - strongest median (at least 3 readings) at or above -75 dBm, reads meta and the needed chunks, - then disconnects. + RSSI for 500 ms, connects to the one with the strongest median (at least 3 readings) at or above + -75 dBm, reads meta and the needed chunks, then disconnects. While none qualifies yet and some + advertiser has fewer than 3 readings, it keeps collecting in 500 ms steps, up to 2 s. - A request received over Bluetooth always opens the normal send review; QuickPay does not apply, because Bluetooth range lets a device that is not the merchant's answer. - Only `bitcoin:`, `lightning:` BOLT11 and USDT `ethereum:` requests are taken over Bluetooth. LNURL and