2015-06-07 12:38:19 -07:00
|
|
|
package ble
|
|
|
|
|
|
|
|
import (
|
2016-12-28 17:53:41 +01:00
|
|
|
"context"
|
2015-06-07 12:38:19 -07:00
|
|
|
"fmt"
|
2016-03-02 22:00:05 -08:00
|
|
|
"log"
|
|
|
|
"strings"
|
2016-12-28 18:29:46 +01:00
|
|
|
"time"
|
2016-07-13 10:16:58 -07:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
blelib "github.com/currantlabs/ble"
|
|
|
|
"github.com/currantlabs/ble/examples/lib/dev"
|
|
|
|
"github.com/pkg/errors"
|
2015-06-07 12:38:19 -07:00
|
|
|
)
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
// ClientAdaptor represents a Client Connection to a BLE Peripheral
|
2016-09-25 20:14:05 +02:00
|
|
|
type ClientAdaptor struct {
|
2016-12-28 17:53:41 +01:00
|
|
|
name string
|
|
|
|
address string
|
|
|
|
|
|
|
|
// uuid blelib.UUID
|
|
|
|
addr blelib.Addr
|
|
|
|
device blelib.Device
|
|
|
|
client blelib.Client
|
|
|
|
profile *blelib.Profile
|
|
|
|
|
|
|
|
connected bool
|
|
|
|
ready chan struct{}
|
2015-06-07 12:38:19 -07:00
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
// NewClientAdaptor returns a new ClientAdaptor given an address or peripheral name
|
|
|
|
func NewClientAdaptor(address string) *ClientAdaptor {
|
2016-09-25 20:14:05 +02:00
|
|
|
return &ClientAdaptor{
|
2016-10-04 10:25:14 +02:00
|
|
|
name: "BLECLient",
|
2016-12-28 17:53:41 +01:00
|
|
|
address: address,
|
2015-06-29 09:47:18 -07:00
|
|
|
connected: false,
|
2015-06-07 12:38:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
func (b *ClientAdaptor) Name() string { return b.name }
|
|
|
|
func (b *ClientAdaptor) SetName(n string) { b.name = n }
|
|
|
|
func (b *ClientAdaptor) Address() string { return b.address }
|
|
|
|
|
|
|
|
//func (b *ClientAdaptor) Peripheral() gatt.Peripheral { return b.peripheral }
|
2015-06-07 12:38:19 -07:00
|
|
|
|
|
|
|
// Connect initiates a connection to the BLE peripheral. Returns true on successful connection.
|
2016-11-07 17:38:14 +01:00
|
|
|
func (b *ClientAdaptor) Connect() (err error) {
|
2016-12-28 17:53:41 +01:00
|
|
|
d, err := dev.NewDevice("default")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't new device")
|
2015-06-07 12:38:19 -07:00
|
|
|
}
|
2016-12-28 17:53:41 +01:00
|
|
|
blelib.SetDefaultDevice(d)
|
|
|
|
b.device = d
|
2015-06-07 12:38:19 -07:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
var cln blelib.Client
|
2015-06-07 12:38:19 -07:00
|
|
|
|
2016-12-28 18:29:46 +01:00
|
|
|
ctx := blelib.WithSigHandler(context.WithTimeout(context.Background(), 3*time.Second))
|
|
|
|
cln, err = blelib.Connect(ctx, filter(b.Address()))
|
2016-12-28 18:06:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "can't connect")
|
2016-12-28 17:53:41 +01:00
|
|
|
}
|
2016-12-28 18:29:46 +01:00
|
|
|
|
2016-12-28 18:06:45 +01:00
|
|
|
b.addr = cln.Address()
|
|
|
|
b.address = cln.Address().String()
|
|
|
|
b.SetName(cln.Name())
|
2016-12-28 17:53:41 +01:00
|
|
|
b.client = cln
|
|
|
|
|
|
|
|
p, err := b.client.DiscoverProfile(true)
|
|
|
|
if err != nil {
|
2016-12-28 18:06:45 +01:00
|
|
|
return errors.Wrap(err, "can't discover profile")
|
2016-12-28 17:53:41 +01:00
|
|
|
}
|
2015-06-07 12:38:19 -07:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
b.profile = p
|
2016-12-28 18:29:46 +01:00
|
|
|
b.connected = true
|
2016-12-28 17:53:41 +01:00
|
|
|
return
|
2015-06-07 12:38:19 -07: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
|
2016-11-07 17:38:14 +01:00
|
|
|
func (b *ClientAdaptor) Reconnect() (err error) {
|
2015-06-07 12:38:19 -07:00
|
|
|
if b.connected {
|
|
|
|
b.Disconnect()
|
|
|
|
}
|
|
|
|
return b.Connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect terminates the connection to the BLE peripheral. Returns true on successful disconnect.
|
2016-11-07 17:38:14 +01:00
|
|
|
func (b *ClientAdaptor) Disconnect() (err error) {
|
2016-12-28 17:53:41 +01:00
|
|
|
b.client.CancelConnection()
|
2015-06-07 12:38:19 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize finalizes the BLEAdaptor
|
2016-11-07 17:38:14 +01:00
|
|
|
func (b *ClientAdaptor) Finalize() (err error) {
|
2015-06-07 12:38:19 -07:00
|
|
|
return b.Disconnect()
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:00:05 -08:00
|
|
|
// ReadCharacteristic returns bytes from the BLE device for the
|
2016-12-28 17:53:41 +01:00
|
|
|
// requested characteristic uuid
|
|
|
|
func (b *ClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error) {
|
2015-06-29 09:47:18 -07:00
|
|
|
if !b.connected {
|
|
|
|
log.Fatalf("Cannot read from BLE device until connected")
|
|
|
|
return
|
|
|
|
}
|
2015-06-07 12:38:19 -07:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
uuid, _ := blelib.Parse(cUUID)
|
2016-07-08 11:44:45 +02:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
|
|
|
|
data, err = b.client.ReadCharacteristic(u.(*blelib.Characteristic))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Printf(" Value %x | %q\n", data, data)
|
|
|
|
return data, nil
|
2015-06-29 15:25:59 -07:00
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
return data, nil
|
2015-06-07 12:38:19 -07:00
|
|
|
}
|
|
|
|
|
2016-07-04 17:00:36 +02:00
|
|
|
// WriteCharacteristic writes bytes to the BLE device for the
|
|
|
|
// requested service and characteristic
|
2016-12-28 17:53:41 +01:00
|
|
|
func (b *ClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err error) {
|
2016-07-04 17:00:36 +02:00
|
|
|
if !b.connected {
|
|
|
|
log.Fatalf("Cannot write to BLE device until connected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
uuid, _ := blelib.Parse(cUUID)
|
2016-07-04 19:46:42 +02:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
|
2016-12-28 18:29:46 +01:00
|
|
|
err = b.client.WriteCharacteristic(u.(*blelib.Characteristic), data, true)
|
2016-12-28 17:53:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-07-04 17:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-04 22:53:54 +02:00
|
|
|
// Subscribe subscribes to notifications from the BLE device for the
|
2016-07-04 19:46:42 +02:00
|
|
|
// requested service and characteristic
|
2016-12-28 17:53:41 +01:00
|
|
|
func (b *ClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err error) {
|
2016-07-04 19:46:42 +02:00
|
|
|
if !b.connected {
|
|
|
|
log.Fatalf("Cannot subscribe to BLE device until connected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
uuid, _ := blelib.Parse(cUUID)
|
2016-07-04 19:46:42 +02:00
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
|
|
|
|
h := func(req []byte) { f(req, nil) }
|
|
|
|
err = b.client.Subscribe(u.(*blelib.Characteristic), false, h)
|
2016-07-03 12:11:34 +02:00
|
|
|
if err != nil {
|
2016-12-28 17:53:41 +01:00
|
|
|
return err
|
2016-03-02 22:00:05 -08:00
|
|
|
}
|
2016-07-08 11:44:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
return
|
2016-03-02 22:00:05 -08:00
|
|
|
}
|
|
|
|
|
2016-12-28 17:53:41 +01:00
|
|
|
func filter(name string) blelib.AdvFilter {
|
|
|
|
return func(a blelib.Advertisement) bool {
|
|
|
|
return strings.ToLower(a.LocalName()) == strings.ToLower(name) ||
|
|
|
|
a.Address().String() == strings.ToLower(name)
|
2016-03-02 22:00:05 -08:00
|
|
|
}
|
|
|
|
}
|