Skip to content

Commit 97eb13b

Browse files
Lenart12deadprogram
authored andcommitted
linux: handle adaptor power state to return an error if the adaptor is powered off while connecting
1 parent 4777dea commit 97eb13b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

gap_linux.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,34 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
390390
switch sig.Name {
391391
case "org.freedesktop.DBus.Properties.PropertiesChanged":
392392
interfaceName := sig.Body[0].(string)
393-
if interfaceName != "org.bluez.Device1" {
394-
continue
395-
}
396-
if sig.Path != device.device.Path() {
397-
continue
398-
}
399-
changes := sig.Body[1].(map[string]dbus.Variant)
400-
if connected, ok := changes["Connected"].Value().(bool); ok && connected {
401-
close(connectChan)
393+
switch interfaceName {
394+
case "org.bluez.Adapter1":
395+
// check power state
396+
changes := sig.Body[1].(map[string]dbus.Variant)
397+
for k, v := range changes {
398+
if k == "Powered" && !v.Value().(bool) {
399+
// adapter is powered off, stop the scan
400+
err = errAdaptorNotPowered
401+
close(connectChan)
402+
}
403+
}
404+
case "org.bluez.Device1":
405+
if sig.Path != device.device.Path() {
406+
continue
407+
}
408+
changes := sig.Body[1].(map[string]dbus.Variant)
409+
if connected, ok := changes["Connected"].Value().(bool); ok && connected {
410+
close(connectChan)
411+
}
402412
}
403413
}
404414
}
405415
}()
406416
<-connectChan
417+
418+
if err != nil {
419+
return Device{}, err
420+
}
407421
}
408422

409423
if a.connectHandler != nil {

0 commit comments

Comments
 (0)