add support multiple services and notifications

This commit is contained in:
Irieda Noboru 2021-03-11 17:12:14 +09:00
parent 28f9f4e69e
commit 745b4b857e
2 changed files with 18 additions and 16 deletions

@ -90,8 +90,7 @@ type Device struct {
servicesChan chan error servicesChan chan error
charsChan chan error charsChan chan error
services map[UUID]*DeviceService services map[UUID]*DeviceService
characteristics map[UUID]*DeviceCharacteristic
} }
// Connect starts a connection attempt to the given peripheral device address. // Connect starts a connection attempt to the given peripheral device address.
@ -159,14 +158,16 @@ func (pd *peripheralDelegate) DidDiscoverCharacteristics(prph cbgo.Peripheral, s
// for a Peripheral receives a notification with a new value, // for a Peripheral receives a notification with a new value,
// or receives a value for a read request. // or receives a value for a read request.
func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) { func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) {
svcuuid, _ := ParseUUID(chr.Service().UUID().String())
uuid, _ := ParseUUID(chr.UUID().String()) uuid, _ := ParseUUID(chr.UUID().String())
if char, ok := pd.d.characteristics[uuid]; ok { if svc, ok := pd.d.services[svcuuid]; ok {
if err == nil && char.callback != nil { if char, ok := svc.characteristics[uuid]; ok {
go char.callback(chr.Value()) if err == nil && char.callback != nil {
} go char.callback(chr.Value())
}
if char.readChan != nil { if char.readChan != nil {
char.readChan <- err char.readChan <- err
}
} }
} }
} }

@ -14,7 +14,7 @@ import (
// //
// Passing a nil slice of UUIDs will return a complete list of // Passing a nil slice of UUIDs will return a complete list of
// services. // services.
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { func (d *Device) DiscoverServices(uuids []UUID) ([]*DeviceService, error) {
d.prph.DiscoverServices([]cbgo.UUID{}) d.prph.DiscoverServices([]cbgo.UUID{})
// clear cache of services // clear cache of services
@ -23,7 +23,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
// wait on channel for service discovery // wait on channel for service discovery
select { select {
case <-d.servicesChan: case <-d.servicesChan:
svcs := []DeviceService{} svcs := []*DeviceService{}
for _, dsvc := range d.prph.Services() { for _, dsvc := range d.prph.Services() {
dsvcuuid, _ := ParseUUID(dsvc.UUID().String()) dsvcuuid, _ := ParseUUID(dsvc.UUID().String())
// add if in our original list // add if in our original list
@ -41,13 +41,13 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
} }
} }
svc := DeviceService{ svc := &DeviceService{
uuidWrapper: dsvcuuid, uuidWrapper: dsvcuuid,
device: d, device: d,
service: dsvc, service: dsvc,
} }
svcs = append(svcs, svc) svcs = append(svcs, svc)
d.services[svc.uuidWrapper] = &svc d.services[svc.uuidWrapper] = svc
} }
return svcs, nil return svcs, nil
case <-time.NewTimer(10 * time.Second).C: case <-time.NewTimer(10 * time.Second).C:
@ -65,7 +65,8 @@ type DeviceService struct {
device *Device device *Device
service cbgo.Service service cbgo.Service
characteristics map[UUID]*DeviceCharacteristic
} }
// UUID returns the UUID for this DeviceService. // UUID returns the UUID for this DeviceService.
@ -88,7 +89,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
s.device.prph.DiscoverCharacteristics(cbuuids, s.service) s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
// clear cache of characteristics // clear cache of characteristics
s.device.characteristics = make(map[UUID]*DeviceCharacteristic) s.characteristics = make(map[UUID]*DeviceCharacteristic)
// wait on channel for characteristic discovery // wait on channel for characteristic discovery
select { select {
@ -119,7 +120,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
}, },
} }
chars = append(chars, char) chars = append(chars, char)
s.device.characteristics[char.uuidWrapper] = &char s.characteristics[char.uuidWrapper] = &char
} }
return chars, nil return chars, nil
case <-time.NewTimer(10 * time.Second).C: case <-time.NewTimer(10 * time.Second).C: