@@ -4,6 +4,7 @@ package bluetooth
4
4
5
5
import (
6
6
"errors"
7
+ "path"
7
8
"sort"
8
9
"strings"
9
10
"time"
@@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
242
243
return errDupNotif
243
244
}
244
245
246
+ // Figure out the path of the device that this characteristic belongs to
247
+ devicePath := dbus .ObjectPath (path .Dir (path .Dir (string (c .characteristic .Path ()))))
248
+
245
249
// Start watching for changes in the Value property.
246
250
c .property = make (chan * dbus.Signal )
247
251
c .adapter .bus .Signal (c .property )
@@ -257,12 +261,22 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
257
261
for sig := range c .property {
258
262
if sig .Name == "org.freedesktop.DBus.Properties.PropertiesChanged" {
259
263
interfaceName := sig .Body [0 ].(string )
260
- if interfaceName != "org.bluez.GattCharacteristic1" {
264
+
265
+ if interfaceName == "org.bluez.Device1" && sig .Path == devicePath {
266
+ changes := sig .Body [1 ].(map [string ]dbus.Variant )
267
+
268
+ if connected , ok := changes ["Connected" ].Value ().(bool ); ok && ! connected {
269
+ c .EnableNotifications (nil )
270
+ return
271
+ }
272
+ } else if interfaceName != "org.bluez.GattCharacteristic1" {
261
273
continue
262
274
}
275
+
263
276
if sig .Path != c .characteristic .Path () {
264
277
continue
265
278
}
279
+
266
280
changes := sig .Body [1 ].(map [string ]dbus.Variant )
267
281
if value , ok := changes ["Value" ].Value ().([]byte ); ok {
268
282
callback (value )
@@ -280,6 +294,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
280
294
281
295
err := c .adapter .bus .RemoveMatchSignal (c .propertiesChangedMatchOption )
282
296
c .adapter .bus .RemoveSignal (c .property )
297
+ close (c .property )
283
298
c .property = nil
284
299
return err
285
300
}
0 commit comments