Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS 12+: Accept service UUIDs when scanning for devices #88

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions gap_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ func (ad Address) Set(val string) {

// Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern
// is to cancel the scan when a particular device has been found.
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
//
// In macOS Monterey (12.x) and above, you must provide a list of services listed
// in the advertising data of the devices you want to discover. Otherwise,
// CoreBluetooth will never return a discovered device.
func (a *Adapter) Scan(serviceUUIDs []UUID, callback func(*Adapter, ScanResult)) (err error) {
macosVersion := 12 // TODO: actually fetch this value from the OS

if callback == nil {
return errors.New("must provide callback to Scan function")
}
Expand All @@ -50,9 +56,30 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
// read from it. If it succeeds, the scan is stopped.
a.scanChan = make(chan error)

a.cm.Scan(nil, &cbgo.CentralManagerScanOpts{
AllowDuplicates: false,
})
if len(serviceUUIDs) == 0 {
if macosVersion < 12 {
return errors.New("one or more serviceUUIDs must be specified for CoreBluetooth to return any results")
}
a.cm.Scan(nil, &cbgo.CentralManagerScanOpts{
AllowDuplicates: false,
})

} else {
// convert service UUIDs to CBUUIDs
suids := make([]cbgo.UUID, len(serviceUUIDs))
for i, uuid := range serviceUUIDs {
b := uuid.Bytes()
u, err := cbgo.UUID128(b[:])
if err != nil {
return err
}
suids[i] = u
}

a.cm.Scan(suids, &cbgo.CentralManagerScanOpts{
AllowDuplicates: false,
})
}

// Check whether the scan is stopped. This is necessary to avoid a race
// condition between the signal channel and the cancelScan channel when
Expand Down