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