@@ -14,6 +14,7 @@ import (
14
14
15
15
var errAdvertisementNotStarted = errors .New ("bluetooth: advertisement is not started" )
16
16
var errAdvertisementAlreadyStarted = errors .New ("bluetooth: advertisement is already started" )
17
+ var errAdaptorNotPowered = errors .New ("bluetooth: adaptor is not powered" )
17
18
18
19
// Unique ID per advertisement (to generate a unique object path).
19
20
var advertisementID uint64
@@ -231,20 +232,34 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
231
232
callback (a , makeScanResult (rawprops ))
232
233
case "org.freedesktop.DBus.Properties.PropertiesChanged" :
233
234
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 :
242
261
continue
243
262
}
244
- for k , v := range changes {
245
- device [k ] = v
246
- }
247
- callback (a , makeScanResult (device ))
248
263
}
249
264
case <- cancelChan :
250
265
continue
0 commit comments