@@ -92,9 +92,11 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
9292 for _ , dchar := range s .service .Characteristics () {
9393 uuid , _ := ParseUUID (dchar .UUID ().String ())
9494 char := DeviceCharacteristic {
95- uuidWrapper : uuid ,
96- service : s ,
97- characteristic : dchar ,
95+ deviceCharacteristic : & deviceCharacteristic {
96+ uuidWrapper : uuid ,
97+ service : s ,
98+ characteristic : dchar ,
99+ },
98100 }
99101 chars = append (chars , char )
100102 s .device .characteristics [char .uuidWrapper ] = & char
@@ -108,12 +110,17 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
108110// DeviceCharacteristic is a BLE characteristic on a connected peripheral
109111// device.
110112type DeviceCharacteristic struct {
113+ * deviceCharacteristic
114+ }
115+
116+ type deviceCharacteristic struct {
111117 uuidWrapper
112118
113119 service * DeviceService
114120
115121 characteristic cbgo.Characteristic
116122 callback func (buf []byte )
123+ readChan chan error
117124}
118125
119126// UUID returns the UUID for this DeviceCharacteristic.
@@ -145,3 +152,24 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
145152
146153 return nil
147154}
155+
156+ // Read reads the current characteristic value.
157+ func (c * deviceCharacteristic ) Read (data []byte ) (n int , err error ) {
158+ c .readChan = make (chan error )
159+ c .service .device .prph .ReadCharacteristic (c .characteristic )
160+
161+ // wait for result
162+ select {
163+ case err := <- c .readChan :
164+ c .readChan = nil
165+ if err != nil {
166+ return 0 , err
167+ }
168+ case <- time .NewTimer (10 * time .Second ).C :
169+ c .readChan = nil
170+ return 0 , errors .New ("timeout on Read()" )
171+ }
172+
173+ copy (data , c .characteristic .Value ())
174+ return len (c .characteristic .Value ()), nil
175+ }
0 commit comments