Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ func generateDescAttributes(d *Descriptor, h uint16) attr {
props: d.props,
pvt: d,
}
if len(d.valuestr) > 0 {
a.value = []byte(d.valuestr)
}
return a
}
17 changes: 15 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ type Descriptor struct {
props Property // enabled properties
secure Property // security enabled properties

h uint16
value []byte
h uint16
value []byte
valuestr string

rhandler ReadHandler
whandler WriteHandler
Expand Down Expand Up @@ -367,6 +368,18 @@ func (d *Descriptor) SetValue(b []byte) {
copy(d.value, b)
}

// SetStringValue makes the descriptor support read requests, and returns a static value.
// SetStringValue must be called before the containing service is added to a server.
// SetStringValue panics if the descriptor has already configured with a ReadHandler.
func (d *Descriptor) SetStringValue(s string) {
if d.rhandler != nil {
panic("descriptor has been configured with a read handler")
}
d.props |= CharRead
// d.secure |= CharRead
d.valuestr = s
}

// HandleRead makes the descriptor support read requests, and routes read requests to h.
// HandleRead must be called before the containing service is added to a server.
// HandleRead panics if the descriptor has been configured with a static value.
Expand Down
8 changes: 7 additions & 1 deletion device_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,14 @@ func (d *device) AddService(s *Service) error {
// skip CCCD
continue
}
var v interface{}
if len(d.valuestr) > 0 {
v = d.valuestr
} else {
v = d.value
}
xd := xpc.Dict{
"kCBMsgArgData": d.value,
"kCBMsgArgData": v,
"kCBMsgArgUUID": reverse(d.uuid.b),
}
xds = append(xds, xd)
Expand Down
3 changes: 1 addition & 2 deletions examples/service/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ func NewBatteryService() *gatt.Service {
lv--
})

// FIXME: this cause connection interrupted on Mac.
// Characteristic User Description
// c.AddDescriptor(gatt.UUID16(0x2901)).SetValue([]byte("Battery level between 0 and 100 percent"))
c.AddDescriptor(gatt.UUID16(0x2901)).SetStringValue("Battery level between 0 and 100 percent")

// Characteristic Presentation Format
c.AddDescriptor(gatt.UUID16(0x2904)).SetValue([]byte{4, 1, 39, 173, 1, 0, 0})
Expand Down