Skip to content

Android/Windows/Apple: check the link before the GATT lookup - #399

Merged
fotiDim merged 6 commits into
mainfrom
fix/394-link-check-before-lookup
Nov 24, 2026
Merged

fotiDim merged 6 commits into
mainfrom
fix/394-link-check-before-lookup

Conversation

@fotiDim

@fotiDim fotiDim commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #396. #394's fail-fast check was only reachable in the window where the platform still held the peripheral's attribute tree: CoreBluetooth discards it when the link drops, and Android/Windows decided from the lookup before checking the link. After a settled drop the caller therefore still got characteristicNotFound on Apple, and a platform-dependent answer on Android/Windows.

This makes the link the deciding factor everywhere: the check runs before the characteristic/descriptor lookup, so a request on a device whose link is down reports deviceDisconnected even when the cached tree no longer holds the characteristic. characteristicNotFound now means the link was up and the attribute is unknown.

Platform Before After
Apple CBPeripheral.state checked after the lookup checked first; requestMtu/readRssi gated too; discoverServices uses the same "Device Disconnected" message
Android lookup first; an autoConnect client keeps its cache isLinkUp before the lookup; addIfLinkedUp stays the lock-protected authority for the #394 race
Windows lookup first HasLiveLink before FetchCharacteristicForOperation

A client the plugin has torn down still reports deviceNotFound on Android and Windows. Reporting deviceDisconnected there too would need remembered per-address state and only renames the error, so it is left out.

Also in this PR

  • iOS example: deployment target 13.1 → 15.0 (Xcode 26 refuses 13.1) and the NSAccessorySetupKitSupports keys removed. With those keys, iPadOS 26.6.1 did not show the broad Bluetooth prompt on a fresh install: CBCentralManager stayed poweredOff and scanning found nothing for 90 s. Without them the prompt appears. example/README.md documents that the AccessorySetupKit picker needs the keys back and that one build cannot do both on a first install.
  • Manual HIL harness: example/integration_test/apple_gatt_disconnect_test.dart runs the connected path (read, descriptor read/write, subscribe, MTU, RSSI) and then both disconnect windows — raced against disconnect() and after it settles — against a real peripheral.

Verification

  • Android: :universal_ble:testDebugUnitTest, 129 passed. New cases: an unknown characteristic on a dropped link reports deviceDisconnected, and on a live link still reports characteristicNotFound.
  • Apple: sources typecheck against the macOS and iOS engine frameworks; the three isolated helpers pass (28/28, 24/24, 31/31).
  • flutter analyze clean; flutter test 341 passed.
  • macOS 26.6.2, FS-7A25F8: connected read/descriptor/subscribe/MTU/RSSI pass; the raced and the settled disconnect report deviceDisconnected for all six.
  • iPadOS 26.6.1, same device: the same four GATT requests report deviceDisconnected in both windows. The MTU/RSSI additions were exercised on macOS only; it is the same Swift source.
  • Windows: compile-checked by CI (not buildable on this machine).

Notes

  • No version bump; CHANGELOG entries are under the unreleased 3.4.1 section.
  • The iPad run needs the Bluetooth prompt accepted once; on a fresh install the example's ASK keys suppress it (see example/README.md).

PR #396 left the Apple half of the fail-fast check untested: the guard needs
a connected CBPeripheral, and there is no unit-level substitute. Add two
manual integration tests to the example app, to be run on a Mac or an iOS
device against a nearby peripheral that exposes a readable, a notifiable and
a descriptor-bearing characteristic (the file documents the one used).

  apple_gatt_disconnect_test.dart
      Deterministic. Verifies the connected path (read, descriptor read,
      descriptor write through CCCD, subscribe/unsubscribe) and then both
      windows a link loss produces: requests raced against disconnect()
      while the peripheral is .disconnecting and still holds its attribute
      tree (all four report deviceDisconnected, the #396 guard), and requests
      after the disconnect completes (all four report characteristicNotFound,
      because CoreBluetooth has discarded CBPeripheral.services and the
      lookup runs before the guard). Both are refused locally, never handed
      to CoreBluetooth.

  apple_link_loss_test.dart
      The real link-loss case. Connects (optionally with autoConnect), waits
      for the peripheral to drop the link on its own, then immediately issues
      the same four requests and reports their codes. A drop has to be
      triggered on the peripheral side; the file explains how to set the wait
      and what NO_LINK_LOSS means.

Neither test runs in CI. Observed on macOS 26.6.2 and iPadOS 26.6.1 with
identical results. On iPadOS the Bluetooth prompt only surfaced once the
example's NSAccessorySetupKitSupports keys were absent, so the test waits for
poweredOn before scanning.
The #394 guard #396 added checked `CBPeripheral.state` after the
characteristic/descriptor lookup. CoreBluetooth discards a peripheral's
attribute tree when the link drops, so by the time a request arrives after
the drop the lookup fails first and the caller still got
characteristicNotFound for a characteristic it had just used. The guard only
fired in the .disconnecting window, where the tree is still cached.

Move the state check ahead of the lookup in readValue, writeValue,
setNotifiable, readDescriptorValue and writeDescriptorValue, matching Linux
and Web. A request on a disconnected peripheral now reports deviceDisconnected
whether or not CoreBluetooth still holds the tree; an unknown characteristic
on a connected peripheral still reports characteristicNotFound.

Android keeps the lookup-first order because an autoConnect client stays open
with its services cached, which is what makes its link check reachable; a
client it closes reports deviceNotFound instead. README and CHANGELOG now
describe the platform split that way.

Verified with example/integration_test/apple_gatt_disconnect_test.dart on
macOS 26.6.2 and iPadOS 26.6.1 against a real peripheral: the connected path
(read, descriptor read/write, subscribe) passes, and both the raced and the
settled disconnect report deviceDisconnected for all four operations.
Two local fixes the iPad HIL run needed, kept in the repo:

- Raise the example's iOS deployment target from 13.1 to 15.0 (Runner project
  and Podfile). Xcode 26 refuses to build 13.1; the plugin's own podspec keeps
  its 13.1 minimum for library consumers.
