Skip to content

Add service-filtered BLE scanning on macOS#126

Open
BBleae wants to merge 1 commit intogo-ble:masterfrom
BBleae:codex/macos-service-filter-scan
Open

Add service-filtered BLE scanning on macOS#126
BBleae wants to merge 1 commit intogo-ble:masterfrom
BBleae:codex/macos-service-filter-scan

Conversation

@BBleae
Copy link
Copy Markdown

@BBleae BBleae commented Mar 18, 2026

Summary

  • add a public ScanWithServices helper alongside Scan
  • let backends opt into native service-filtered scanning through a small internal interface
  • forward service UUID filters to CoreBluetooth on macOS so matching devices can return scan response data

Motivation

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/ble currently 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.go now exposes ScanWithServices(ctx, allowDup, handler, filter, services).

If the active backend implements the internal serviceScanner interface, the requested service UUIDs are forwarded natively. Otherwise the helper falls back to the existing Scan behavior.

On darwin, Device.ScanWithServices forwards the UUID slice to CBCentralManager.scanForPeripheralsWithServices via cbgo, while keeping the rest of the scan flow unchanged.

Testing

  • go test ./...
  • verified this path against a BLE thermal printer workflow on macOS where unfiltered scans did not surface the needed scan response data

Copilot AI review requested due to automatic review settings March 18, 2026 13:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 existing Scan(...) when no services are provided or the backend doesn’t support native service-filtered scanning.
  • Introduced an internal serviceScanner opt-in interface for backends to implement.
  • Updated the Darwin backend to support ScanWithServices by 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 thread gatt.go
Comment on lines +113 to +117
defer untrap(trap(ctx))

if len(services) == 0 {
return Scan(ctx, allowDup, h, f)
}
Comment thread darwin/device.go
Comment on lines 129 to 132
go func() {
d.cm.Scan(nil, &cbgo.CentralManagerScanOpts{
d.cm.Scan(uuidsToCbgoUUIDs(services), &cbgo.CentralManagerScanOpts{
AllowDuplicates: allowDup,
})
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