Debian Codegraph @

examples/explorer.go

raw symbols SCIP
1 // +build
2
3 package main
4
5 import (
6 "flag"
7 "fmt"
8 "log"
9 "os"
10 "strings"
11 "time"
12
13 "github.com/bettercap/gatt"
14 "github.com/bettercap/gatt/examples/option"
15 )
16
17 var done = make(chan struct{})
18
19 func onStateChanged(d gatt.Device, s gatt.State) {
20 fmt.Println("State:", s)
21 switch s {
22 case gatt.StatePoweredOn:
23 fmt.Println("Scanning...")
24 d.Scan([]gatt.UUID{}, false)
25 return
26 default:
27 d.StopScanning()
28 }
29 }
30
31 func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
32 id := strings.ToUpper(flag.Args()[0])
33 if strings.ToUpper(p.ID()) != id {
34 return
35 }
36
37 // Stop scanning once we've got the peripheral we're looking for.
38 p.Device().StopScanning()
39
40 fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
41 fmt.Println(" Local Name =", a.LocalName)
42 fmt.Println(" TX Power Level =", a.TxPowerLevel)
43 fmt.Println(" Manufacturer Data =", a.ManufacturerData)
44 fmt.Println(" Service Data =", a.ServiceData)
45 fmt.Println("")
46
47 p.Device().Connect(p)
48 }
49
50 func onPeriphConnected(p gatt.Peripheral, err error) {
51 fmt.Println("Connected")
52 defer p.Device().CancelConnection(p)
53
54 if err := p.SetMTU(500); err != nil {
55 fmt.Printf("Failed to set MTU, err: %s\n", err)
56 }
57
58 // Discovery services
59 ss, err := p.DiscoverServices(nil)
60 if err != nil {
61 fmt.Printf("Failed to discover services, err: %s\n", err)
62 return
63 }
64
65 for _, s := range ss {
66 msg := "Service: " + s.UUID().String()
67 if len(s.Name()) > 0 {
68 msg += " (" + s.Name() + ")"
69 }
70 fmt.Println(msg)
71
72 // Discovery characteristics
73 cs, err := p.DiscoverCharacteristics(nil, s)
74 if err != nil {
75 fmt.Printf("Failed to discover characteristics, err: %s\n", err)
76 continue
77 }
78
79 for _, c := range cs {
80 msg := " Characteristic " + c.UUID().String()
81 if len(c.Name()) > 0 {
82 msg += " (" + c.Name() + ")"
83 }
84 msg += "\n properties " + c.Properties().String()
85 fmt.Println(msg)
86
87 // Read the characteristic, if possible.
88 if (c.Properties() & gatt.CharRead) != 0 {
89 b, err := p.ReadCharacteristic(c)
90 if err != nil {
91 fmt.Printf("Failed to read characteristic, err: %s\n", err)
92 continue
93 }
94 fmt.Printf(" value %x | %q\n", b, b)
95 }
96
97 // Discovery descriptors
98 ds, err := p.DiscoverDescriptors(nil, c)
99 if err != nil {
100 fmt.Printf("Failed to discover descriptors, err: %s\n", err)
101 continue
102 }
103
104 for _, d := range ds {
105 msg := " Descriptor " + d.UUID().String()
106 if len(d.Name()) > 0 {
107 msg += " (" + d.Name() + ")"
108 }
109 fmt.Println(msg)
110
111 // Read descriptor (could fail, if it's not readable)
112 b, err := p.ReadDescriptor(d)
113 if err != nil {
114 fmt.Printf("Failed to read descriptor, err: %s\n", err)
115 continue
116 }
117 fmt.Printf(" value %x | %q\n", b, b)
118 }
119
120 // Subscribe the characteristic, if possible.
121 if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 {
122 f := func(c *gatt.Characteristic, b []byte, err error) {
123 fmt.Printf("notified: % X | %q\n", b, b)
124 }
125 if err := p.SetNotifyValue(c, f); err != nil {
126 fmt.Printf("Failed to subscribe characteristic, err: %s\n", err)
127 continue
128 }
129 }
130
131 }
132 fmt.Println()
133 }
134
135 fmt.Printf("Waiting for 5 seconds to get some notifiations, if any.\n")
136 time.Sleep(5 * time.Second)
137 }
138
139 func onPeriphDisconnected(p gatt.Peripheral, err error) {
140 fmt.Println("Disconnected")
141 close(done)
142 }
143
144 func main() {
145 flag.Parse()
146 if len(flag.Args()) != 1 {
147 log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0])
148 }
149
150 d, err := gatt.NewDevice(option.DefaultClientOptions...)
151 if err != nil {
152 log.Fatalf("Failed to open device, err: %s\n", err)
153 return
154 }
155
156 // Register handlers.
157 d.Handle(
158 gatt.PeripheralDiscovered(onPeriphDiscovered),
159 gatt.PeripheralConnected(onPeriphConnected),
160 gatt.PeripheralDisconnected(onPeriphDisconnected),
161 )
162
163 d.Init(onStateChanged)
164 <-done
165 fmt.Println("Done")
166 }