gobot.io/x/gobot@v1.16.0/examples/ble_battery.go (about)

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