Add DiscoverServices() and interlocks to check central manager

ready state.
This commit is contained in:
Greg 2019-10-23 19:28:43 -04:00
parent 133b0883e3
commit 0424b70ef3
1 changed files with 36 additions and 12 deletions

View File

@ -20,7 +20,7 @@ type BLE struct {
peripherals Peripherals peripherals Peripherals
hr int hr int
wantScan bool ready, wantScan bool
sync.Mutex sync.Mutex
} }
@ -125,10 +125,9 @@ func (b *BLE) setState(x ns.CBManagerState) {
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
b.state = x b.state = x
if b.state == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) && b.wantScan { if b.ready && b.wantScan {
go func() { go func() {
fmt.Printf("Go: Scanning\n") fmt.Printf("Go: Scanning\n")
//cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid), nil)
cm.ScanForPeripheralsWithServices(nil, nil) cm.ScanForPeripheralsWithServices(nil, nil)
}() }()
} }
@ -147,19 +146,35 @@ func (b *BLE) Scan() {
} }
func (b *BLE) StopScan() { func (b *BLE) StopScan() {
b.Lock()
if !b.ready {
b.Unlock()
return
}
b.Unlock()
fmt.Printf("Go: stopping scan\n") fmt.Printf("Go: stopping scan\n")
cm.StopScan() cm.StopScan()
} }
func (b *BLE) Connect(p Peripheral) { func (b *BLE) Connect(p Peripheral) {
go func() { b.Lock()
cm.ConnectPeripheral(p.p, nil) if !b.ready {
}() b.Unlock()
return
}
b.Unlock()
cm.ConnectPeripheral(p.p, nil)
fmt.Printf("cm.ConnectPeripheral() returned\n")
} }
func updateState(c *ns.CBCentralManager) { func updateState(c *ns.CBCentralManager) {
fmt.Printf("Go: did update state\n") fmt.Printf("Go: did update state\n")
st := cm.CBManager.State() st := cm.CBManager.State()
if st == (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
ble.ready = true
} else {
ble.ready = false
}
ble.setState(st) ble.setState(st)
ble.events <- UpdateStateEvent{State: stringState(st)} ble.events <- UpdateStateEvent{State: stringState(st)}
} }
@ -195,12 +210,10 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
p.SetDelegate(cd) p.SetDelegate(cd)
// discover all services on this device
p.DiscoverServices(nil)
id := p.Identifier() id := p.Identifier()
peripheral := Peripheral{p: p, identifier: id} peripheral := Peripheral{p: p, identifier: id}
found := false found := false
ble.peripherals.Lock() ble.peripherals.Lock()
for _, item := range ble.peripherals.items { for _, item := range ble.peripherals.items {
if item.p.identifier == id { if item.p.identifier == id {
@ -210,13 +223,26 @@ func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
} }
} }
ble.peripherals.Unlock() ble.peripherals.Unlock()
if !found { if !found {
peripheral.Name = peripheralName(p) peripheral.Name = peripheralName(p)
ble.peripherals.Add(peripheral)
} }
ble.events <- ConnectEvent{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) { func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
fmt.Printf("Did discover services\n") fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool { 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() ble.Unlock()
fmt.Printf("Heart rate: %d\n", ble.hr) fmt.Printf("Heart rate: %d\n", ble.hr)
} }
fmt.Printf("Go: updateValue returning\n")
} }
var ( var (
@ -287,7 +312,6 @@ var (
info_uuid *ns.CBUUID info_uuid *ns.CBUUID
cd *ns.CBDelegate cd *ns.CBDelegate
cm *ns.CBCentralManager cm *ns.CBCentralManager
//peripheral *ns.CBPeripheral
) )
func NewBLE() *BLE { func NewBLE() *BLE {