| 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 |
| } |