Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the EnableNotifications Method, add the parameter to specifing… #293

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions gattc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading