本文整理汇总了Golang中github.com/currantlabs/gatt.Peripheral.DiscoverDescriptors方法的典型用法代码示例。如果您正苦于以下问题:Golang Peripheral.DiscoverDescriptors方法的具体用法?Golang Peripheral.DiscoverDescriptors怎么用?Golang Peripheral.DiscoverDescriptors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/currantlabs/gatt.Peripheral
的用法示例。
在下文中一共展示了Peripheral.DiscoverDescriptors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ConnectHandler
func (b *ClientAdaptor) ConnectHandler(p gatt.Peripheral, err error) {
fmt.Printf("\nConnected Peripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
b.peripheral = p
if err := p.SetMTU(250); err != nil {
fmt.Printf("Failed to set MTU, err: %s\n", err)
}
ss, err := p.DiscoverServices(nil)
if err != nil {
fmt.Printf("Failed to discover services, err: %s\n", err)
return
}
outer:
for _, s := range ss {
b.services[s.UUID().String()] = NewService(s.UUID().String(), s)
cs, err := p.DiscoverCharacteristics(nil, s)
if err != nil {
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
continue
}
for _, c := range cs {
_, err := p.DiscoverDescriptors(nil, c)
if err != nil {
fmt.Printf("Failed to discover descriptors: %v\n", err)
continue outer
}
b.services[s.UUID().String()].characteristics[c.UUID().String()] = c
}
}
b.connected = true
close(b.ready)
}
示例2: onPeriphConnected
func onPeriphConnected(p gatt.Peripheral, err error) {
fmt.Println("Connected")
defer p.Device().CancelConnection(p)
if err := p.SetMTU(500); err != nil {
fmt.Printf("Failed to set MTU, err: %s\n", err)
}
// Discovery services
ss, err := p.DiscoverServices(nil)
if err != nil {
fmt.Printf("Failed to discover services, err: %s\n", err)
return
}
for _, s := range ss {
msg := "Service: " + s.UUID().String()
if len(s.Name()) > 0 {
msg += " (" + s.Name() + ")"
}
fmt.Println(msg)
// Discovery characteristics
cs, err := p.DiscoverCharacteristics(nil, s)
if err != nil {
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
continue
}
for _, c := range cs {
msg := " Characteristic " + c.UUID().String()
if len(c.Name()) > 0 {
msg += " (" + c.Name() + ")"
}
msg += "\n properties " + c.Properties().String()
fmt.Println(msg)
// Read the characteristic, if possible.
if (c.Properties() & gatt.CharRead) != 0 {
b, err := p.ReadCharacteristic(c)
if err != nil {
fmt.Printf("Failed to read characteristic, err: %s\n", err)
continue
}
fmt.Printf(" value %x | %q\n", b, b)
}
// Discovery descriptors
ds, err := p.DiscoverDescriptors(nil, c)
if err != nil {
fmt.Printf("Failed to discover descriptors, err: %s\n", err)
continue
}
for _, d := range ds {
msg := " Descriptor " + d.UUID().String()
if len(d.Name()) > 0 {
msg += " (" + d.Name() + ")"
}
fmt.Println(msg)
// Read descriptor (could fail, if it's not readable)
b, err := p.ReadDescriptor(d)
if err != nil {
fmt.Printf("Failed to read descriptor, err: %s\n", err)
continue
}
fmt.Printf(" value %x | %q\n", b, b)
}
// Subscribe the characteristic, if possible.
if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 {
f := func(c *gatt.Characteristic, b []byte, err error) {
fmt.Printf("notified: % X | %q\n", b, b)
}
if err := p.SetNotifyValue(c, f); err != nil {
fmt.Printf("Failed to subscribe characteristic, err: %s\n", err)
continue
}
}
}
fmt.Println()
}
fmt.Printf("Waiting for 5 seconds to get some notifiations, if any.\n")
time.Sleep(5 * time.Second)
}