Bluetooth Low Energy for Go
Go to file
Greg Pomerantz d70bbfed13 Guard against Gio's null-view detach event (recents-wipe SIGABRT)
Gio's GioView.onDestroyView sends ViewEvent{View: 0} as its detach
signal when the view is destroyed — i.e. the activity going away on a
recents-wipe. An app that forwards the event to Enable() passes a zero
view through to registerFragment, whose GetObjectClass(null) aborts the
process (JNI DETECTED ERROR: java_object == null in call to
GetObjectClass).

Enable() now returns early on a zero view so all callers are covered
even if they forward the event unfiltered, and registerFragment
null-checks as defense in depth. Same fix as pad (fdbffc9); verified
there on a Pixel 9 Pro / Android 17 where the recents swipe used to
crash the app.
2026-08-20 22:16:23 -04:00
depjars Fixes to depjars/build.go 2019-12-30 11:26:24 -05:00
gatt Working Android implementation. 2019-11-27 10:44:37 -05:00
ns Update to gioui.org@v0.0.0-20211113093644. Updates for MacOS 11.5. 2021-11-15 14:14:45 -05:00
.gitignore Documentation 2019-11-27 11:11:42 -05:00
ble_android.go Guard against Gio's null-view detach event (recents-wipe SIGABRT) 2026-08-20 22:16:23 -04:00
ble_darwin.go Update to gioui.org@v0.0.0-20211113093644. Updates for MacOS 11.5. 2021-11-15 14:14:45 -05:00
ble.go Improve handling of peripheral disconnections and some refactoring. 2019-12-05 08:49:41 -05:00
Ble.jar Port to gio gioui.org@20200829. 2020-09-05 14:12:29 -04:00
blessed-full.jar Add Timber license. Add depjars/ and go:generate commands to build 2019-12-13 13:20:30 -05:00
BlessedConnect.java Port to gio gioui.org@20200829. 2020-09-05 14:12:29 -04:00
go.mod Port to gio gioui.org@20200829. 2020-09-05 14:12:29 -04:00
go.sum Port to gio gioui.org@20200829. 2020-09-05 14:12:29 -04:00
jni_android.c Guard against Gio's null-view detach event (recents-wipe SIGABRT) 2026-08-20 22:16:23 -04:00
jni_android.go Partial android implementation, including Enable(), readyToScan(), 2019-11-22 16:15:02 -05:00
jni_android.h Bump gio version, use window.Do() instead of RegisterFragment(). 2020-06-19 14:35:49 -04:00
LICENSE Add license information. 2019-12-11 09:58:27 -05:00
LICENSE-BLESSED Add license information. 2019-12-11 09:58:27 -05:00
LICENSE-TIMBER Add Timber license. Add depjars/ and go:generate commands to build 2019-12-13 13:20:30 -05:00
nswrap.yaml Update to gioui.org@v0.0.0-20211113093644. Updates for MacOS 11.5. 2021-11-15 14:14:45 -05:00
README.md Switch back to Blessed library after bug fixes. 2019-12-10 09:13:32 -05:00
timber.jar Add Timber license. Add depjars/ and go:generate commands to build 2019-12-13 13:20:30 -05:00

BLE

A modern, cross-platform Go library for Bluetooth Low Energy

This package is a working but incomplete interface for access to Bluetooth Low Energy devices. It is intended to support multiple operating systems with a uniform API. Currently, MacOS and Android are supported. Android support requires the use of Gio.

The API is asyncrhonous and event driven. To access Bluetooth hardware, first create a BLE variable with NewBLE(). On Android, you must also call Enable with a pointer to your Gio Window.

import (
	...
	"git.wow.st/gmp/ble"
	"gioui.org/app"
)

func runloop() {
	b := ble.NewBle()
	w := app.NewWindow()
	b.Enable(w)
	...

Once enabled, you can call the primary API functions, Scan, StopScan, Connect, and CancelConnection. You can also query the state of the Bluetooth hardware by calling State.

Calling Events on your BLE object returns a channel on which API events are delivered. These events notify your program when the state of the BLE hardware changes (e.g. powered on or off), peripherals are connected, services or characteristics are discovered, or values for characteristics are updated.

Using Gio, your main event loop will look something like this:

	for {
		select {
		case e := <-b.Events():
			switch e := e.(type) {
				// Handle BLE Events
			}
		}
		case e := <-w.Events():
			switch e := e.(type) {
				// Handle Gio events
			}
		}
	}

An example heart rate monitor app can be found at git.wow.st/gmp/hrm.