From 0424b70ef302e08a60492cf6b54ebc25d1254852 Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 23 Oct 2019 19:28:43 -0400 Subject: [PATCH] Add DiscoverServices() and interlocks to check central manager ready state. --- ble_darwin.go | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/ble_darwin.go b/ble_darwin.go index 1ba39ee..ec544e2 100644 --- a/ble_darwin.go +++ b/ble_darwin.go @@ -20,7 +20,7 @@ type BLE struct { peripherals Peripherals hr int - wantScan bool + ready, wantScan bool sync.Mutex } @@ -125,10 +125,9 @@ func (b *BLE) setState(x ns.CBManagerState) { b.Lock() defer b.Unlock() b.state = x - if b.state == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) && b.wantScan { + if b.ready && b.wantScan { go func() { fmt.Printf("Go: Scanning\n") - //cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid), nil) cm.ScanForPeripheralsWithServices(nil, nil) }() } @@ -147,19 +146,35 @@ func (b *BLE) Scan() { } func (b *BLE) StopScan() { + b.Lock() + if !b.ready { + b.Unlock() + return + } + b.Unlock() fmt.Printf("Go: stopping scan\n") cm.StopScan() } func (b *BLE) Connect(p Peripheral) { - go func() { - cm.ConnectPeripheral(p.p, nil) - }() + b.Lock() + if !b.ready { + b.Unlock() + return + } + b.Unlock() + cm.ConnectPeripheral(p.p, nil) + fmt.Printf("cm.ConnectPeripheral() returned\n") } func updateState(c *ns.CBCentralManager) { fmt.Printf("Go: did update state\n") st := cm.CBManager.State() + if st == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) { + ble.ready = true + } else { + ble.ready = false + } ble.setState(st) ble.events <- UpdateStateEvent{State: stringState(st)} } @@ -195,12 +210,10 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) { p.SetDelegate(cd) - // discover all services on this device - - p.DiscoverServices(nil) id := p.Identifier() peripheral := Peripheral{p: p, identifier: id} found := false + ble.peripherals.Lock() for _, item := range ble.peripherals.items { if item.p.identifier == id { @@ -210,13 +223,26 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) { } } ble.peripherals.Unlock() + if !found { peripheral.Name = peripheralName(p) + ble.peripherals.Add(peripheral) } + ble.events <- ConnectEvent{peripheral} - fmt.Printf("Go: discoverPeripheral returning\n") + fmt.Printf("Go: connectPeripheral returning\n") } +func DiscoverServices(x Peripheral) { + fmt.Printf("Discovering services on %s\n", x.Name) + p := x.p + + // discover all services on this device + + p.DiscoverServices(nil) +} + + func discoverServices(p *ns.CBPeripheral, e *ns.NSError) { fmt.Printf("Did discover services\n") p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool { @@ -278,7 +304,6 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) { ble.Unlock() fmt.Printf("Heart rate: %d\n", ble.hr) } - fmt.Printf("Go: updateValue returning\n") } var ( @@ -287,7 +312,6 @@ var ( info_uuid *ns.CBUUID cd *ns.CBDelegate cm *ns.CBCentralManager - //peripheral *ns.CBPeripheral ) func NewBLE() *BLE {