- Drop the NSAccessorySetupKitSupports / NSAccessorySetupBluetoothServices /
  NSAccessorySetupBluetoothNames keys. With them, iPadOS 26.6.1 did not show
  the broad Bluetooth permission prompt on a fresh install: CBCentralManager
  stayed poweredOff and scanning found nothing for 90 s. Without them the
  prompt appears and the scan works. The example README explains that the
  AccessorySetupKit picker needs the keys added back, and that one build
  cannot do both on a first install.
Steps 1 and 3 of the cross-platform alignment; step 2 (what a closed client
reports) is a separate change.

- Android: readValue, writeValue, setNotifiable, readDescriptorValue and
  writeDescriptorValue check isLinkUp(gatt) before the characteristic lookup.
  An autoConnect client keeps its cached tree across a link loss, so the
  lookup could still fail with characteristicNotFound for a characteristic the
  caller had used; the answer now depends on the connection, not the cache.
  submitGattRequest/addIfLinkedUp keep their lock-protected check as the
  authoritative one for the #394 race.
- Windows: HasLiveLink now runs before FetchCharacteristicForOperation in
  ReadValue, WriteValue, ReadDescriptorValue, WriteDescriptorValue and
  SetNotifiableAsync, for the same reason.
- Apple: requestMtu and readRssi refuse a disconnected peripheral with
  deviceDisconnected instead of reaching CoreBluetooth, and discoverServices
  uses the same "Device Disconnected" message, so the gated set and the error
  text match Android.

Result: a known device whose link is down reports deviceDisconnected whether
or not the cached attribute tree still holds the characteristic;
characteristicNotFound now means the link was up. Clients the plugin has
already forgotten still report deviceNotFound on Android and Windows (step 2).

Verification: Android unit tests 129 passed (two new cases: an unknown
characteristic on a dropped link reports disconnected, and on a live link it
still reports not-found); Apple sources typecheck for macOS and iOS, the three
isolated helpers pass, flutter analyze clean, flutter test 341 passed. The
macOS HIL run against FS-7A25F8 now exercises six gated calls: MTU and RSSI
succeed while connected and report deviceDisconnected in both the raced and
the settled disconnect. Windows is compile-checked by CI.
The precedence rule was repeated as a five-line comment at every call site
(five times in the Android plugin, three plus two in the Swift one). State it
once next to isLinkUp and leave a single line where the check is made.

Delete example/integration_test/apple_link_loss_test.dart. It waits for a
peripheral to drop the link on its own, which the test device never does
without help, so it cannot run unattended; apple_gatt_disconnect_test.dart
covers the raced and the settled disconnect deterministically.
- README: drop the claim that characteristicNotFound implies a live link.
  Android reports it for a read the stack refuses to start, which the
  parallel-queueId flow can produce on a live link.
- requestMtu: document that a disconnected device fails with
  deviceDisconnected, including iOS/macOS where the value is local.
- Retarget the Android value-length comments to the reordered checks: a
  wrong device, attribute or operation now outranks an oversized payload.
- Add the descriptor precedence tests, dropped link and live link, as the
  characteristic paths already have.
- Warn in the example's picker sheet that this build omits the
  AccessorySetupKit keys, so the picker cannot be shown.
- HIL test: record unexpected error types instead of aborting the run, and
  disconnect in a teardown so a failed assertion leaves no link open.
@fotiDim
fotiDim merged commit 32fce88 into main Nov 24, 2026
4 checks passed
@fotiDim
fotiDim deleted the fix/394-link-check-before-lookup branch November 24, 2026 11:33
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.

1 participant