Skip to content

Commit

Permalink
update the EnableNotifications Method, add the parameter to specifing… (
Browse files Browse the repository at this point in the history
#293)

Update the EnableNotifications Method, add the parameter to specifing the notify or indicate mode of characteristics, just for windows os.

---------

Co-authored-by: liming <[email protected]>
  • Loading branch information
lemongGH and liming authored Jan 10, 2025
1 parent 48b1dfe commit c6dfccb
Showing 1 changed file with 48 additions and 13 deletions.
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 {
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

0 comments on commit c6dfccb

Please sign in to comment.