2019-05-02 14:17:11 -04:00
|
|
|
package main
|
|
|
|
|
2019-05-09 22:33:06 -04:00
|
|
|
import "C"
|
|
|
|
|
2019-05-02 14:17:11 -04:00
|
|
|
import (
|
2019-05-10 02:00:56 -04:00
|
|
|
"encoding/binary"
|
2019-05-02 14:17:11 -04:00
|
|
|
"fmt"
|
2020-06-24 13:03:53 -04:00
|
|
|
"git.wow.st/gmp/nswrap/examples/bluetooth/ns"
|
2019-06-18 09:15:46 -04:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2019-05-09 22:33:06 -04:00
|
|
|
)
|
|
|
|
|
2020-06-25 16:56:55 -04:00
|
|
|
func updateState(self ns.CBDelegate, c *ns.CBCentralManager, b bool) {
|
2019-05-09 22:33:06 -04:00
|
|
|
fmt.Printf("Go: did update state\n")
|
2020-06-24 13:03:53 -04:00
|
|
|
switch ns.NSInteger(cm.CBManager.State()) {
|
2019-05-09 22:33:06 -04:00
|
|
|
case ns.CBManagerStateUnknown:
|
|
|
|
fmt.Printf(" unknown\n")
|
|
|
|
case ns.CBManagerStateResetting:
|
|
|
|
fmt.Printf(" resetting\n")
|
|
|
|
case ns.CBManagerStateUnsupported:
|
|
|
|
fmt.Printf(" unsupported\n")
|
|
|
|
case ns.CBManagerStateUnauthorized:
|
|
|
|
fmt.Printf(" unauthorized\n")
|
|
|
|
case ns.CBManagerStatePoweredOff:
|
|
|
|
fmt.Printf(" powered off\n")
|
|
|
|
case ns.CBManagerStatePoweredOn:
|
|
|
|
fmt.Printf(" powered on\n")
|
2019-06-13 16:47:36 -04:00
|
|
|
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid), nil)
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
fmt.Printf("Go: updateState returning\n")
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func discoverPeripheral(self ns.CBDelegate, c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("Did discover peripheral\n")
|
|
|
|
c.StopScan()
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// if we already have a pointer to a peripheral, check that this one is different,
|
|
|
|
// and if so release the old one. Be careful to check the Objective-C pointers
|
|
|
|
// here as the Go pointers will differ.
|
|
|
|
|
2019-06-19 14:07:33 -04:00
|
|
|
if peripheral != nil && p.Ptr() != peripheral.Ptr() {
|
|
|
|
peripheral.Release()
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
|
2019-05-10 02:00:56 -04:00
|
|
|
peripheral = p
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// we need to take ownership of this peripheral so CoreBluetooth doesn't
|
|
|
|
// dealloc it.
|
|
|
|
|
2019-05-10 02:00:56 -04:00
|
|
|
peripheral.Retain()
|
2019-06-13 16:47:36 -04:00
|
|
|
c.ConnectPeripheral(peripheral, nil)
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func connectPeripheral(self ns.CBDelegate, c *ns.CBCentralManager, p *ns.CBPeripheral) {
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("Did connect peripheral\n")
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// set ourselves up as a peripheral delegate
|
|
|
|
|
2019-05-10 02:00:56 -04:00
|
|
|
p.SetDelegate(cd)
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// discover all services on this device
|
|
|
|
|
2019-06-13 16:47:36 -04:00
|
|
|
p.DiscoverServices(nil)
|
2019-06-18 09:15:46 -04:00
|
|
|
fmt.Printf("Go: discoverPeripheral returning\n")
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func discoverServices(self ns.CBDelegate, p *ns.CBPeripheral, e *ns.NSError) {
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("Did discover services\n")
|
2019-06-13 16:47:36 -04:00
|
|
|
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-05-10 02:00:56 -04:00
|
|
|
serv := o.CBService()
|
2019-06-18 09:15:46 -04:00
|
|
|
uuid := serv.UUID()
|
2019-05-10 02:00:56 -04:00
|
|
|
switch {
|
2019-06-18 09:15:46 -04:00
|
|
|
case uuid.IsEqualTo(hrm_uuid):
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("--heart rate monitor service\n")
|
2019-06-13 16:47:36 -04:00
|
|
|
p.DiscoverCharacteristics(nil, serv)
|
2019-06-18 09:15:46 -04:00
|
|
|
case uuid.IsEqualTo(info_uuid):
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("--device information service\n")
|
2019-06-13 16:47:36 -04:00
|
|
|
p.DiscoverCharacteristics(nil, serv)
|
2019-06-11 16:41:21 -04:00
|
|
|
default:
|
|
|
|
fmt.Printf("--unknown service\n")
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-06-18 09:15:46 -04:00
|
|
|
fmt.Printf("Go: discoverServices returning\n")
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 16:47:36 -04:00
|
|
|
func hr(d *ns.NSData) int {
|
2019-05-10 02:00:56 -04:00
|
|
|
if l := int(d.Length()); l < 4 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
x := C.GoBytes(d.Bytes(), 4)
|
2019-05-10 02:00:56 -04:00
|
|
|
flags := x[0]
|
2019-06-11 12:38:22 -04:00
|
|
|
if flags&0x80 != 0 { // uint16 format
|
2019-05-10 02:35:52 -04:00
|
|
|
return int(binary.BigEndian.Uint16(x[1:2]))
|
2019-05-10 02:00:56 -04:00
|
|
|
} else {
|
2019-05-10 02:35:52 -04:00
|
|
|
return int(x[1])
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func discoverCharacteristics(self ns.CBDelegate, p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
|
2019-05-10 02:00:56 -04:00
|
|
|
fmt.Printf("Did discover characteristics\n")
|
2019-06-18 09:15:46 -04:00
|
|
|
uuid := s.UUID()
|
2019-06-27 11:41:58 -04:00
|
|
|
fmt.Printf("----%s\n", uuid.UUIDString())
|
2019-06-18 09:15:46 -04:00
|
|
|
if uuid.IsEqualTo(hrm_uuid) {
|
2019-06-13 16:47:36 -04:00
|
|
|
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-05-10 02:00:56 -04:00
|
|
|
chr := o.CBCharacteristic()
|
2019-06-18 09:15:46 -04:00
|
|
|
chuuid := chr.UUID()
|
2019-06-27 11:41:58 -04:00
|
|
|
fmt.Printf("------%s\n", chuuid.UUIDString())
|
2019-06-18 09:15:46 -04:00
|
|
|
if chuuid.IsEqualTo(hrv_uuid) {
|
2019-06-11 12:38:22 -04:00
|
|
|
p.SetNotifyValue(1, chr)
|
2019-05-10 02:00:56 -04:00
|
|
|
v := chr.Value()
|
|
|
|
fmt.Println(hr(v))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
fmt.Printf("Go: discoverCharacteristics returning\n")
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func updateValue(self ns.CBDelegate, p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
|
2019-05-10 02:00:56 -04:00
|
|
|
if chr.UUID().IsEqualTo(hrv_uuid) {
|
|
|
|
v := chr.Value()
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Heart rate: %d\n", hr(v))
|
2019-05-09 22:33:06 -04:00
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
fmt.Printf("Go: updateValue returning\n")
|
2019-05-09 22:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-06-13 16:47:36 -04:00
|
|
|
hrm_uuid *ns.CBUUID
|
|
|
|
hrv_uuid *ns.CBUUID
|
2019-06-18 09:15:46 -04:00
|
|
|
info_uuid *ns.CBUUID
|
2019-06-13 16:47:36 -04:00
|
|
|
cd *ns.CBDelegate
|
|
|
|
cm *ns.CBCentralManager
|
|
|
|
peripheral *ns.CBPeripheral
|
2019-05-02 14:17:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-06-11 12:38:22 -04:00
|
|
|
queue := ns.DispatchQueueCreate(ns.CharWithGoString("my_new_queue"), nil)
|
2019-05-09 22:33:06 -04:00
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
cd = ns.CBDelegateAlloc()
|
2019-05-11 23:03:56 -04:00
|
|
|
|
2019-05-09 22:33:06 -04:00
|
|
|
cd.CentralManagerDidUpdateStateCallback(updateState)
|
2019-05-10 02:00:56 -04:00
|
|
|
cd.CentralManagerDidDiscoverPeripheralCallback(discoverPeripheral)
|
|
|
|
cd.CentralManagerDidConnectPeripheralCallback(connectPeripheral)
|
|
|
|
cd.PeripheralDidDiscoverServicesCallback(discoverServices)
|
|
|
|
cd.PeripheralDidDiscoverCharacteristicsForServiceCallback(discoverCharacteristics)
|
|
|
|
cd.PeripheralDidUpdateValueForCharacteristicCallback(updateValue)
|
|
|
|
|
|
|
|
hrm_uuid = ns.CBUUIDWithGoString("180D")
|
|
|
|
hrv_uuid = ns.CBUUIDWithGoString("2A37")
|
2019-06-18 09:15:46 -04:00
|
|
|
info_uuid = ns.CBUUIDWithGoString("180A")
|
2019-05-09 22:33:06 -04:00
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
// We defined our own queue because this won't work on the main queue.
|
2019-06-11 12:38:22 -04:00
|
|
|
cm = ns.CBCentralManagerAlloc().InitWithDelegateQueue(cd, queue)
|
2019-05-03 13:14:30 -04:00
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
// For debugging purposes, run GC every second to make sure things are not
|
|
|
|
// over-released.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
runtime.GC()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}()
|
2019-06-11 12:38:22 -04:00
|
|
|
select {}
|
2019-05-02 14:17:11 -04:00
|
|
|
}
|