Go Bluetooth is a cross-platform package for using Bluetooth Low Energy hardware from the Go programming language.
It works on typical operating systems such as Linux, macOS, and Windows.
By using TinyGo, it can also be used running "bare metal" on microcontrollers produced by Nordic Semiconductor, or boards that have a Bluetooth co-processor that uses the Bluetooth Host Controller Interface (HCI).
The Go Bluetooth package can be used to create both Bluetooth Low Energy Centrals as well as to create Bluetooth Low Energy Peripherals.
A typical Bluetooth Low Energy Central would be your laptop computer or mobile phone.
This example shows a central that scans for peripheral devices and then displays information about them as they are discovered:
package main
import (
"tinygo.org/x/bluetooth"
)
var adapter = bluetooth.DefaultAdapter
func main() {
// Enable BLE interface.
must("enable BLE stack", adapter.Enable())
// Start scanning.
println("scanning...")
err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
println("found device:", device.Address.String(), device.RSSI, device.LocalName())
})
must("start scan", err)
}
func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}A typical Bluetooth Low Energy Peripheral would be a temperature sensor or heart rate sensor.
This example shows a peripheral that advertises itself as being available for connection:
package main
import (
"context"
"time"
"tinygo.org/x/bluetooth"
)
var adapter = bluetooth.DefaultAdapter
func main() {
// Enable BLE interface.
must("enable BLE stack", adapter.Enable())
ctx, cancel := context.WithCancel(context.Background())
adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) {
if connected {
println("device connected:", device.Address.String())
return
}
println("device disconnected:", device.Address.String())
cancel()
})
// Define the peripheral device info.
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go Bluetooth",
}))
// Start advertising
must("start adv", adv.Start())
// Stop advertising to release resources
defer adv.Stop()
println("advertising...")
<- ctx.Done()
}
func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}| Linux | macOS | Windows | Nordic Semi | ESP32 (NINA-FW) | CYW43439 (RP2040-W) | |
|---|---|---|---|---|---|---|
| API used | BlueZ | CoreBluetooth | WinRT | SoftDevice | HCI | HCI |
| Scanning | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Connect to peripheral | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Write peripheral characteristics | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Receive notifications | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| Advertisement | ✔️ | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
| Local services | ✔️ | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
| Local characteristics | ✔️ | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
| Send notifications | ✔️ | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
Go Bluetooth support for Linux uses BlueZ via the D-Bus interface. This should work with most distros that support BlueZ such as Ubuntu, Debian, Fedora, and Arch Linux, among others.
Linux can be used both as a BLE Central or as a BLE Peripheral.
You need to have a fairly recent version of BlueZ, for example v5.48 is the latest released version for Ubuntu/Debian.
sudo apt update
sudo apt install bluez
Once you have done this, you can obtain the Go Bluetooth package using Git:
git clone https://github.com/tinygo-org/bluetooth.git
After you have followed the installation, you should be able to compile/run the "scanner" test program:
cd bluetooth
go run ./examples/scanner
Go Bluetooth support for macOS uses the CoreBluetooth libraries thanks to the