From fbb58a8d99aed8e51f2fcd35fb0cd1821a1c0c25 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 25 Oct 2019 14:03:20 -0400 Subject: [PATCH] Add CancelConnection() and Disconnect(). --- ble_darwin.go | 61 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/ble_darwin.go b/ble_darwin.go index 1b85894..3196788 100644 --- a/ble_darwin.go +++ b/ble_darwin.go @@ -245,6 +245,22 @@ func (b *BLE) StopScan() { b.cm.StopScan() } +func CancelConnection(p Peripheral) { + b := pdLookup[p.p.Ptr()] + b.connections.Lock() + var ch chan string; + for _, item := range b.connections.items { + if item.p.Identifier == p.Identifier { + ch = item.state + break + } + } + b.connections.Unlock() + if ch != nil { + ch <- "cancel" + } +} + func connectTracker(b *BLE, x ConnectionListItem) bool { fmt.Printf("connectTracker(): %s\n", x.p.Name) b.connections.Lock() @@ -252,7 +268,7 @@ func connectTracker(b *BLE, x ConnectionListItem) bool { if item.p.Identifier == x.p.Identifier { fmt.Printf("connectTracker(): already connecting to %s\n", x.p.Name) b.connections.Unlock() - return false + return true } } b.connections.items = append(b.connections.items, x) @@ -260,6 +276,17 @@ func connectTracker(b *BLE, x ConnectionListItem) bool { fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.p.Ptr()) b.cm.ConnectPeripheral(x.p.p, nil) + cancel := func() { + b.cm.CancelPeripheralConnection(x.p.p) + b.connections.Lock() + for n, item := range b.connections.items { + if item.p.Identifier == x.p.Identifier { + b.connections.items = append(b.connections.items[:n], b.connections.items[n+1:]...) + } + } + b.connections.Unlock() + } + go func() { tick := time.NewTicker(time.Second * 5) select { @@ -268,16 +295,12 @@ func connectTracker(b *BLE, x ConnectionListItem) bool { b.cm.CancelPeripheralConnection(x.p.p) case <-tick.C: fmt.Printf("connectTracker(): Connection to %s timed out\n", x.p.Name) - b.cm.CancelPeripheralConnection(x.p.p) - b.connections.Lock() - for n, item := range b.connections.items { - if item.p.Identifier == x.p.Identifier { - b.connections.items = append(b.connections.items[:n], b.connections.items[n+1:]...) - } - } - b.connections.Unlock() + cancel() b.events <- ConnectTimeoutEvent{x.p} case state := <-x.state: + if state == "cancel" { + cancel() + } fmt.Printf("connectTracker(): state %s\n", state) } }() @@ -312,6 +335,24 @@ func (b *BLE) Connect(p Peripheral) bool { return connectTracker(b, item) } +func Disconnect(p Peripheral) { + b := pdLookup[p.p.Ptr()] + found := false + b.connections.Lock() + for i, item := range b.connections.items { + if item.p.Identifier == p.Identifier { + found = true + b.connections.items = append(b.connections.items[:i], b.connections.items[i+1:]...) + break + } + } + b.connections.Unlock() + if !found { + return + } + b.cm.CancelPeripheralConnection(p.p) +} + func updateState(c *ns.CBCentralManager) { b := cdLookup[c.Ptr()] st := c.CBManager.State() @@ -321,11 +362,13 @@ func updateState(c *ns.CBCentralManager) { } else { if b.ready { close(b.connections.close) + b.connections.Lock() for _, item := range b.connections.items { fmt.Printf("Closing connection to %s\n", item.p.Name) b.cm.CancelPeripheralConnection(item.p.p) } b.connections.items = b.connections.items[:0] + b.connections.Unlock() b.ready = false } }