up
This commit is contained in:
+16
-23
@@ -10,21 +10,22 @@ import (
|
||||
"github.com/currantlabs/gatt/examples/option"
|
||||
)
|
||||
|
||||
var done = make(chan struct{})
|
||||
|
||||
type BleService struct {
|
||||
workers []workers.Worker
|
||||
workers []*workers.Worker
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// AddWorker - add new worker
|
||||
func (s BleService) AddWorker(worker workers.Worker) {
|
||||
func (s *BleService) AddWorker(worker workers.Worker) {
|
||||
if s.workers == nil {
|
||||
s.workers = []workers.Worker{}
|
||||
fmt.Println("Init workers.")
|
||||
s.workers = make([]*workers.Worker, 0, 0)
|
||||
}
|
||||
s.workers = append(s.workers, worker)
|
||||
s.workers = append(s.workers, &worker)
|
||||
}
|
||||
|
||||
func (s BleService) Init() error {
|
||||
func (s *BleService) Init() error {
|
||||
s.done = make(chan struct{})
|
||||
d, err := gatt.NewDevice(option.DefaultClientOptions...)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open device, err: %s\n", err)
|
||||
@@ -39,13 +40,13 @@ func (s BleService) Init() error {
|
||||
)
|
||||
|
||||
d.Init(s.onStateChanged)
|
||||
<-done
|
||||
<-s.done
|
||||
fmt.Println("Done")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s BleService) onStateChanged(d gatt.Device, state gatt.State) {
|
||||
func (s *BleService) onStateChanged(d gatt.Device, state gatt.State) {
|
||||
fmt.Println("State:", state)
|
||||
switch state {
|
||||
case gatt.StatePoweredOn:
|
||||
@@ -57,17 +58,10 @@ func (s BleService) onStateChanged(d gatt.Device, state gatt.State) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s BleService) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
|
||||
fmt.Println(" Local Name =", a.LocalName)
|
||||
fmt.Println(" TX Power Level =", a.TxPowerLevel)
|
||||
fmt.Println(" Manufacturer Data =", a.ManufacturerData)
|
||||
fmt.Println(" Service Data =", a.ServiceData)
|
||||
fmt.Println(" RSSI =", rssi)
|
||||
|
||||
func (s *BleService) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||
isConnect := false
|
||||
for _, worker := range s.workers {
|
||||
ret := worker.OnDiscovery(p, a, rssi)
|
||||
ret := (*worker).OnDiscovery(p, a, rssi)
|
||||
if ret && !isConnect {
|
||||
isConnect = true
|
||||
}
|
||||
@@ -78,8 +72,7 @@ func (s BleService) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement,
|
||||
}
|
||||
}
|
||||
|
||||
func (s BleService) onPeriphConnected(p gatt.Peripheral, err error) {
|
||||
fmt.Println("Connected")
|
||||
func (s *BleService) onPeriphConnected(p gatt.Peripheral, err error) {
|
||||
defer p.Device().CancelConnection(p)
|
||||
|
||||
if err := p.SetMTU(500); err != nil {
|
||||
@@ -88,11 +81,11 @@ func (s BleService) onPeriphConnected(p gatt.Peripheral, err error) {
|
||||
fmt.Printf("Set mtu done.\n")
|
||||
|
||||
for _, worker := range s.workers {
|
||||
worker.OnConnect(p, err)
|
||||
(*worker).OnConnect(p, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s BleService) onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||
func (s *BleService) onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||
fmt.Println("Disconnected")
|
||||
close(done)
|
||||
close(s.done)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user