tinygo-org/bluetooth

Public

Cross-platform Bluetooth API for Go and TinyGo. Supports Linux, macOS, Windows, & bare metal using Nordic SoftDevice or HCI (ESP32/Pico-W/etc)

  • Stars1k+6 tuần này
  • Forks199
  • Watchers16
  • Issues99
  • Ngày tạo03/06/2019
  • Cập nhật gần nhất15/10/2026
  • Ngôn ngữC
  • Giấy phép—
README.md42.4 KBRaw

Go Bluetooth

Go Bluetooth

PkgGoDev Linux macOS Windows

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. It also works in the browser with WASM.

By using TinyGo, it can also be used running "bare metal" on microcontrollers produced by Nordic Semiconductor, on the Espressif ESP32-C3 and ESP32-S3 with their onboard radio, or on 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.

Important

You can help TinyGo with a financial contribution using OpenCollective. Please see https://opencollective.com/tinygo for more information. Thank you!

Bluetooth Low Energy Central

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())
	}
}

Bluetooth Low Energy Peripheral

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

Nội dung README thuộc về tinygo-org và cộng đồng đóng góp, giấy phép Không có giấy phép. Nguồn: github.com/tinygo-org/bluetooth