Change Go types to plain structs instead of pointers to structs.

Add methods for protocols that Objective-C interfaces claim to
implement. Separate class methods from instance methods in
internal data structures. Add layout constraints with visual
formats to examples/app. Fix typedefs for wrapped objective-c
object types.
This commit is contained in:
gmp
2019-05-23 16:31:47 -04:00
parent 4f6930002d
commit a616761475
10 changed files with 355 additions and 220 deletions
+22 -23
View File
@@ -8,7 +8,7 @@ import (
"gitlab.wow.st/gmp/nswrap/examples/bluetooth/ns"
)
func updateState(c *ns.CBCentralManager) {
func updateState(c ns.CBCentralManager) {
fmt.Printf("Go: did update state\n")
switch cm.CBManager.State() {
case ns.CBManagerStateUnknown:
@@ -23,44 +23,44 @@ func updateState(c *ns.CBCentralManager) {
fmt.Printf(" powered off\n")
case ns.CBManagerStatePoweredOn:
fmt.Printf(" powered on\n")
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid),nil)
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid),ns.NSDictionary{})
}
}
func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
func discoverPeripheral(c ns.CBCentralManager, p ns.CBPeripheral, d ns.NSDictionary, rssi ns.NSNumber) {
fmt.Printf("Did discover peripheral\n")
c.StopScan()
if peripheral != nil {
if peripheral.Ptr() != nil {
peripheral.Release()
}
peripheral = p
peripheral.Retain()
c.ConnectPeripheral(peripheral,nil)
c.ConnectPeripheral(peripheral,ns.NSDictionary{})
}
func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
func connectPeripheral(c ns.CBCentralManager, p ns.CBPeripheral) {
fmt.Printf("Did connect peripheral\n")
p.SetDelegate(cd)
p.DiscoverServices(nil)
p.DiscoverServices(ns.NSArray{})
}
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
func discoverServices(p ns.CBPeripheral, e ns.NSError) {
fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
p.Services().ObjectEnumerator().ForIn(func(o ns.Id) bool {
serv := o.CBService()
switch {
case serv.UUID().IsEqualTo(hrm_uuid):
fmt.Printf("--heart rate monitor service\n")
p.DiscoverCharacteristics(nil,serv)
p.DiscoverCharacteristics(ns.NSArray{},serv)
case serv.UUID().IsEqualTo(ns.CBUUIDWithGoString("180A")):
fmt.Printf("--device information service\n")
p.DiscoverCharacteristics(nil,serv)
p.DiscoverCharacteristics(ns.NSArray{},serv)
}
return true
})
}
func hr(d *ns.NSData) int {
func hr(d ns.NSData) int {
if l := int(d.Length()); l < 4 {
return 0
}
@@ -73,11 +73,11 @@ func hr(d *ns.NSData) int {
}
}
func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
func discoverCharacteristics(p ns.CBPeripheral, s ns.CBService, e ns.NSError) {
fmt.Printf("Did discover characteristics\n")
fmt.Printf("----%s\n",s.UUID().UUIDString().UTF8String())
if s.UUID().IsEqualTo(hrm_uuid) {
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
s.Characteristics().ObjectEnumerator().ForIn(func(o ns.Id) bool {
chr := o.CBCharacteristic()
fmt.Printf("------%s\n",chr.UUID().UUIDString().UTF8String())
if chr.UUID().IsEqualTo(hrv_uuid) {
@@ -90,7 +90,7 @@ func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError)
}
}
func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
func updateValue(p ns.CBPeripheral, chr ns.CBCharacteristic, e ns.NSError) {
if chr.UUID().IsEqualTo(hrv_uuid) {
v := chr.Value()
fmt.Printf("Heart rate: %d\n",hr(v))
@@ -98,17 +98,17 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
}
var (
hrm_uuid *ns.CBUUID
hrv_uuid *ns.CBUUID
cd *ns.BleDelegate
cm *ns.CBCentralManager
peripheral *ns.CBPeripheral
hrm_uuid ns.CBUUID
hrv_uuid ns.CBUUID
cd ns.CBDelegate
cm ns.CBCentralManager
peripheral ns.CBPeripheral
)
func main() {
queue := ns.DispatchQueueCreate(ns.CharWithGoString("st.wow.gitlab.ble"),nil)
cd = ns.BleDelegateAlloc()
cd = ns.CBDelegateAlloc()
cd.CentralManagerDidUpdateStateCallback(updateState)
cd.CentralManagerDidDiscoverPeripheralCallback(discoverPeripheral)
@@ -120,8 +120,7 @@ func main() {
hrm_uuid = ns.CBUUIDWithGoString("180D")
hrv_uuid = ns.CBUUIDWithGoString("2A37")
cm = ns.CBCentralManagerAlloc()
cm.InitWithDelegate(cd,queue,nil)
cm = ns.CBCentralManagerAlloc().InitWithDelegate(cd,queue,ns.NSDictionary{})
select { }
}
+1 -1
View File
@@ -27,7 +27,7 @@ functions: [ NSMakeRange, dispatch_queue_create ]
enums: [ CB.* ]
frameworks: [ Foundation, CoreBluetooth ]
delegates:
BleDelegate:
CBDelegate:
CBCentralManagerDelegate:
- centralManagerDidUpdateState
- centralManagerDidDiscoverPeripheral