Skip to content

fix: answer every nearby read and keep the gatt server open - #2258

Open
ovitrif wants to merge 3 commits into
masterfrom
fix/nearby-gatt-server-reuse
Open

ovitrif wants to merge 3 commits into
masterfrom
fix/nearby-gatt-server-reuse

Conversation

@ovitrif

@ovitrif ovitrif commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2257

Description

Fix for the likely cause. Not tested on devices yet (see Gaps).

The trace in this issue shows each failed attempt as connect, MTU 517, then a disconnect 7.4 to 7.8 s later and a new attempt ~2.7 s after it, which is the iPhone reader's 8 s timeout plus its 2 s retry. The S22's Bluetooth dump still listed six advertising sets for the nearby service (some from November, some from the dev build) and three registrations of the nearby service, and only turning Bluetooth off and on on the S22 fixed it. The GATT database is shared by every server on the phone, so a service left behind by a closed server can take the iPhone's read, and nobody answers it. Android logs read requests only at verbose level, so the trace can't show which step stalled.

  • Should fix the iPhone waiting out its 8 s timeout on a read nobody answers, because a read was answered through the advertiser's current server: it was dropped when that server was gone, and an older server's read was answered on the newer server with a request id that server never issued. Each read is now answered through the server it arrived on, never on a newer one. A read that reaches a server after Receive closed it can't be answered (Android sends nothing once the server is closed), so that payer still waits out its timeout and retries; the app now logs it
  • Should stop leaving the nearby service behind in the Bluetooth stack, because the server was closed without removing its service first; it now clears the service before closing
  • Should stop Receive from closing and reopening the GATT server on each request update (two servers opened 0.8 s apart in the trace), because the Bluetooth effect was keyed on the request; a new request is now served from the open server. Pause, leaving Receive and having nothing to offer still close it. A payer that read the length of one request gets its chunks from that same request when the request changes mid-read

Out of Scope

  • iOS reader: no change. It times out, disconnects and retries as designed. If the device check shows duplicate nearby services reaching the iPhone, it could try each one instead of the first
  • Nearby advertising sets left registered by other builds (the dev app): Bitkit can't remove another app's registrations

Design

N/A — no UI changes.

Preview

N/A — no user-visible changes.

QA Notes

Setup

  • Android build of this PR (mainnet or dev) on a Samsung phone on Android 16 if available, any Android phone otherwise; iPhone on current Bitkit; Bluetooth and Nearby payments on, Nearby devices permission granted.

Journeys

N/A — not drivable; see Manual Tests.

Manual Tests

  • 1. Android Receive → Auto, iPhone home scanner held beside it → amount screen opens within a few seconds — needs two physical phones
  • 2. Close and reopen Receive on Android five times, then scan from the iPhone → amount screen opens without turning Bluetooth off and on — needs two physical phones
  • 3. With Receive open, change the request (add an amount) while the iPhone scans → iPhone opens the new request — needs two physical phones
  • 4. Close Receive on Android while the iPhone is reading the request → note whether the iPhone times out after 8 s or fails at once, and that its next scan works once Receive is open again — needs two physical phones

Automated Checks

  • added NearbyAdvertiseEffectTest.kt — a request change keeps the server open; nothing to offer closes it
  • updated NearbyAdvertiserTest.kt — a new request is served from the open server; a read after stop, or on a closed server, is only tried on its own server, never the new one, and a send that goes nowhere is logged; the service is cleared before close; a payer gets the chunks of the request it read the length of, until it disconnects
  • ran just compile, just lint, just test

Evidence

  • just compile, just lint and just test pass on 89c9dbb; just compile, just lint and the nearby unit tests pass on 5729094.

Gaps

  • 1 to 4 not run: need an Android phone and an iPhone side by side.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Gemini 4 Neon (reasoning: high)
  • Review rounds: 2

An iPhone reading a Galaxy S22's request connected and then timed out
after 8 s until Bluetooth was cycled on the S22, which had kept
services of earlier GATT servers registered.

- Answer each read through the server it arrived on, with an error
  once that server is closed, instead of leaving it unanswered.
