Move BLE type into ble.go.
This commit is contained in:
parent
2d4f106dd7
commit
c911bbb547
12
ble.go
12
ble.go
|
@ -8,6 +8,18 @@ import (
|
|||
"git.wow.st/gmp/ble/gatt"
|
||||
)
|
||||
|
||||
type BLE struct {
|
||||
state bleState
|
||||
events chan interface{}
|
||||
peripherals Peripherals
|
||||
handle bleHandle
|
||||
|
||||
ready, wantScan bool
|
||||
sync.Mutex
|
||||
|
||||
connections Connections
|
||||
}
|
||||
|
||||
type PeripheralListItem struct {
|
||||
p Peripheral
|
||||
seen time.Time
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"encoding/binary"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
|
@ -14,17 +13,11 @@ import (
|
|||
"git.wow.st/gmp/ble/ns"
|
||||
)
|
||||
|
||||
type BLE struct {
|
||||
state ns.CBManagerState
|
||||
events chan interface{}
|
||||
peripherals Peripherals
|
||||
type bleState ns.CBManagerState
|
||||
|
||||
type bleHandle struct {
|
||||
cd *ns.CBDelegate
|
||||
cm *ns.CBCentralManager
|
||||
|
||||
ready, wantScan bool
|
||||
sync.Mutex
|
||||
|
||||
connections Connections
|
||||
}
|
||||
|
||||
var cdLookup map[unsafe.Pointer]*BLE
|
||||
|
@ -91,18 +84,18 @@ func newPeripheral(x *ns.CBPeripheral) Peripheral {
|
|||
|
||||
func (b *BLE) stringState() string {
|
||||
x := b.state
|
||||
switch x {
|
||||
case (ns.CBManagerState)(ns.CBManagerStateResetting):
|
||||
switch (ns.NSInteger)(x) {
|
||||
case ns.CBManagerStateResetting:
|
||||
return "resetting"
|
||||
case (ns.CBManagerState)(ns.CBManagerStateUnsupported):
|
||||
case ns.CBManagerStateUnsupported:
|
||||
return "unsupported"
|
||||
case (ns.CBManagerState)(ns.CBManagerStateUnauthorized):
|
||||
case ns.CBManagerStateUnauthorized:
|
||||
return "unauthorized"
|
||||
case (ns.CBManagerState)(ns.CBManagerStatePoweredOff):
|
||||
case ns.CBManagerStatePoweredOff:
|
||||
return "powered off"
|
||||
case (ns.CBManagerState)(ns.CBManagerStatePoweredOn):
|
||||
case ns.CBManagerStatePoweredOn:
|
||||
return "powered on"
|
||||
case (ns.CBManagerState)(ns.CBManagerStateUnknown):
|
||||
case ns.CBManagerStateUnknown:
|
||||
return "unknown"
|
||||
default:
|
||||
return "no state"
|
||||
|
@ -110,21 +103,21 @@ func (b *BLE) stringState() string {
|
|||
}
|
||||
|
||||
func (b *BLE) readyToScan() bool {
|
||||
return b.state == (ns.CBManagerState)(ns.CBManagerStatePoweredOn)
|
||||
return b.state == (bleState)(ns.CBManagerStatePoweredOn)
|
||||
}
|
||||
|
||||
func (b *BLE) scan() {
|
||||
b.cm.ScanForPeripheralsWithServices(nil, nil)
|
||||
b.handle.cm.ScanForPeripheralsWithServices(nil, nil)
|
||||
}
|
||||
|
||||
func (b *BLE) stopScan() {
|
||||
b.cm.StopScan()
|
||||
b.handle.cm.StopScan()
|
||||
}
|
||||
|
||||
func (b *BLE) setState(x ns.CBManagerState) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
b.state = x
|
||||
b.state = (bleState)(x)
|
||||
if b.ready && b.wantScan {
|
||||
fmt.Printf("Go: Scanning\n")
|
||||
b.scan()
|
||||
|
@ -134,7 +127,7 @@ func (b *BLE) setState(x ns.CBManagerState) {
|
|||
|
||||
func (b *BLE) connectPeripheral(x Peripheral) {
|
||||
fmt.Printf("BLE.Connect(): calling cm.ConnectPeripheral(%p)\n", x.p.Ptr())
|
||||
b.cm.ConnectPeripheral(x.p, nil)
|
||||
b.handle.cm.ConnectPeripheral(x.p, nil)
|
||||
}
|
||||
|
||||
func CancelConnection(p Peripheral) {
|
||||
|
@ -157,12 +150,12 @@ func CancelConnection(p Peripheral) {
|
|||
}
|
||||
|
||||
func (b *BLE) cancelConnection(p Peripheral) {
|
||||
b.cm.CancelPeripheralConnection(p.p)
|
||||
b.handle.cm.CancelPeripheralConnection(p.p)
|
||||
}
|
||||
|
||||
func (b *BLE) knownPeripheral(p Peripheral) (Peripheral, bool) {
|
||||
fmt.Printf("RetrievePeripheralsWithIdentifiers\n")
|
||||
ps := b.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
||||
ps := b.handle.cm.RetrievePeripheralsWithIdentifiers(ns.NSArrayWithObjects(ns.NSUUIDAlloc().InitWithUUIDString(ns.NSStringWithGoString(p.Identifier))))
|
||||
if x := (int)(ps.Count()); x > 0 {
|
||||
fmt.Printf("--found %d\n", x)
|
||||
cbp := ps.ObjectAtIndex(0).CBPeripheral()
|
||||
|
@ -185,7 +178,7 @@ func didUpdateState(c *ns.CBCentralManager) {
|
|||
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.handle.cm.CancelPeripheralConnection(item.p.p)
|
||||
}
|
||||
b.connections.items = b.connections.items[:0]
|
||||
b.connections.Unlock()
|
||||
|
@ -218,7 +211,7 @@ func didConnectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
|||
b := cdLookup[c.Ptr()]
|
||||
|
||||
// set ourselves up as a peripheral delegate
|
||||
p.SetDelegate(b.cd)
|
||||
p.SetDelegate(b.handle.cd)
|
||||
|
||||
uuidstring := p.Identifier().UUIDString().String()
|
||||
peripheral := newPeripheral(p)
|
||||
|
@ -336,7 +329,7 @@ func NewBLE() *BLE {
|
|||
cd.PeripheralDidUpdateValueForCharacteristicCallback(didUpdateValue)
|
||||
|
||||
|
||||
ble.cd = cd
|
||||
ble.handle.cd = cd
|
||||
if cdLookup == nil {
|
||||
cdLookup = make(map[unsafe.Pointer]*BLE, 0)
|
||||
}
|
||||
|
@ -345,8 +338,8 @@ func NewBLE() *BLE {
|
|||
}
|
||||
|
||||
// We defined our own queue because this won't work on the main queue.
|
||||
ble.cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
|
||||
cdLookup[ble.cm.Ptr()] = ble
|
||||
ble.handle.cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
|
||||
cdLookup[ble.handle.cm.Ptr()] = ble
|
||||
|
||||
// For debugging purposes, run GC every second to make sure things are
|
||||
// not over-released.
|
||||
|
|
Loading…
Reference in New Issue
Block a user