52 lines
846 B
Go
52 lines
846 B
Go
//go:build example
|
|
// +build example
|
|
|
|
//
|
|
// Do not build by default.
|
|
|
|
/*
|
|
How to run
|
|
Pass the Bluetooth address or name as the first param:
|
|
|
|
go run examples/ble_battery.go BB-1234
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"gobot.io/x/gobot/v2"
|
|
"gobot.io/x/gobot/v2/drivers/ble"
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
|
)
|
|
|
|
func main() {
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
|
battery := ble.NewBatteryDriver(bleAdaptor)
|
|
|
|
work := func() {
|
|
gobot.Every(5*time.Second, func() {
|
|
level, err := battery.GetBatteryLevel()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println("Battery level:", level)
|
|
})
|
|
}
|
|
|
|
robot := gobot.NewRobot("bleBot",
|
|
[]gobot.Connection{bleAdaptor},
|
|
[]gobot.Device{battery},
|
|
work,
|
|
)
|
|
|
|
if err := robot.Start(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|