Add service-filtered BLE scanning on macOS#126
Open
BBleae wants to merge 1 commit intogo-ble:masterfrom
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new public scan helper that allows callers to request service-filtered BLE scans, and enables the macOS (CoreBluetooth) backend to forward those UUID filters natively so devices that only reveal needed scan response data under filtered scans can be discovered.
Changes:
- Added
ble.ScanWithServices(...)with a fallback to existingScan(...)when no services are provided or the backend doesn’t support native service-filtered scanning. - Introduced an internal
serviceScanneropt-in interface for backends to implement. - Updated the Darwin backend to support
ScanWithServicesby passing the service UUID list through to CoreBluetooth scan.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| gatt.go | Adds ScanWithServices helper and internal serviceScanner interface with fallback behavior. |
| darwin/device.go | Implements service-filtered scanning on macOS by forwarding UUID filters to CoreBluetooth. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+113
to
+117
| defer untrap(trap(ctx)) | ||
|
|
||
| if len(services) == 0 { | ||
| return Scan(ctx, allowDup, h, f) | ||
| } |
Comment on lines
129
to
132
| go func() { | ||
| d.cm.Scan(nil, &cbgo.CentralManagerScanOpts{ | ||
| d.cm.Scan(uuidsToCbgoUUIDs(services), &cbgo.CentralManagerScanOpts{ | ||
| AllowDuplicates: allowDup, | ||
| }) |
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
ScanWithServiceshelper alongsideScanMotivation
Some BLE devices on macOS only expose the information needed for connection after CoreBluetooth is asked to scan with specific service UUID filters. Browsers do this for Web Bluetooth service-filtered scans, but
go-ble/blecurrently always starts CoreBluetooth scans without service filters.This makes it difficult to discover devices that rely on scan responses unless callers reimplement backend-specific logic.
Implementation
gatt.gonow exposesScanWithServices(ctx, allowDup, handler, filter, services).If the active backend implements the internal
serviceScannerinterface, the requested service UUIDs are forwarded natively. Otherwise the helper falls back to the existingScanbehavior.On darwin,
Device.ScanWithServicesforwards the UUID slice toCBCentralManager.scanForPeripheralsWithServicesviacbgo, while keeping the rest of the scan flow unchanged.Testing
go test ./...