- Clear the service before closing the server.
- Serve a new request from the open server instead of reopening it.

Fixes #2257

Verified: just compile, just lint, just test on this change
@ovitrif ovitrif added this to the 3.3.0 milestone Dec 5, 2026
@ovitrif ovitrif self-assigned this Dec 5, 2026
@ovitrif
ovitrif requested review from a team, ben-kaufman and jvsena42 and removed request for a team December 5, 2026 20:21
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T20:24:06.659414Z 039aceb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect the Android nearby-payment Bluetooth serving path in Receive.

The PR appears safe to merge, with a non-blocking gap in tests for request updates during a read.

Findings

  1. P2 Mid-read updates lack coverage ▶

Summary

This PR keeps the nearby GATT server open when the payment request changes.

  • Nearby reads get answered by the server that received them.
  • Receive keeps its Bluetooth server open as payment requests change.

Reviews (1) · Last reviewed commit: "fix: answer every nearby read and keep t..." · Reviewed by Greptile

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 5729094 (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings. Answers each nearby GATT read through the server it arrived on (an error once that server is closed), clears the service before closing, and keeps one server open across request updates. Reviewed 039aceb9c, full tier, static: the Bitkit Android branch is not built on this box, so this is reasoned from the code and from CI.

What I checked, and 5 candidates I ruled out

Read in full: NearbyAdvertiser.kt, NearbyEffects.kt (advertise effect and offerPaymentNearby), NearbyScanner.kt read session, NearbyProtocol.kt, both test files; iOS NearbyPaymentScanner.swift for how a GATT error is handled on the reader side.
CI: build-and-test, lint, detekt, compose UI and the e2e shards green; onchain_boost_receive_widgets still pending when this was posted.

Ruled out

  • Observer re-add misses the new uri: LifecycleRegistry replays events up to the current state to a newly added observer, so re-keying the effect on uri re-runs ON_RESUME and serves the new request; a null uri goes through serveNearby to stop().
  • Stop on dispose skipped or doubled: the uri effect only removes its observer, the advertiser effect stops once on leaving composition, and stop() is idempotent.
  • Read on a binder thread sees a stale generation: serverGeneration is now @Volatile and request already was; a read racing stop() gets GATT_FAILURE through its own server, which the iOS reader treats as a quick retry (fail(error) keeps retrySoon true).
  • Request changes between the meta read and a chunk read: now possible on the kept server, but the reader then sees a length mismatch and drops the read (Android decode returns null, iOS fails it); a mix of two equal-length requests over 512 bytes would still have to pass nearbyPaymentRequest. Narrow, and the old behaviour (server torn down mid-read) also lost that read.
  • clearServices() on a server that failed addService: openServer closes and returns null on that path, so stop() never sees it.

Merge confidence: 4/5, the unit tests cover the server reuse and the error answers, but the fix targets a device-level Bluetooth stack state that only manual tests 1 to 3 can confirm.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run metadata available to the reviewer does not report it)

Keeping the server open across request changes let a payer read the
length of one request and the chunks of the next.

Verified: just compile, just lint, just test on this change
response == null -> BluetoothGatt.GATT_INVALID_OFFSET
else -> BluetoothGatt.GATT_SUCCESS
}
runCatching { owner.sendResponse(device, requestId, status, offset, response) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A read already dispatched on the old server can still reach this line after close(). Android 16 then returns false without sending GATT_FAILURE. The tests verify the call, so they pass even when the response never goes out. If the connection stays up, the payer can still wait for its timeout. Could we log the failed send and check what the payer sees when Receive closes during a read? Android 16 source

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5729094. A false return is now logged, and the tests stub it for a closed server and check the warning. What the payer sees when Receive closes mid-read needs two phones, so it's added as QA case 4 in the description.

A read that reaches a server after close() can't be answered: sendResponse
returns false and sends nothing. Log it, and make the tests model that
instead of counting the call as an answer.

Verified: just compile, just lint, nearby unit tests (just test on the tree before the log test)
@ovitrif
ovitrif requested a review from ben-kaufman December 6, 2026 10:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: iPhone Nearby scan times out reading a Galaxy S22 payment request

3 participants