forked from go-ble/ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.go
57 lines (51 loc) · 1.42 KB
/
exp.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
package main
import (
"fmt"
"github.com/go-ble/ble"
)
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)
}
}
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
}