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
charsChan chan error
services map[UUID]*DeviceService
characteristics map[UUID]*DeviceCharacteristic
services map[UUID]*DeviceService
}
// 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,
// or receives a value for a read request.
func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) {
svcuuid, _ := ParseUUID(chr.Service().UUID().String())
uuid, _ := ParseUUID(chr.UUID().String())
if char, ok := pd.d.characteristics[uuid]; ok {
if err == nil && char.callback != nil {
go char.callback(chr.Value())
}
if char.readChan != nil {
char.readChan <- err
if svc, ok := pd.d.services[svcuuid]; ok {
if char, ok := svc.characteristics[uuid]; ok {
if err == nil && char.callback != nil {
go char.callback(chr.Value())
}
if char.readChan != nil {
char.readChan <- err
}
}
}
}

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