Go Bluetooth是一个跨平台的软件包,用于Go编程语言控制的蓝牙BLE硬件。它可以运行在Linux,macOS和Windows,以及Nordic单片机上。
Linux | macOS | Windows | Nordic Semi | |
---|---|---|---|---|
API used | BlueZ | CoreBluetooth | WinRT | SoftDevice |
Scanning | ||||
Connect to peripheral | x | |||
Write peripheral characteristics | x | |||
Receive notifications | x | |||
Advertisement | x | x | ||
Local services | x | x | ||
Local characteristics | x | x | ||
Send notifications | x | x |
代码: 全选
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())
}
}
https://github.com/tinygo-org/bluetooth