ef90e5d337
* gattc: use UUID() to allow for bare metal to permit clean use of short UUIDs Signed-off-by: deadprogram <ron@hybridgroup.com> * gattc/macos: correct usage of UUID wrapper type alias Signed-off-by: Ron Evans <ron@hybridgroup.com> * gattc/sd: correct usage of UUID wrapper type alias Signed-off-by: Ron Evans <ron@hybridgroup.com> * gattc/sd, uuid/sd: changes intended to reduce memory allocations for service and characteristic discovery Signed-off-by: deadprogram <ron@hybridgroup.com> * gattc/sd: partial improvements to DiscoverServices/DiscoverCharacteristics Signed-off-by: deadprogram <ron@hybridgroup.com> * gattc/sd: mostly getting uuid back for services in DiscoverServices Signed-off-by: deadprogram <ron@hybridgroup.com> * uuid/sd: correct way to calculate UUID from shortUUID Signed-off-by: deadprogram <ron@hybridgroup.com> * gattc/sd: able to discover services and characteristics Signed-off-by: deadprogram <ron@hybridgroup.com> * examples: updated discover example that can run with OS or bare metal Signed-off-by: deadprogram <ron@hybridgroup.com> * gattc/sd: ensure safe casts for length of returned struct when converting short UUID Signed-off-by: deadprogram <ron@hybridgroup.com>
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
// This example scans and then connects to a specific Bluetooth peripheral
|
|
// and then displays all of the services and characteristics.
|
|
//
|
|
// To run this on a desktop system:
|
|
//
|
|
// go run ./examples/discover EE:74:7D:C9:2A:68
|
|
//
|
|
// To run this on a microcontroller, change the constant value in the file
|
|
// "mcu.go" to set the MAC address of the device you want to discover.
|
|
// Then, flash to the microcontroller board like this:
|
|
//
|
|
// tinygo flash -o circuitplay-bluefruit ./examples/discover
|
|
//
|
|
// Once the program is flashed to the board, connect to the USB port
|
|
// via serial to view the output.
|
|
//
|
|
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"tinygo.org/x/bluetooth"
|
|
)
|
|
|
|
var adapter = bluetooth.DefaultAdapter
|
|
|
|
func main() {
|
|
time.Sleep(3 * time.Second)
|
|
|
|
println("enabling")
|
|
|
|
// Enable BLE interface.
|
|
must("enable BLE stack", adapter.Enable())
|
|
|
|
ch := make(chan bluetooth.ScanResult, 1)
|
|
|
|
// Start scanning.
|
|
println("scanning...")
|
|
err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) {
|
|
println("found device:", result.Address.String(), result.RSSI, result.LocalName())
|
|
if result.Address.String() == connectAddress() {
|
|
adapter.StopScan()
|
|
ch <- result
|
|
}
|
|
})
|
|
|
|
var device *bluetooth.Device
|
|
select {
|
|
case result := <-ch:
|
|
device, err = adapter.Connect(result.Address, bluetooth.ConnectionParams{})
|
|
if err != nil {
|
|
println(err.Error())
|
|
return
|
|
}
|
|
|
|
println("connected to ", result.Address.String())
|
|
}
|
|
|
|
// get services
|
|
println("discovering services/characteristics")
|
|
srvcs, err := device.DiscoverServices(nil)
|
|
must("discover services", err)
|
|
|
|
for _, srvc := range srvcs {
|
|
println("- service", srvc.UUID().String())
|
|
|
|
chars, err := srvc.DiscoverCharacteristics(nil)
|
|
if err != nil {
|
|
println(err)
|
|
}
|
|
for _, char := range chars {
|
|
println("-- characteristic", char.UUID().String())
|
|
}
|
|
}
|
|
|
|
done()
|
|
}
|
|
|
|
func must(action string, err error) {
|
|
if err != nil {
|
|
panic("failed to " + action + ": " + err.Error())
|
|
}
|
|
}
|