Add CancelConnection() and Disconnect().

This commit is contained in:
Greg 2019-10-25 14:03:20 -04:00
parent b87fea7fb3
commit fbb58a8d99

View File

@ -245,6 +245,22 @@ func (b *BLE) StopScan() {
b.cm.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 { func connectTracker(b *BLE, x ConnectionListItem) bool {
fmt.Printf("connectTracker(): %s\n", x.p.Name) fmt.Printf("connectTracker(): %s\n", x.p.Name)
b.connections.Lock() b.connections.Lock()
@ -252,7 +268,7 @@ func connectTracker(b *BLE, x ConnectionListItem) bool {
if item.p.Identifier == x.p.Identifier { if item.p.Identifier == x.p.Identifier {
fmt.Printf("connectTracker(): already connecting to %s\n", x.p.Name) fmt.Printf("connectTracker(): already connecting to %s\n", x.p.Name)
b.connections.Unlock() b.connections.Unlock()
return false return true
} }
} }
b.connections.items = append(b.connections.items, x) 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()) fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.p.Ptr())
b.cm.ConnectPeripheral(x.p.p, nil) 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() { go func() {
tick := time.NewTicker(time.Second * 5) tick := time.NewTicker(time.Second * 5)
select { select {
@ -268,16 +295,12 @@ func connectTracker(b *BLE, x ConnectionListItem) bool {
b.cm.CancelPeripheralConnection(x.p.p) b.cm.CancelPeripheralConnection(x.p.p)
case <-tick.C: case <-tick.C:
fmt.Printf("connectTracker(): Connection to %s timed out\n", x.p.Name) fmt.Printf("connectTracker(): Connection to %s timed out\n", x.p.Name)
b.cm.CancelPeripheralConnection(x.p.p) cancel()
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()
b.events <- ConnectTimeoutEvent{x.p} b.events <- ConnectTimeoutEvent{x.p}
case state := <-x.state: case state := <-x.state:
if state == "cancel" {
cancel()
}
fmt.Printf("connectTracker(): state %s\n", state) fmt.Printf("connectTracker(): state %s\n", state)
} }
}() }()
@ -312,6 +335,24 @@ func (b *BLE) Connect(p Peripheral) bool {
return connectTracker(b, item) 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) { func updateState(c *ns.CBCentralManager) {
b := cdLookup[c.Ptr()] b := cdLookup[c.Ptr()]
st := c.CBManager.State() st := c.CBManager.State()
@ -321,11 +362,13 @@ func updateState(c *ns.CBCentralManager) {
} else { } else {
if b.ready { if b.ready {
close(b.connections.close) close(b.connections.close)
b.connections.Lock()
for _, item := range b.connections.items { for _, item := range b.connections.items {
fmt.Printf("Closing connection to %s\n", item.p.Name) fmt.Printf("Closing connection to %s\n", item.p.Name)
b.cm.CancelPeripheralConnection(item.p.p) b.cm.CancelPeripheralConnection(item.p.p)
} }
b.connections.items = b.connections.items[:0] b.connections.items = b.connections.items[:0]
b.connections.Unlock()
b.ready = false b.ready = false
} }
} }