diff --git a/gap_linux.go b/gap_linux.go index 2e4ecc3..ae32d01 100644 --- a/gap_linux.go +++ b/gap_linux.go @@ -345,6 +345,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err // were connected between the two calls the signal wouldn't be picked up. signal := make(chan *dbus.Signal) a.bus.Signal(signal) + defer close(signal) defer a.bus.RemoveSignal(signal) propertiesChangedMatchOptions := []dbus.MatchOption{dbus.WithMatchInterface("org.freedesktop.DBus.Properties")} a.bus.AddMatchSignal(propertiesChangedMatchOptions...) diff --git a/gattc_linux.go b/gattc_linux.go index 8ffbab3..e5a51c2 100644 --- a/gattc_linux.go +++ b/gattc_linux.go @@ -4,6 +4,7 @@ package bluetooth import ( "errors" + "path" "sort" "strings" "time" @@ -242,6 +243,9 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err return errDupNotif } + // Figure out the path of the device that this characteristic belongs to + devicePath := dbus.ObjectPath(path.Dir(path.Dir(string(c.characteristic.Path())))) + // Start watching for changes in the Value property. c.property = make(chan *dbus.Signal) c.adapter.bus.Signal(c.property) @@ -257,12 +261,21 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err for sig := range c.property { if sig.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" { interfaceName := sig.Body[0].(string) - if interfaceName != "org.bluez.GattCharacteristic1" { + + switch { + case interfaceName == "org.bluez.Device1" && sig.Path == devicePath: + changes := sig.Body[1].(map[string]dbus.Variant) + + if connected, ok := changes["Connected"].Value().(bool); ok && !connected { + c.EnableNotifications(nil) + return + } + case interfaceName != "org.bluez.GattCharacteristic1": continue - } - if sig.Path != c.characteristic.Path() { + case sig.Path != c.characteristic.Path(): continue } + changes := sig.Body[1].(map[string]dbus.Variant) if value, ok := changes["Value"].Value().([]byte); ok { callback(value) @@ -280,6 +293,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err err := c.adapter.bus.RemoveMatchSignal(c.propertiesChangedMatchOption) c.adapter.bus.RemoveSignal(c.property) + close(c.property) c.property = nil return err }