2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-01-25 02:44:47 +08:00
|
|
|
/*
|
2017-06-10 19:18:29 +08:00
|
|
|
How to run
|
|
|
|
Pass the Bluetooth address or name as the first param:
|
2017-01-25 02:44:47 +08:00
|
|
|
|
2017-06-10 19:18:29 +08:00
|
|
|
go run examples/ble_multiple_generic.go BB-1234 BB-1235
|
2017-01-25 02:44:47 +08:00
|
|
|
|
|
|
|
NOTE: sudo is required to use BLE in Linux
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2017-01-25 02:44:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewSwarmBot(port string) *gobot.Robot {
|
2024-02-05 01:50:43 +08:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(port)
|
2017-01-25 02:44:47 +08:00
|
|
|
access := ble.NewGenericAccessDriver(bleAdaptor)
|
|
|
|
|
|
|
|
work := func() {
|
2024-02-11 01:02:09 +08:00
|
|
|
devName, err := access.GetDeviceName()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Device name:", devName)
|
|
|
|
|
|
|
|
appearance, err := access.GetAppearance()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Appearance:", appearance)
|
2017-01-25 02:44:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bot "+port,
|
|
|
|
[]gobot.Connection{bleAdaptor},
|
|
|
|
[]gobot.Device{access},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
return robot
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
master := gobot.NewMaster()
|
|
|
|
api.NewAPI(master).Start()
|
|
|
|
|
|
|
|
for _, port := range os.Args[1:] {
|
|
|
|
bot := NewSwarmBot(port)
|
|
|
|
master.AddRobot(bot)
|
|
|
|
}
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := master.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-01-25 02:44:47 +08:00
|
|
|
}
|