Skip to content

fix(android): match GATT services and characteristics by instance id - #114

Merged
framinosona merged 1 commit into
mainfrom
framinosona/android-gatt-instance-id
Nov 19, 2026
Merged

framinosona merged 1 commit into
mainfrom
framinosona/android-gatt-instance-id

Conversation

@framinosona

@framinosona framinosona commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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 throws MultipleServicesFoundException inside the native callback;
  • the exception goes to 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 and BluetoothGattService.InstanceId.
  • AndroidBluetoothRemoteService.AreRepresentingTheSameObject: UUID and BluetoothGattCharacteristic.InstanceId.
  • Both predicates are also the ones used by the discovery calls (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.
  • Descriptors are unchanged: they are matched by UUID within their own characteristic, which is unambiguous once the characteristic is.
  • The public GetService(Guid) still throws MultipleServicesFoundException for a duplicated UUID, by design; IBluetoothRemoteService has no instance discriminator, and exposing one is a separate API decision.

Change Type

  • Feature
  • Bug fix
  • Refactor (refa)
  • Docs only
  • CI/build/tooling

Affected Areas

  • Abstractions
  • Core
  • Platform: Android
  • Platform: iOS/macOS
  • Platform: Windows
  • Platform: DotNetCore fallback
  • Facade (Bluetooth.Maui)
  • Documentation

Behavior And Compatibility

  • Public API changed
  • Exception behavior changed
  • DI registration behavior changed
  • Capability matrix impact
  • No externally visible behavior change

On a peripheral with duplicated service or characteristic UUIDs, reads, writes and notifications on Android now complete instead of timing out, and MultipleServicesFoundException is no longer raised from inside the GATT callbacks. Peripherals without duplicated UUIDs resolve to the same wrapper as before.

Platform Notes

  • Android only. iOS/macOS already compare the UUID and the native object handle; Windows has no native-to-wrapper callback lookup. Neither was run against a duplicate-service peripheral.
  • The GATT server side (AndroidBluetoothBroadcaster/AndroidBluetoothLocalService) has its own native lookups and is not touched here.

Tests

  • Unit tests added/updated
  • Integration/smoke tests added/updated
  • Manual platform validation performed
  • Not applicable (explain)

Validation notes:

  • Bluetooth.Maui.Platforms.Droid Release --no-incremental builds with 0 warnings and 0 errors.
  • No unit test: BluetoothGattService and BluetoothGattCharacteristic need the Android runtime, and the repo has no Android test host.
  • Run on a device (an Android 16 phone), with the Bluetooth.Maui.Sample.Scanner app built in Release from main (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, values instance-1 and instance-2). The phone lists both services after exploring. Same steps on both builds: connect, explore, open a duplicated service, read its characteristic.
  • Not run: a real device that lists one service UUID several times (the test peripheral above is a Mac), other Android versions or phones, the notification and write paths (only reads), re-discovery after a service change, iOS and Windows (not touched by this PR), battery and throughput.

Documentation

  • Docs updated in same PR
  • Capability claims verified against implementation
  • Links validated
  • Not applicable (explain)

No public behavior is documented that this changes.

Logging / Diagnostics

  • New logs added
  • EventId range validated
  • No logging changes

Risks And Follow-ups

Risk level:

  • Low
  • Medium
  • High

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:

  • Writes, notifications and re-discovery were not exercised on a device; other phones and Android versions were not tried.
  • Whether to expose an instance discriminator through IBluetoothRemoteService (API decision).
  • The GATT server lookups in the Android broadcaster.

Checklist

  • I reviewed Docs/Best-Practices/Contribution-DoD.md
  • Commit header follows type (scope): short imperative and is <= 72 chars
  • Commit type is one of: feat, fix, refa, perf, docs, ci, chore, test, build
  • Commit body is 1-2 factual sentences (what/why), no emojis, refs, or co-authors
  • If architectural behavior changed, an ADR was added or updated under Docs/Architecture/ADR

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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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 InstanceId in 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

@framinosona
framinosona merged commit 7ee72f6 into main Nov 19, 2026
10 checks passed
@framinosona
framinosona deleted the framinosona/android-gatt-instance-id branch November 19, 2026 11:43
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] [Android] A peripheral that exposes the same service UUID more than once makes every read, write and notification on it time out

2 participants