2015-06-08 03:38:19 +08:00
|
|
|
# Bluetooth LE
|
|
|
|
|
2016-12-28 02:54:13 +08:00
|
|
|
The Gobot BLE adaptor makes it easy to interact with Bluetooth LE aka Bluetooth 4.0 using Go.
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
It is written using the [TinyGo Bluetooth](tinygo.org/x/bluetooth) package.
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
Learn more about Bluetooth LE at <http://en.wikipedia.org/wiki/Bluetooth_low_energy>
|
2016-12-28 02:54:13 +08:00
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
Drivers for several BLE Services can be found in the according [driver folder](https://github.com/hybridgroup/gobot/tree/release/drivers/ble).
|
2015-06-08 03:38:19 +08:00
|
|
|
|
|
|
|
## How to Install
|
2023-06-05 00:36:55 +08:00
|
|
|
|
|
|
|
Please refer to the main [README.md](https://github.com/hybridgroup/gobot/blob/release/README.md)
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2022-09-16 03:44:27 +08:00
|
|
|
### macOS
|
2016-12-19 20:43:32 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
You need to have XCode installed to be able to compile code that uses the Gobot BLE adaptor on macOS. This is because the
|
|
|
|
`bluetooth` package uses a CGo based implementation.
|
2016-12-19 20:43:32 +08:00
|
|
|
|
|
|
|
### Ubuntu
|
|
|
|
|
|
|
|
Everything should already just compile on most Linux systems.
|
|
|
|
|
2022-09-16 03:44:27 +08:00
|
|
|
### Windows
|
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
You will need to have a GCC compiler such as [mingw-w64](https://github.com/mingw-w64/mingw-w64) installed in order to use
|
|
|
|
BLE on Windows.
|
2022-09-16 03:44:27 +08:00
|
|
|
|
2015-06-08 03:38:19 +08:00
|
|
|
## How To Connect
|
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
When using BLE a "peripheral" aka "server" is something you connect to such a a pulse meter. A "central" aka "client" is
|
|
|
|
what does the connecting, such as your computer or mobile phone.
|
2016-12-19 20:43:32 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
You need to know the BLE ID of the peripheral you want to connect to. The Gobot BLE client adaptor also lets you connect
|
|
|
|
to a peripheral by friendly name.
|
2016-12-19 20:43:32 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
### Connect on Ubuntu
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
On Linux the BLE code will need to run as a root user account. The easiest way to accomplish this is probably to use
|
|
|
|
`go build` to build your program, and then to run the requesting executable using `sudo`.
|
2016-12-19 20:43:32 +08:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
go build examples/minidrone.go
|
|
|
|
sudo ./minidrone AA:BB:CC:DD:EE
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
### Connect on Windows
|
2015-06-08 03:38:19 +08:00
|
|
|
|
2016-12-19 20:43:32 +08:00
|
|
|
Hopefully coming soon...
|
2015-06-08 03:38:19 +08:00
|
|
|
|
|
|
|
## How to Use
|
|
|
|
|
2016-12-19 20:43:32 +08:00
|
|
|
Here is an example that uses the BLE "Battery" service to retrieve the current change level of the peripheral device:
|
2015-06-08 03:38:19 +08:00
|
|
|
|
|
|
|
```go
|
2016-12-19 20:43:32 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-05 00:36:55 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
2016-12-19 20:43:32 +08:00
|
|
|
|
2023-06-05 00:36:55 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/bleclient"
|
2016-12-19 20:43:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-05 01:50:43 +08:00
|
|
|
bleAdaptor := bleclient.NewAdaptor(os.Args[1])
|
2023-06-05 00:36:55 +08:00
|
|
|
battery := ble.NewBatteryDriver(bleAdaptor)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(5*time.Second, func() {
|
2024-02-11 01:02:09 +08:00
|
|
|
level, err := battery.GetBatteryLevel()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Battery level:", level)
|
2023-06-05 00:36:55 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bleBot",
|
|
|
|
[]gobot.Connection{bleAdaptor},
|
|
|
|
[]gobot.Device{battery},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-12-19 20:43:32 +08:00
|
|
|
}
|
2015-06-08 03:38:19 +08:00
|
|
|
```
|