Repository navigation
fix(android): match GATT services and characteristics by instance id - #114
Merged
Merged
Conversation
The native-to-wrapper lookup compared UUIDs only, so a peripheral that lists one service UUID several times made every GATT callback for it throw MultipleServicesFoundException inside the native callback and the caller time out. Services and characteristics are now matched by UUID and instance id; descriptors stay matched by UUID within their own characteristic.
There was a problem hiding this comment.
🔵 Needs a closer look
Android GATT callback identity changes lack hardware validation, requiring human checks of reads, writes, notifications, and rediscovery on peripherals with duplicate and unique UUIDs.
No open issues 🎉
What changed in this PR
Addresses #107 in Plugin.Bluetooth’s Android GATT client by distinguishing services and characteristics that share a UUID, preventing ambiguous callback lookups that cause operation timeouts.
Changes:
- Match services and characteristics by both UUID and native
InstanceIdin the predicates used by callbacks and discovery. - Add comments explaining why instance IDs are needed; public UUID-only lookup behavior and descriptor matching remain unchanged.
The PR reports a successful Android Release build; validation on physical hardware remains pending.
| File | Description |
|---|---|
| Bluetooth.Maui.Platforms.Droid/Scanning/AndroidBluetoothRemoteService.cs | Adds instance-ID matching for characteristics with duplicate UUIDs. |
| Bluetooth.Maui.Platforms.Droid/Scanning/AndroidBluetoothRemoteDevice.cs | Adds instance-ID matching for services with duplicate UUIDs. |
Review effort: Balanced
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.
Summary
The Android native-to-wrapper lookup compared UUIDs only. A peripheral that lists the same service UUID more than once got one wrapper per native service, and every GATT callback for a characteristic in such a service then found several matching wrappers. Services and characteristics are now matched by UUID and instance id, which is how Android tells attributes with the same UUID apart.
Fixes #107.
Why
On
main, for a peripheral that lists one service UUID several times:AndroidBluetoothRemoteDevice.GetService(BluetoothGattService)finds several wrappers and throwsMultipleServicesFoundExceptioninside the native callback;BluetoothUnhandledExceptionListener, nothing completes the pending read, write or notification, and the caller sees a timeout.The same shape applies to duplicate characteristic UUIDs inside one service (
AndroidBluetoothRemoteService.GetCharacteristic).Change
AndroidBluetoothRemoteDevice.AreRepresentingTheSameObject: UUID andBluetoothGattService.InstanceId.AndroidBluetoothRemoteService.AreRepresentingTheSameObject: UUID andBluetoothGattCharacteristic.InstanceId.OnServicesExplorationSucceeded,OnCharacteristicsExplorationSucceeded), so a re-discovery matches the same wrappers instead of merging or duplicating them. The instance id is stable across re-discovery; an object handle is not guaranteed to be.GetService(Guid)still throwsMultipleServicesFoundExceptionfor a duplicated UUID, by design;IBluetoothRemoteServicehas no instance discriminator, and exposing one is a separate API decision.Change Type
refa)Affected Areas
Bluetooth.Maui)Behavior And Compatibility
On a peripheral with duplicated service or characteristic UUIDs, reads, writes and notifications on Android now complete instead of timing out, and
MultipleServicesFoundExceptionis no longer raised from inside the GATT callbacks. Peripherals without duplicated UUIDs resolve to the same wrapper as before.Platform Notes
AndroidBluetoothBroadcaster/AndroidBluetoothLocalService) has its own native lookups and is not touched here.Tests
Validation notes:
Bluetooth.Maui.Platforms.DroidRelease--no-incrementalbuilds with 0 warnings and 0 errors.BluetoothGattServiceandBluetoothGattCharacteristicneed the Android runtime, and the repo has no Android test host.Bluetooth.Maui.Sample.Scannerapp built in Release frommain(ce8de70) and from this branch (80765da), against a throwaway CoreBluetooth test peripheral on a Mac that lists two primary services with the same UUID, each with a readable characteristic (same UUID, valuesinstance-1andinstance-2). The phone lists both services after exploring. Same steps on both builds: connect, explore, open a duplicated service, read its characteristic.main: the read raises "Multiple services have been found matching criteria" from inside the native callback (the unhandled-exception listener) and no value arrives. This reproduces [BUG] [Android] A peripheral that exposes the same service UUID more than once makes every read, write and notification on it time out #107.instance-1(69 6E 73 74 61 6E 63 65 2D 31) and the second readsinstance-2(… 2D 32), so each callback resolves to its own wrapper.00002a00of the Mac host): returns the expected value.Documentation
No public behavior is documented that this changes.
Logging / Diagnostics
Risks And Follow-ups
Risk level:
Medium because the predicate is on the path of every Android GATT callback and it was validated on one phone and one Android version: a native object whose instance id differs between discovery and a callback would stop resolving. Android documents the instance id as unique and stable for this purpose, and the device run above resolved both a duplicated and a unique service correctly.
Known limitations or deferred follow-ups:
IBluetoothRemoteService(API decision).Checklist
type (scope): short imperativeand is <= 72 chars