2020-08-27 23:22:27 +03:00
|
|
|
package bluetooth
|
|
|
|
|
2020-08-28 09:35:13 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-28 14:31:17 +03:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-08-28 09:35:13 +03:00
|
|
|
|
|
|
|
"github.com/JuulLabs-OSS/cbgo"
|
|
|
|
)
|
|
|
|
|
2020-08-29 15:43:11 +03:00
|
|
|
// Address contains a Bluetooth address which on macOS is a UUID.
|
2020-08-28 13:40:03 +03:00
|
|
|
type Address struct {
|
2020-08-28 14:31:17 +03:00
|
|
|
// UUID since this is macOS.
|
2020-08-28 13:40:03 +03:00
|
|
|
UUID
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
// IsRandom ignored on macOS.
|
2020-08-28 13:40:03 +03:00
|
|
|
func (ad Address) IsRandom() bool {
|
2020-08-28 14:31:17 +03:00
|
|
|
return false
|
2020-08-28 13:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
// SetRandom ignored on macOS.
|
2020-08-28 13:40:03 +03:00
|
|
|
func (ad Address) SetRandom(val bool) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the address
|
|
|
|
func (ad Address) Set(val interface{}) {
|
|
|
|
ad.UUID = val.(UUID)
|
|
|
|
}
|
|
|
|
|
2020-08-27 23:22:27 +03:00
|
|
|
// Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern
|
|
|
|
// is to cancel the scan when a particular device has been found.
|
|
|
|
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
|
2020-08-28 09:35:13 +03:00
|
|
|
if callback == nil {
|
|
|
|
return errors.New("must provide callback to Scan function")
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
if a.scanChan != nil {
|
2020-08-28 09:35:13 +03:00
|
|
|
return errors.New("already calling Scan function")
|
|
|
|
}
|
|
|
|
|
|
|
|
a.peripheralFoundHandler = callback
|
|
|
|
|
|
|
|
// Channel that will be closed when the scan is stopped.
|
|
|
|
// Detecting whether the scan is stopped can be done by doing a non-blocking
|
|
|
|
// read from it. If it succeeds, the scan is stopped.
|
2020-08-28 14:31:17 +03:00
|
|
|
a.scanChan = make(chan error)
|
2020-08-28 09:35:13 +03:00
|
|
|
|
|
|
|
a.cm.Scan(nil, &cbgo.CentralManagerScanOpts{
|
2020-08-28 14:31:17 +03:00
|
|
|
AllowDuplicates: false,
|
2020-08-28 09:35:13 +03:00
|
|
|
})
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
// Check whether the scan is stopped. This is necessary to avoid a race
|
|
|
|
// condition between the signal channel and the cancelScan channel when
|
|
|
|
// the callback calls StopScan() (no new callbacks may be called after
|
|
|
|
// StopScan is called).
|
|
|
|
select {
|
|
|
|
case <-a.scanChan:
|
|
|
|
close(a.scanChan)
|
|
|
|
a.scanChan = nil
|
|
|
|
return nil
|
2020-08-28 09:35:13 +03:00
|
|
|
}
|
2020-08-27 23:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopScan stops any in-progress scan. It can be called from within a Scan
|
|
|
|
// callback to stop the current scan. If no scan is in progress, an error will
|
|
|
|
// be returned.
|
|
|
|
func (a *Adapter) StopScan() error {
|
2020-08-28 14:31:17 +03:00
|
|
|
if a.scanChan == nil {
|
|
|
|
return errors.New("not calling Scan function")
|
2020-08-28 09:35:13 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
a.scanChan <- nil
|
2020-08-28 09:35:13 +03:00
|
|
|
a.cm.StopScan()
|
|
|
|
|
2020-08-27 23:22:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-28 14:31:17 +03:00
|
|
|
|
|
|
|
// Device is a connection to a remote peripheral.
|
|
|
|
type Device struct {
|
2020-08-29 16:01:34 +03:00
|
|
|
delegate *peripheralDelegate
|
2020-08-28 14:31:17 +03:00
|
|
|
|
2020-08-29 12:35:26 +03:00
|
|
|
cm cbgo.CentralManager
|
|
|
|
prph cbgo.Peripheral
|
|
|
|
|
|
|
|
servicesChan chan error
|
|
|
|
charsChan chan error
|
2020-08-29 17:15:24 +03:00
|
|
|
|
|
|
|
services map[UUID]*DeviceService
|
|
|
|
characteristics map[UUID]*DeviceCharacteristic
|
2020-08-28 14:31:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect starts a connection attempt to the given peripheral device address.
|
|
|
|
func (a *Adapter) Connect(address Addresser, params ConnectionParams) (*Device, error) {
|
|
|
|
adr := address.(Address)
|
|
|
|
uuid, err := cbgo.ParseUUID(adr.UUID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prphs := a.cm.RetrievePeripheralsWithIdentifiers([]cbgo.UUID{uuid})
|
|
|
|
if len(prphs) == 0 {
|
|
|
|
return nil, fmt.Errorf("Connect failed: no peer with address: %s", adr.UUID.String())
|
|
|
|
}
|
|
|
|
a.cm.Connect(prphs[0], nil)
|
|
|
|
|
|
|
|
// wait on channel for connect
|
|
|
|
select {
|
|
|
|
case p := <-a.connectChan:
|
|
|
|
d := &Device{
|
2020-08-29 12:35:26 +03:00
|
|
|
cm: a.cm,
|
|
|
|
prph: p,
|
|
|
|
servicesChan: make(chan error),
|
|
|
|
charsChan: make(chan error),
|
2020-08-28 14:31:17 +03:00
|
|
|
}
|
|
|
|
|
2020-08-29 16:01:34 +03:00
|
|
|
d.delegate = &peripheralDelegate{d: d}
|
|
|
|
p.SetDelegate(d.delegate)
|
|
|
|
|
2020-09-10 18:17:45 +03:00
|
|
|
a.connectHandler(nil, true)
|
|
|
|
|
2020-08-28 14:31:17 +03:00
|
|
|
return d, nil
|
|
|
|
case <-time.NewTimer(10 * time.Second).C:
|
|
|
|
return nil, errors.New("timeout on Connect")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 16:01:34 +03:00
|
|
|
// Peripheral delegate functions
|
2020-08-29 12:35:26 +03:00
|
|
|
|
2020-08-29 16:01:34 +03:00
|
|
|
type peripheralDelegate struct {
|
|
|
|
cbgo.PeripheralDelegateBase
|
2020-08-29 12:35:26 +03:00
|
|
|
|
2020-08-29 16:01:34 +03:00
|
|
|
d *Device
|
|
|
|
}
|
2020-08-28 14:31:17 +03:00
|
|
|
|
2020-08-29 12:35:26 +03:00
|
|
|
// DidDiscoverServices is called when the services for a Peripheral
|
|
|
|
// have been discovered.
|
2020-08-29 16:01:34 +03:00
|
|
|
func (pd *peripheralDelegate) DidDiscoverServices(prph cbgo.Peripheral, err error) {
|
|
|
|
pd.d.servicesChan <- nil
|
2020-08-28 14:31:17 +03:00
|
|
|
}
|
2020-08-29 12:35:26 +03:00
|
|
|
|
|
|
|
// DidDiscoverCharacteristics is called when the characteristics for a Service
|
|
|
|
// for a Peripheral have been discovered.
|
2020-08-29 16:01:34 +03:00
|
|
|
func (pd *peripheralDelegate) DidDiscoverCharacteristics(prph cbgo.Peripheral, svc cbgo.Service, err error) {
|
|
|
|
pd.d.charsChan <- nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-08-29 17:15:24 +03:00
|
|
|
uuid, _ := ParseUUID(chr.UUID().String())
|
|
|
|
if char, ok := pd.d.characteristics[uuid]; ok {
|
|
|
|
if char != nil && char.callback != nil {
|
|
|
|
go char.callback(chr.Value())
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 14:31:17 +03:00
|
|
|
}
|