Compare commits
No commits in common. "5955e369e4eb378cd51b39e01225e2e03a0e0440" and "ec02bc7bcc2b1a53e0a69d255000d5d7eb76c460" have entirely different histories.
5955e369e4
...
ec02bc7bcc
122
ble_darwin.go
122
ble_darwin.go
|
@ -119,7 +119,6 @@ func (ps *Peripherals) Add(x Peripheral) bool {
|
||||||
}
|
}
|
||||||
//take ownership of this Objective-C object
|
//take ownership of this Objective-C object
|
||||||
x.p.Retain()
|
x.p.Retain()
|
||||||
runtime.SetFinalizer(x.p, nil)
|
|
||||||
x.p.GC()
|
x.p.GC()
|
||||||
item := PeripheralListItem{p: x, seen: time.Now()}
|
item := PeripheralListItem{p: x, seen: time.Now()}
|
||||||
ps.items = append(ps.items, item)
|
ps.items = append(ps.items, item)
|
||||||
|
@ -165,8 +164,6 @@ type DiscoverCharacteristicEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateValueEvent struct {
|
type UpdateValueEvent struct {
|
||||||
Peripheral Peripheral
|
|
||||||
Characteristic *ns.CBCharacteristic
|
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,9 +225,6 @@ func (b *BLE) Scan() {
|
||||||
if b.state != (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
|
if b.state != (ns.CBManagerState)(ns.CBManagerStatePoweredOn) {
|
||||||
b.wantScan = true
|
b.wantScan = true
|
||||||
} else {
|
} else {
|
||||||
b.peripherals.Lock()
|
|
||||||
b.peripherals.items = b.peripherals.items[:0]
|
|
||||||
b.peripherals.Unlock()
|
|
||||||
fmt.Printf("Go: Scanning\n")
|
fmt.Printf("Go: Scanning\n")
|
||||||
b.cm.ScanForPeripheralsWithServices(nil, nil)
|
b.cm.ScanForPeripheralsWithServices(nil, nil)
|
||||||
}
|
}
|
||||||
|
@ -247,41 +241,15 @@ func (b *BLE) StopScan() {
|
||||||
b.cm.StopScan()
|
b.cm.StopScan()
|
||||||
}
|
}
|
||||||
|
|
||||||
func CancelConnection(p Peripheral) {
|
func connectTracker(b *BLE, x ConnectionListItem) {
|
||||||
b := pdLookup[p.p.Ptr()]
|
|
||||||
if b == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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)
|
fmt.Printf("connectTracker(): %s\n", x.p.Name)
|
||||||
b.connections.Lock()
|
tick := time.NewTicker(time.Second * 5)
|
||||||
for _, item := range b.connections.items {
|
select {
|
||||||
if item.p.Identifier == x.p.Identifier {
|
case <-b.connections.close:
|
||||||
fmt.Printf("connectTracker(): already connecting to %s\n", x.p.Name)
|
fmt.Printf("Closing connection to %s\n", x.p.Name)
|
||||||
b.connections.Unlock()
|
b.cm.CancelPeripheralConnection(x.p.p)
|
||||||
return true
|
case <-tick.C:
|
||||||
}
|
fmt.Printf("Connection to %s timed out\n", x.p.Name)
|
||||||
}
|
|
||||||
b.connections.items = append(b.connections.items, x)
|
|
||||||
b.connections.Unlock()
|
|
||||||
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.cm.CancelPeripheralConnection(x.p.p)
|
||||||
b.connections.Lock()
|
b.connections.Lock()
|
||||||
for n, item := range b.connections.items {
|
for n, item := range b.connections.items {
|
||||||
|
@ -290,34 +258,20 @@ func connectTracker(b *BLE, x ConnectionListItem) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.connections.Unlock()
|
b.connections.Unlock()
|
||||||
}
|
b.events <- ConnectTimeoutEvent{x.p}
|
||||||
|
case state := <-x.state:
|
||||||
go func() {
|
fmt.Printf("connectTracker: state\n")
|
||||||
tick := time.NewTicker(time.Second * 5)
|
if state == "connected" {
|
||||||
select {
|
fmt.Printf("--connected")
|
||||||
case <-b.connections.close:
|
|
||||||
fmt.Printf("connectTracker(): Closing connection to %s\n", x.p.Name)
|
|
||||||
b.cm.CancelPeripheralConnection(x.p.p)
|
|
||||||
case <-tick.C:
|
|
||||||
fmt.Printf("connectTracker(): Connection to %s timed out\n", x.p.Name)
|
|
||||||
cancel()
|
|
||||||
b.events <- ConnectTimeoutEvent{x.p}
|
|
||||||
case state := <-x.state:
|
|
||||||
if state == "cancel" {
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
fmt.Printf("connectTracker(): state %s\n", state)
|
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BLE) Connect(p Peripheral) bool {
|
func (b *BLE) Connect(p Peripheral) {
|
||||||
b.Lock()
|
b.Lock()
|
||||||
if !b.ready {
|
if !b.ready {
|
||||||
b.Unlock()
|
b.Unlock()
|
||||||
fmt.Printf("--BLE not ready\n")
|
return
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
b.Unlock()
|
b.Unlock()
|
||||||
if p.p == nil {
|
if p.p == nil {
|
||||||
|
@ -325,37 +279,19 @@ func (b *BLE) Connect(p Peripheral) bool {
|
||||||
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
||||||
if x := (int)(ps.Count()); x > 0 {
|
if x := (int)(ps.Count()); x > 0 {
|
||||||
fmt.Printf("--found %d\n", x)
|
fmt.Printf("--found %d\n", x)
|
||||||
cbp := ps.ObjectAtIndex(0).CBPeripheral()
|
p.p = ps.ObjectAtIndex(0).CBPeripheral()
|
||||||
//NOTE: ns.ObjectAtIndex() calls SetFinalizer for us, which will be a problem
|
|
||||||
//later when we call GC(), so we first clear the finalizer here.
|
|
||||||
runtime.SetFinalizer(cbp, nil)
|
|
||||||
p = newPeripheral(cbp)
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("--none found\n")
|
fmt.Printf("--none found\n")
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
item := ConnectionListItem{p, make(chan string)}
|
item := ConnectionListItem{p, make(chan string)}
|
||||||
fmt.Printf("BLE.Connect() calling connectTracker\n")
|
b.connections.Add(item)
|
||||||
return connectTracker(b, item)
|
go connectTracker(b, item)
|
||||||
}
|
//time.Sleep(time.Second/10)
|
||||||
|
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", p.p.Ptr())
|
||||||
func Disconnect(p Peripheral) {
|
b.cm.ConnectPeripheral(p.p, nil)
|
||||||
b := pdLookup[p.p.Ptr()]
|
fmt.Printf("BLE.Connect() returned\n")
|
||||||
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) {
|
||||||
|
@ -367,13 +303,11 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -385,15 +319,11 @@ func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDict
|
||||||
b := cdLookup[c.Ptr()]
|
b := cdLookup[c.Ptr()]
|
||||||
peripheral := newPeripheral(p)
|
peripheral := newPeripheral(p)
|
||||||
peripheral.RSSI = (int)(rssi.IntValue())
|
peripheral.RSSI = (int)(rssi.IntValue())
|
||||||
_discoverPeripheral(b, peripheral)
|
|
||||||
}
|
|
||||||
|
|
||||||
func _discoverPeripheral(b *BLE, peripheral Peripheral) {
|
|
||||||
if peripheral.Name == "" {
|
if peripheral.Name == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if ok := b.peripherals.Add(peripheral); ok {
|
if ok := b.peripherals.Add(peripheral); ok {
|
||||||
pdLookup[peripheral.p.Ptr()] = b
|
pdLookup[p.Ptr()] = b
|
||||||
b.events <- DiscoverPeripheralEvent{Peripheral: peripheral}
|
b.events <- DiscoverPeripheralEvent{Peripheral: peripheral}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -499,8 +429,6 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
|
||||||
b := pdLookup[p.Ptr()]
|
b := pdLookup[p.Ptr()]
|
||||||
v := chr.Value()
|
v := chr.Value()
|
||||||
b.events <-UpdateValueEvent{
|
b.events <-UpdateValueEvent{
|
||||||
Peripheral: newPeripheral(p),
|
|
||||||
Characteristic: chr,
|
|
||||||
Data: C.GoBytes(v.Bytes(), (C.int)(v.Length())),
|
Data: C.GoBytes(v.Bytes(), (C.int)(v.Length())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user