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 3 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
61 changes: 48 additions & 13 deletions gattc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ 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")
errNoNotifyOrIndicate = errors.New("bluetooth: notify or indicate not supported")
errInvalidNotificationMode = errors.New("bluetooth: invalid notification mode")
errEnableNotificationsFailed = errors.New("bluetooth: enable notifications failed")
)

type NotificationMode = genericattributeprofile.GattCharacteristicProperties

const (
NotificationModeNotify NotificationMode = genericattributeprofile.GattCharacteristicPropertiesNotify
NotificationModeIndicate NotificationMode = genericattributeprofile.GattCharacteristicPropertiesIndicate
)

// DiscoverServices starts a service discovery procedure. Pass a list of service
// UUIDs you are interested in to this function. Either a slice of all services
// is returned (of the same length as the requested UUIDs and in the same
Expand Down Expand Up @@ -360,14 +370,44 @@ func (c DeviceCharacteristic) Read(data []byte) (int, error) {
return len(readBuffer), nil
}

// EnableNotifications enables notifications in the Client Characteristic
// EnableNotifications enables notifications or indicate in the Client Characteristic
// Configuration Descriptor (CCCD). And it favors Notify over Indicate.
func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnableNotifications enables notifications or indications, whatever is available. And it favors notifications over indications. This PR should not change that behaviour.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i will update to keep the original behavior

var err error
if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify == 0 {
err = c.EnableNotificationsWithMode(NotificationModeNotify, callback)
} else if c.properties&genericattributeprofile.GattCharacteristicPropertiesIndicate == 0 {
err = c.EnableNotificationsWithMode(NotificationModeIndicate, callback)
} else {
return errNoNotifyOrIndicate
}

if err != nil {
return err
}
return nil
}

// EnableNotificationsWithMode 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
// changes. And you can select the notify/indicate mode as you need.
func (c DeviceCharacteristic) EnableNotificationsWithMode(mode NotificationMode, callback func(buf []byte)) error {
configValue := genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueNone
if mode == NotificationModeIndicate {
if c.properties&genericattributeprofile.GattCharacteristicPropertiesIndicate == 0 {
return errNoIndicate
}
// set to indicate mode
configValue = genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueIndicate
} else if mode == NotificationModeNotify {
if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify == 0 {
return errNoNotify
}
// set to notify mode
configValue = genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueNotify
} else {
return errInvalidNotificationMode
}

// listen value changed event
Expand Down Expand Up @@ -404,12 +444,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
return err
}

var writeOp *foundation.IAsyncOperation
if c.properties&genericattributeprofile.GattCharacteristicPropertiesNotify != 0 {
writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueNotify)
} else {
writeOp, err = c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(genericattributeprofile.GattClientCharacteristicConfigurationDescriptorValueIndicate)
}
writeOp, err := c.characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(configValue)
if err != nil {
return err
}
Expand Down
Loading