2015-06-08 03:38:19 +08:00
|
|
|
package ble
|
|
|
|
|
|
|
|
import (
|
2020-10-25 17:17:11 +08:00
|
|
|
"fmt"
|
2016-03-03 14:00:05 +08:00
|
|
|
"log"
|
2017-01-24 20:36:16 +08:00
|
|
|
"sync"
|
2020-10-26 05:21:20 +08:00
|
|
|
"time"
|
2016-07-14 01:16:58 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2017-02-02 22:56:26 +08:00
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
"tinygo.org/x/bluetooth"
|
2015-06-08 03:38:19 +08:00
|
|
|
)
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
var (
|
|
|
|
currentAdapter *bluetooth.Adapter
|
|
|
|
bleMutex sync.Mutex
|
|
|
|
)
|
2017-01-24 20:36:16 +08:00
|
|
|
|
2017-04-08 23:35:18 +08:00
|
|
|
// BLEConnector is the interface that a BLE ClientAdaptor must implement
|
2017-04-05 17:47:28 +08:00
|
|
|
type BLEConnector interface {
|
|
|
|
Connect() error
|
|
|
|
Reconnect() error
|
|
|
|
Disconnect() error
|
|
|
|
Finalize() error
|
|
|
|
Name() string
|
|
|
|
SetName(string)
|
|
|
|
Address() string
|
|
|
|
ReadCharacteristic(string) ([]byte, error)
|
|
|
|
WriteCharacteristic(string, []byte) error
|
|
|
|
Subscribe(string, func([]byte, error)) error
|
2018-02-18 15:47:28 +08:00
|
|
|
WithoutResponses(bool)
|
2017-01-14 19:57:37 +08:00
|
|
|
}
|
|
|
|
|
2016-12-29 00:53:41 +08:00
|
|
|
// ClientAdaptor represents a Client Connection to a BLE Peripheral
|
2016-09-26 02:14:05 +08:00
|
|
|
type ClientAdaptor struct {
|
2020-10-25 17:17:11 +08:00
|
|
|
name string
|
|
|
|
address string
|
|
|
|
AdapterName string
|
2016-12-29 00:53:41 +08:00
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
addr bluetooth.Address
|
|
|
|
adpt *bluetooth.Adapter
|
|
|
|
device *bluetooth.Device
|
|
|
|
characteristics map[string]bluetooth.DeviceCharacteristic
|
2016-12-29 00:53:41 +08:00
|
|
|
|
2018-02-18 15:47:28 +08:00
|
|
|
connected bool
|
|
|
|
withoutResponses bool
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
// NewClientAdaptor returns a new ClientAdaptor given an address
|
2016-12-29 00:53:41 +08:00
|
|
|
func NewClientAdaptor(address string) *ClientAdaptor {
|
2016-09-26 02:14:05 +08:00
|
|
|
return &ClientAdaptor{
|
2018-02-18 15:47:28 +08:00
|
|
|
name: gobot.DefaultName("BLEClient"),
|
|
|
|
address: address,
|
2020-10-25 17:17:11 +08:00
|
|
|
AdapterName: "default",
|
2018-02-18 15:47:28 +08:00
|
|
|
connected: false,
|
|
|
|
withoutResponses: false,
|
2020-10-25 17:17:11 +08:00
|
|
|
characteristics: make(map[string]bluetooth.DeviceCharacteristic),
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 00:45:48 +08:00
|
|
|
// Name returns the name for the adaptor
|
|
|
|
func (b *ClientAdaptor) Name() string { return b.name }
|
|
|
|
|
|
|
|
// SetName sets the name for the adaptor
|
2016-12-29 00:53:41 +08:00
|
|
|
func (b *ClientAdaptor) SetName(n string) { b.name = n }
|
|
|
|
|
2017-01-15 00:45:48 +08:00
|
|
|
// Address returns the Bluetooth LE address for the adaptor
|
|
|
|
func (b *ClientAdaptor) Address() string { return b.address }
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2018-02-18 15:47:28 +08:00
|
|
|
// WithoutResponses sets if the adaptor should expect responses after
|
2017-06-08 19:41:59 +08:00
|
|
|
// writing characteristics for this device
|
2018-02-18 15:47:28 +08:00
|
|
|
func (b *ClientAdaptor) WithoutResponses(use bool) { b.withoutResponses = use }
|
2017-06-08 19:41:59 +08:00
|
|
|
|
2015-06-08 03:38:19 +08:00
|
|
|
// Connect initiates a connection to the BLE peripheral. Returns true on successful connection.
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) Connect() error {
|
2017-01-24 20:36:16 +08:00
|
|
|
bleMutex.Lock()
|
|
|
|
defer bleMutex.Unlock()
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
var err error
|
2020-10-25 17:17:11 +08:00
|
|
|
// enable adaptor
|
|
|
|
b.adpt, err = getBLEAdapter(b.AdapterName)
|
2016-12-29 00:53:41 +08:00
|
|
|
if err != nil {
|
2023-06-15 00:27:22 +08:00
|
|
|
return fmt.Errorf("can't get adapter %s: %w", b.AdapterName, err)
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
// handle address
|
|
|
|
b.addr.Set(b.Address())
|
|
|
|
|
|
|
|
// scan for the address
|
2020-10-26 05:21:20 +08:00
|
|
|
ch := make(chan bluetooth.ScanResult, 1)
|
2020-10-25 17:17:11 +08:00
|
|
|
err = b.adpt.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) {
|
|
|
|
if result.Address.String() == b.Address() {
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := b.adpt.StopScan(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-25 19:09:53 +08:00
|
|
|
b.SetName(result.LocalName())
|
2020-10-25 17:17:11 +08:00
|
|
|
ch <- result
|
|
|
|
}
|
|
|
|
})
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2016-12-29 01:06:45 +08:00
|
|
|
if err != nil {
|
2020-10-25 17:17:11 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait to connect to peripheral device
|
2023-05-19 20:16:22 +08:00
|
|
|
result := <-ch
|
|
|
|
b.device, err = b.adpt.Connect(result.Address, bluetooth.ConnectionParams{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-12-29 00:53:41 +08:00
|
|
|
}
|
2016-12-29 01:29:46 +08:00
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
// get all services/characteristics
|
|
|
|
srvcs, err := b.device.DiscoverServices(nil)
|
2023-06-13 01:51:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-25 17:17:11 +08:00
|
|
|
for _, srvc := range srvcs {
|
|
|
|
chars, err := srvc.DiscoverCharacteristics(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, char := range chars {
|
|
|
|
b.characteristics[char.UUID().String()] = char
|
|
|
|
}
|
2016-12-29 00:53:41 +08:00
|
|
|
}
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2016-12-29 01:29:46 +08:00
|
|
|
b.connected = true
|
2023-06-13 01:51:25 +08:00
|
|
|
return nil
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reconnect attempts to reconnect to the BLE peripheral. If it has an active connection
|
|
|
|
// it will first close that connection and then establish a new connection.
|
|
|
|
// Returns true on Successful reconnection
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) Reconnect() error {
|
2015-06-08 03:38:19 +08:00
|
|
|
if b.connected {
|
2023-06-13 01:51:25 +08:00
|
|
|
if err := b.Disconnect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
return b.Connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect terminates the connection to the BLE peripheral. Returns true on successful disconnect.
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) Disconnect() error {
|
|
|
|
err := b.device.Disconnect()
|
2020-10-26 05:21:20 +08:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2023-06-13 01:51:25 +08:00
|
|
|
return err
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize finalizes the BLEAdaptor
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) Finalize() error {
|
2015-06-08 03:38:19 +08:00
|
|
|
return b.Disconnect()
|
|
|
|
}
|
|
|
|
|
2016-03-03 14:00:05 +08:00
|
|
|
// ReadCharacteristic returns bytes from the BLE device for the
|
2016-12-29 00:53:41 +08:00
|
|
|
// requested characteristic uuid
|
|
|
|
func (b *ClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
|
2015-06-30 00:47:18 +08:00
|
|
|
if !b.connected {
|
2023-06-13 01:51:25 +08:00
|
|
|
return nil, fmt.Errorf("Cannot read from BLE device until connected")
|
2015-06-30 00:47:18 +08:00
|
|
|
}
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2020-10-25 19:09:53 +08:00
|
|
|
cUUID = convertUUID(cUUID)
|
2015-06-30 06:25:59 +08:00
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
if char, ok := b.characteristics[cUUID]; ok {
|
|
|
|
buf := make([]byte, 255)
|
|
|
|
n, err := char.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Unknown characteristic: %s", cUUID)
|
2015-06-08 03:38:19 +08:00
|
|
|
}
|
|
|
|
|
2016-07-04 23:00:36 +08:00
|
|
|
// WriteCharacteristic writes bytes to the BLE device for the
|
|
|
|
// requested service and characteristic
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) WriteCharacteristic(cUUID string, data []byte) error {
|
2016-07-04 23:00:36 +08:00
|
|
|
if !b.connected {
|
2023-06-13 01:51:25 +08:00
|
|
|
return fmt.Errorf("Cannot write to BLE device until connected")
|
2016-07-04 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 19:09:53 +08:00
|
|
|
cUUID = convertUUID(cUUID)
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
if char, ok := b.characteristics[cUUID]; ok {
|
|
|
|
_, err := char.WriteWithoutResponse(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-07-04 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
return fmt.Errorf("Unknown characteristic: %s", cUUID)
|
2016-07-04 23:00:36 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 04:53:54 +08:00
|
|
|
// Subscribe subscribes to notifications from the BLE device for the
|
2016-07-05 01:46:42 +08:00
|
|
|
// requested service and characteristic
|
2023-06-13 01:51:25 +08:00
|
|
|
func (b *ClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) error {
|
2016-07-05 01:46:42 +08:00
|
|
|
if !b.connected {
|
2023-06-13 01:51:25 +08:00
|
|
|
return fmt.Errorf("Cannot subscribe to BLE device until connected")
|
2016-07-05 01:46:42 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 19:09:53 +08:00
|
|
|
cUUID = convertUUID(cUUID)
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
if char, ok := b.characteristics[cUUID]; ok {
|
|
|
|
fn := func(d []byte) {
|
|
|
|
f(d, nil)
|
2016-03-03 14:00:05 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
return char.EnableNotifications(fn)
|
2016-07-08 17:44:45 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
return fmt.Errorf("Unknown characteristic: %s", cUUID)
|
2016-03-03 14:00:05 +08:00
|
|
|
}
|
|
|
|
|
2023-06-15 00:27:22 +08:00
|
|
|
// getBLEAdapter is singleton for bluetooth adapter connection
|
2023-10-21 02:50:42 +08:00
|
|
|
func getBLEAdapter(impl string) (*bluetooth.Adapter, error) { //nolint:unparam // TODO: impl is unused, maybe an error
|
2020-10-25 17:17:11 +08:00
|
|
|
if currentAdapter != nil {
|
|
|
|
return currentAdapter, nil
|
2017-04-05 17:47:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
currentAdapter = bluetooth.DefaultAdapter
|
|
|
|
err := currentAdapter.Enable()
|
|
|
|
if err != nil {
|
2023-06-15 00:27:22 +08:00
|
|
|
return nil, err
|
2017-04-05 17:47:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:17:11 +08:00
|
|
|
return currentAdapter, nil
|
2016-03-03 14:00:05 +08:00
|
|
|
}
|