Skip to content

Conversation

@callebtc
Copy link
Collaborator

Summary

This PR implements a robust mechanism for GATT clients to signal their stable Peer ID to the server immediately after connection. This solves the issue of redundant connections caused by MAC address rotation and allows servers to enforce strict 1-to-1 connection policies per peer.

Changes

  • Protocol: Added IDENTITY_CHARACTERISTIC_UUID (...C5E) to AppConstants.
  • Server: BluetoothGattServerManager now exposes the Identity Characteristic and handles write requests. It maps the MAC address to the Peer ID and disconnects the new connection if that Peer ID is already connected via another MAC.
  • Client: BluetoothGattClientManager checks for the Identity Characteristic during service discovery and writes its truncated Peer ID (8 bytes).
  • Tracking: BluetoothConnectionTracker supports updating the Peer ID for an existing device connection (setDevicePeerID).
  • Docs: Added full technical specification at docs/PEERID_GATT_SERVER_CLIENT_SPEC.md for cross-platform implementation (iOS).

Testing

  • Verified that clients write their ID upon connection.
  • Verified that servers accept the ID and update their internal map.
  • Verified that duplicate connections from the same Peer ID (different MAC) are rejected.

…cation

Implements a mechanism for GATT clients to signal their Peer ID to the server immediately after connection, allowing for robust deduplication of redundant connections.

Includes:
- New IDENTITY_CHARACTERISTIC_UUID in AppConstants
- Server-side handling of identity writes and deduplication logic
- Client-side active signaling of Peer ID upon service discovery
- Updated BluetoothConnectionTracker to map devices to Peer IDs
- Comprehensive specification document in docs/PEERID_GATT_SERVER_CLIENT_SPEC.md
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c7483956e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +489 to +493
val idBytes = myPeerID.chunked(2).map { it.toInt(16).toByte() }.toByteArray().take(8).toByteArray()
identityChar.value = idBytes
identityChar.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
gatt.writeCharacteristic(identityChar)
Log.d(TAG, "Client: Signaled identity to server $deviceAddress")

Choose a reason for hiding this comment

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

P2 Badge Serialize GATT ops before enabling notifications

Android’s GATT stack only allows one outstanding operation at a time, and this path issues writeCharacteristic(identityChar) and then immediately proceeds to write the CCCD. On devices that enforce serialization (common), the subsequent writeDescriptor can return false/never execute, leaving notifications disabled and the connection unable to receive data even though it is marked “connected.” This is especially risky with WRITE_TYPE_NO_RESPONSE, which provides no completion signal to gate the descriptor write. Consider queueing the CCCD write and only enabling notifications after the identity write completes (or use a write type with a completion callback).

Useful? React with 👍 / 👎.

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.

2 participants