@@ -4,6 +4,7 @@ package bluetooth
44
55import (
66 "errors"
7+ "path"
78 "sort"
89 "strings"
910 "time"
@@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
242243 return errDupNotif
243244 }
244245
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+
245249 // Start watching for changes in the Value property.
246250 c.property = make(chan *dbus.Signal)
247251 c.adapter.bus.Signal(c.property)
@@ -257,12 +261,22 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
257261 for sig := range c.property {
258262 if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" {
259263 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" {
261273 continue
262274 }
275+
263276 if sig.Path != c.characteristic.Path() {
264277 continue
265278 }
279+
266280 changes := sig.Body[1].(map[string]dbus.Variant)
267281 if value, ok := changes["Value"].Value().([]byte); ok {
268282 callback(value)
@@ -280,6 +294,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
280294
281295 err := c.adapter.bus.RemoveMatchSignal(c.propertiesChangedMatchOption)
282296 c.adapter.bus.RemoveSignal(c.property)
297+ close(c.property)
283298 c.property = nil
284299 return err
285300 }
0 commit comments