diff --git a/gattc_windows.go b/gattc_windows.go index 69ff5f4..406502d 100644 --- a/gattc_windows.go +++ b/gattc_windows.go @@ -19,7 +19,8 @@ var ( errNoWriteWithoutResponse = errors.New("bluetooth: write without response not supported") errWriteFailed = errors.New("bluetooth: write failed") errNoRead = errors.New("bluetooth: read not supported") - errNoNotify = errors.New("bluetooth: notify/indicate not supported") + errNoNotify = errors.New("bluetooth: notify not supported") + errNoIndicate = errors.New("bluetooth: indicate not supported") errEnableNotificationsFailed = errors.New("bluetooth: enable notifications failed") ) @@ -360,14 +361,29 @@ func (c DeviceCharacteristic) Read(data []byte) (int, error) { return len(readBuffer), nil } +// EnableNotifications enables notifications in the Client Characteristic +// Configuration Descriptor (CCCD). Just the Notify mode is supported. +func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error { + err := c.EnableNotificationsWithMode(false, callback) + if err != nil { + return err + } + return nil +} + // EnableNotifications enables notifications in the Client Characteristic // Configuration Descriptor (CCCD). This means that most peripherals will send a // notification with a new value every time the value of the characteristic // changes. -func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error { - if (c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify == 0) && - (c.properties&genericattributeprofile.GattCharacteristicPropertiesIndicate == 0) { - return errNoNotify +func (c DeviceCharacteristic) EnableNotificationsWithMode(usingIndicate bool, callback func(buf []byte)) error { + if usingIndicate { + if c.properties&genericattributeprofile.GattCharacteristicPropertiesIndicate == 0 { + return errNoIndicate + } + } else { + if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify == 0 { + return errNoNotify + } } // listen value changed event @@ -405,10 +421,14 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err } var writeOp *foundation.IAsyncOperation - if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify != 0 { - writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueNotify) + if usingIndicate { + if c.properties&genericattributeprofile.GattCharacteristicPropertiesIndicate != 0 { + writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueIndicate) + } } else { - writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueIndicate) + if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify != 0 { + writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueNotify) + } } if err != nil { return err