gobot.io/x/gobot/v2@v2.1.0/examples/ble_battery.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to run
     9   Pass the Bluetooth address or name as the first param:
    10  
    11  	go run examples/ble_battery.go BB-1234
    12  
    13   NOTE: sudo is required to use BLE in Linux
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"time"
    22  
    23  	"gobot.io/x/gobot/v2"
    24  	"gobot.io/x/gobot/v2/platforms/ble"
    25  )
    26  
    27  func main() {
    28  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    29  	battery := ble.NewBatteryDriver(bleAdaptor)
    30  
    31  	work := func() {
    32  		gobot.Every(5*time.Second, func() {
    33  			fmt.Println("Battery level:", battery.GetBatteryLevel())
    34  		})
    35  	}
    36  
    37  	robot := gobot.NewRobot("bleBot",
    38  		[]gobot.Connection{bleAdaptor},
    39  		[]gobot.Device{battery},
    40  		work,
    41  	)
    42  
    43  	robot.Start()
    44  }