forked from thomascriley/ble
/
main.go
182 lines (160 loc) · 5.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/thomascriley/ble"
"github.com/thomascriley/ble/examples/lib/dev"
)
var (
device = flag.String("device", "default", "implementation of ble")
name = flag.String("name", "Gopher", "name of remote peripheral")
addr = flag.String("addr", "", "address of remote peripheral (MAC on Linux, UUID on OS X)")
sub = flag.Duration("sub", 0, "subscribe to notification and indication for a specified period")
sd = flag.Duration("sd", 5*time.Second, "scanning duration, 0 for indefinitely")
)
func main() {
flag.Parse()
d, err := dev.NewDevice(*device)
if err != nil {
log.Fatalf("can't new device : %s", err)
}
ble.SetDefaultDevice(d)
// Default to search device with name of Gopher (or specified by user).
filter := func(a ble.Advertisement) bool {
return strings.ToUpper(a.LocalName()) == strings.ToUpper(*name)
}
// If addr is specified, search for addr instead.
if len(*addr) != 0 {
filter = func(a ble.Advertisement) bool {
return strings.ToUpper(a.Address().String()) == strings.ToUpper(*addr)
}
}
// Scan for specified durantion, or until interrupted by user.
fmt.Printf("Scanning for %s...\n", *sd)
ctx := ble.WithSigHandler(context.WithTimeout(context.Background(), *sd))
cln, err := ble.Connect(ctx, filter)
if err != nil {
log.Fatalf("can't connect : %s", err)
}
// Make sure we had the chance to print out the message.
done := make(chan struct{})
// Normally, the connection is disconnected by us after our exploration.
// However, it can be asynchronously disconnected by the remote peripheral.
// So we wait(detect) the disconnection in the go routine.
go func() {
<-cln.Disconnected()
fmt.Printf("[ %s ] is disconnected \n", cln.Address())
close(done)
}()
fmt.Printf("Discovering profile...\n")
p, err := cln.DiscoverProfile(true)
if err != nil {
log.Fatalf("can't discover profile: %s", err)
}
// Start the exploration.
explore(cln, p)
// Disconnect the connection. (On OS X, this might take a while.)
fmt.Printf("Disconnecting [ %s ]... (this might take up to few seconds on OS X)\n", cln.Address())
cln.CancelConnection()
<-done
}
func explore(cln ble.Client, p *ble.Profile) error {
for _, s := range p.Services {
fmt.Printf(" Service: %s %s, Handle (0x%02X)\n", s.UUID, ble.Name(s.UUID), s.Handle)
for _, c := range s.Characteristics {
fmt.Printf(" Characteristic: %s %s, Property: 0x%02X (%s), Handle(0x%02X), VHandle(0x%02X)\n",
c.UUID, ble.Name(c.UUID), c.Property, propString(c.Property), c.Handle, c.ValueHandle)
if (c.Property & ble.CharRead) != 0 {
b, err := cln.ReadCharacteristic(c)
if err != nil {
fmt.Printf("Failed to read characteristic: %s\n", err)
continue
}
fmt.Printf(" Value %x | %q\n", b, b)
}
for _, d := range c.Descriptors {
fmt.Printf(" Descriptor: %s %s, Handle(0x%02x)\n", d.UUID, ble.Name(d.UUID), d.Handle)
b, err := cln.ReadDescriptor(d)
if err != nil {
fmt.Printf("Failed to read descriptor: %s\n", err)
continue
}
fmt.Printf(" Value %x | %q\n", b, b)
}
if *sub != 0 {
// Don't bother to subscribe the Service Changed characteristics.
if c.UUID.Equal(ble.ServiceChangedUUID) {
continue
}
// Don't touch the Apple-specific Service/Characteristic.
// Service: D0611E78BBB44591A5F8487910AE4366
// Characteristic: 8667556C9A374C9184ED54EE27D90049, Property: 0x18 (WN),
// Descriptor: 2902, Client Characteristic Configuration
// Value 0000 | "\x00\x00"
if c.UUID.Equal(ble.MustParse("8667556C9A374C9184ED54EE27D90049")) {
continue
}
if (c.Property & ble.CharNotify) != 0 {
fmt.Printf("\n-- Subscribe to notification for %s --\n", *sub)
h := func(req []byte) { fmt.Printf("Notified: %q [ % X ]\n", string(req), req) }
if err := cln.Subscribe(c, false, h); err != nil {
log.Fatalf("subscribe failed: %s", err)
}
time.Sleep(*sub)
if err := cln.Unsubscribe(c, false); err != nil {
log.Fatalf("unsubscribe failed: %s", err)
}
fmt.Printf("-- Unsubscribe to notification --\n")
}
if (c.Property & ble.CharIndicate) != 0 {
fmt.Printf("\n-- Subscribe to indication of %s --\n", *sub)
h := func(req []byte) { fmt.Printf("Indicated: %q [ % X ]\n", string(req), req) }
if err := cln.Subscribe(c, true, h); err != nil {
log.Fatalf("subscribe failed: %s", err)
}
time.Sleep(*sub)
if err := cln.Unsubscribe(c, true); err != nil {
log.Fatalf("unsubscribe failed: %s", err)
}
fmt.Printf("-- Unsubscribe to indication --\n")
}
}
}
fmt.Printf("\n")
}
return nil
}
func propString(p ble.Property) string {
var s string
for k, v := range map[ble.Property]string{
ble.CharBroadcast: "B",
ble.CharRead: "R",
ble.CharWriteNR: "w",
ble.CharWrite: "W",
ble.CharNotify: "N",
ble.CharIndicate: "I",
ble.CharSignedWrite: "S",
ble.CharExtended: "E",
} {
if p&k != 0 {
s += v
}
}
return s
}
func chkErr(err error) {
switch errors.Cause(err) {
case nil:
case context.DeadlineExceeded:
fmt.Printf("done\n")
case context.Canceled:
fmt.Printf("canceled\n")
default:
log.Fatalf(err.Error())
}
}