Skip to content

Commit d2c99ce

Browse files
committed
linux: handle adaptor power state to return an error if the adaptor is powered off while scanning
Signed-off-by: deadprogram <[email protected]>
1 parent b82048c commit d2c99ce

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

gap_linux.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
var errAdvertisementNotStarted = errors.New("bluetooth: advertisement is not started")
1616
var errAdvertisementAlreadyStarted = errors.New("bluetooth: advertisement is already started")
17+
var errAdaptorNotPowered = errors.New("bluetooth: adaptor is not powered")
1718

1819
// Unique ID per advertisement (to generate a unique object path).
1920
var advertisementID uint64
@@ -231,20 +232,34 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
231232
callback(a, makeScanResult(rawprops))
232233
case "org.freedesktop.DBus.Properties.PropertiesChanged":
233234
interfaceName := sig.Body[0].(string)
234-
if interfaceName != "org.bluez.Device1" {
235-
continue
236-
}
237-
changes := sig.Body[1].(map[string]dbus.Variant)
238-
device, ok := devices[sig.Path]
239-
if !ok {
240-
// This shouldn't happen, but protect against it just in
241-
// case.
235+
switch interfaceName {
236+
case "org.bluez.Adapter1":
237+
// check power state
238+
changes := sig.Body[1].(map[string]dbus.Variant)
239+
for k, v := range changes {
240+
if k == "Powered" && !v.Value().(bool) {
241+
// adapter is powered off, stop the scan
242+
close(cancelChan)
243+
return errAdaptorNotPowered
244+
}
245+
}
246+
247+
case "org.bluez.Device1":
248+
changes := sig.Body[1].(map[string]dbus.Variant)
249+
device, ok := devices[sig.Path]
250+
if !ok {
251+
// This shouldn't happen, but protect against it just in
252+
// case.
253+
continue
254+
}
255+
for k, v := range changes {
256+
device[k] = v
257+
}
258+
callback(a, makeScanResult(device))
259+
260+
default:
242261
continue
243262
}
244-
for k, v := range changes {
245-
device[k] = v
246-
}
247-
callback(a, makeScanResult(device))
248263
}
249264
case <-cancelChan:
250265
continue

0 commit comments

Comments
 (0)