macos: added characteristic notifications

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2020-08-29 16:15:24 +02:00
parent a7844e1734
commit 95cabe86a1
3 changed files with 20 additions and 4 deletions

@ -1,6 +1,6 @@
# Go Bluetooth
[![CircleCI](https://circleci.com/gh/tinygo-org/bluetooth/tree/master.svg?style=svg)](https://circleci.com/gh/tinygo-org/bluetooth/tree/master)
[![CircleCI](https://circleci.com/gh/tinygo-org/bluetooth/tree/dev.svg?style=svg)](https://circleci.com/gh/tinygo-org/bluetooth/tree/dev)
[![GoDoc](https://godoc.org/github.com/tinygo-org/bluetooth?status.svg)](https://godoc.org/github.com/tinygo-org/bluetooth)
This package attempts to build a cross-platform Bluetooth Low Energy module for Go. It currently supports the following systems:
@ -11,7 +11,7 @@ This package attempts to build a cross-platform Bluetooth Low Energy module for
| Scanning | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Connect to peripheral | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Write peripheral characteristics | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Receive notifications | :x: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Receive notifications | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| Advertisement | :x: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Local services | :x: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| Local characteristics | :x: | :heavy_check_mark: | :heavy_check_mark: | :x: |
@ -52,7 +52,7 @@ Flashing will then need to be done a bit differently, using the CMSIS-DAP interf
## API stability
**The API is not stable!** Because many features are not yet implemented and some platforms (e.g. MacOS) are not yet supported, it's hard to say what a good API will be. Therefore, if you want stability you should pick a particular git commit and use that. Go modules can be useful for this purpose.
**The API is not stable!** Because many features are not yet implemented and some platforms (e.g. Windows and macOS) are not yet fully supported, it's hard to say what a good API will be. Therefore, if you want stability you should pick a particular git commit and use that. Go modules can be useful for this purpose.
Some things that will probably change:

@ -85,6 +85,9 @@ type Device struct {
servicesChan chan error
charsChan chan error
services map[UUID]*DeviceService
characteristics map[UUID]*DeviceCharacteristic
}
// Connect starts a connection attempt to the given peripheral device address.
@ -142,5 +145,10 @@ func (pd *peripheralDelegate) DidDiscoverCharacteristics(prph cbgo.Peripheral, s
// DidUpdateValueForCharacteristic is called when the characteristic for a Service
// for a Peripheral receives a notification with a new value.
func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) {
// TODO: implement this
uuid, _ := ParseUUID(chr.UUID().String())
if char, ok := pd.d.characteristics[uuid]; ok {
if char != nil && char.callback != nil {
go char.callback(chr.Value())
}
}
}

@ -20,6 +20,9 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
d.prph.DiscoverServices(cbuuids)
// clear cache of services
d.services = make(map[UUID]*DeviceService)
// wait on channel for service discovery
select {
case <-d.servicesChan:
@ -32,6 +35,7 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
service: dsvc,
}
svcs = append(svcs, svc)
d.services[svc.UUID] = &svc
}
return svcs, nil
case <-time.NewTimer(10 * time.Second).C:
@ -63,6 +67,9 @@ 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)
// wait on channel for characteristic discovery
select {
case <-s.device.charsChan:
@ -75,6 +82,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
characteristic: dchar,
}
chars = append(chars, char)
s.device.characteristics[char.UUID] = &char
}
return chars, nil
case <-time.NewTimer(10 * time.Second).C: