Repository navigation
Android/Windows/Apple: check the link before the GATT lookup - #399
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
characteristicNotFoundon 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
deviceDisconnectedeven when the cached tree no longer holds the characteristic.characteristicNotFoundnow means the link was up and the attribute is unknown.CBPeripheral.statechecked after the lookuprequestMtu/readRssigated too;discoverServicesuses the same "Device Disconnected" messageautoConnectclient keeps its cacheisLinkUpbefore the lookup;addIfLinkedUpstays the lock-protected authority for the #394 raceHasLiveLinkbeforeFetchCharacteristicForOperationA client the plugin has torn down still reports
deviceNotFoundon Android and Windows. ReportingdeviceDisconnectedthere too would need remembered per-address state and only renames the error, so it is left out.Also in this PR
NSAccessorySetupKitSupportskeys removed. With those keys, iPadOS 26.6.1 did not show the broad Bluetooth prompt on a fresh install:CBCentralManagerstayedpoweredOffand scanning found nothing for 90 s. Without them the prompt appears.example/README.mddocuments that the AccessorySetupKit picker needs the keys back and that one build cannot do both on a first install.example/integration_test/apple_gatt_disconnect_test.dartruns the connected path (read, descriptor read/write, subscribe, MTU, RSSI) and then both disconnect windows — raced againstdisconnect()and after it settles — against a real peripheral.Verification
:universal_ble:testDebugUnitTest, 129 passed. New cases: an unknown characteristic on a dropped link reportsdeviceDisconnected, and on a live link still reportscharacteristicNotFound.flutter analyzeclean;flutter test341 passed.deviceDisconnectedfor all six.deviceDisconnectedin both windows. The MTU/RSSI additions were exercised on macOS only; it is the same Swift source.Notes
example/README.md).