From ada90768863797fcd0df421c979be15c01e1d2ef Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Sat, 29 Aug 2020 18:13:45 +0200 Subject: [PATCH] examples: add discover example that shows services/characteristics Signed-off-by: Ron Evans --- examples/discover/main.go | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 examples/discover/main.go diff --git a/examples/discover/main.go b/examples/discover/main.go new file mode 100644 index 0000000..dd388ea --- /dev/null +++ b/examples/discover/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "os" + + "github.com/tinygo-org/bluetooth" +) + +var adapter = bluetooth.DefaultAdapter + +func main() { + if len(os.Args) < 2 { + println("usage: discover [local name]") + os.Exit(1) + } + + // look for device with specific name + name := os.Args[1] + + // Enable BLE interface. + must("enable BLE stack", adapter.Enable()) + + ch := make(chan bluetooth.ScanResult, 1) + + // Start scanning. + println("scanning...") + err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) { + println("found device:", result.Address.String(), result.RSSI, result.LocalName()) + if result.LocalName() == name { + adapter.StopScan() + ch <- result + } + }) + + var device *bluetooth.Device + select { + case result := <-ch: + device, err = adapter.Connect(result.Address, bluetooth.ConnectionParams{}) + if err != nil { + println(err.Error()) + os.Exit(1) + } + + println("connected to ", result.LocalName()) + } + + // get services + println("discovering services/characteristics") + srvcs, err := device.DiscoverServices(nil) + for _, srvc := range srvcs { + println("- service", srvc.UUID.String()) + + chars, _ := srvc.DiscoverCharacteristics(nil) + for _, char := range chars { + println("-- characteristic", char.UUID.String()) + } + } + + must("start scan", err) +} + +func must(action string, err error) { + if err != nil { + panic("failed to " + action + ": " + err.Error()) + } +}