gobot.io/x/gobot/v2@v2.1.0/examples/ble_multiple_generic.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_multiple_generic.go BB-1234 BB-1235
    12  
    13   NOTE: sudo is required to use BLE in Linux
    14  */
    15  
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"gobot.io/x/gobot/v2"
    23  	"gobot.io/x/gobot/v2/api"
    24  	"gobot.io/x/gobot/v2/platforms/ble"
    25  )
    26  
    27  func NewSwarmBot(port string) *gobot.Robot {
    28  	bleAdaptor := ble.NewClientAdaptor(port)
    29  	access := ble.NewGenericAccessDriver(bleAdaptor)
    30  
    31  	work := func() {
    32  		fmt.Println("Device name:", access.GetDeviceName())
    33  		fmt.Println("Appearance:", access.GetAppearance())
    34  	}
    35  
    36  	robot := gobot.NewRobot("bot "+port,
    37  		[]gobot.Connection{bleAdaptor},
    38  		[]gobot.Device{access},
    39  		work,
    40  	)
    41  
    42  	return robot
    43  }
    44  
    45  func main() {
    46  	master := gobot.NewMaster()
    47  	api.NewAPI(master).Start()
    48  
    49  	for _, port := range os.Args[1:] {
    50  		bot := NewSwarmBot(port)
    51  		master.AddRobot(bot)
    52  	}
    53  
    54  	master.Start()
    55  }