Cross-platform Bluetooth API for Go and TinyGo. Supports Linux, macOS, Windows, and bare metal using Nordic SoftDevice or HCI
Find a file
Ayke van Laethem c034fbca54
all: change NewAdvertisement to DefaultAdvertisement
Instead of attempting to allocate multiple advertisement instances, only
use one by default. If needed, a NewAdvertisement method could be added
in the future for devices that actually do support multiple
advertisements at a time.

The motivation for this change is fix an inconsistency with the nrf51
(which already had the behavior of DefaultAdvertisement) and the
discovery that nrf52 devices also don't seem to support more than one
advertisement instance, even though their API does allow for multiple
instances. But the primary motivation is that for consistency with
hosted systems, it would be best if the nrf port would automatically
re-enable advertisement when a connection is lost (or made).

While BlueZ does support more than one instance, it is implemented by
simply iterating through the active advertisement instances so could
also be implemented by doing that manually. I haven't checked the
behavior of Windows and MacOS - but as always, the API is not yet stable
and can be changed if needed.
2020-06-01 18:09:44 +02:00
.circleci Move to github.com/tinygo-org/bluetooth 2020-05-31 16:45:26 +02:00
examples all: change NewAdvertisement to DefaultAdvertisement 2020-06-01 18:09:44 +02:00
s110_nrf51_8.0.0 Add S110 version 8.0.0 SoftDevice for nrf51822 2020-05-31 15:40:57 +02:00
s132_nrf52_6.1.1 Initial commit 2019-11-08 10:58:02 +01:00
s140_nrf52_7.0.1 Port current codebase to S140 softdevice 2020-03-06 14:24:50 +01:00
winbt Add initial Windows support 2020-05-30 21:51:27 +02:00
.gitattributes Initial commit 2019-11-08 10:58:02 +01:00
.gitignore Add smoke tests that run in CI 2020-05-24 19:19:37 +02:00
adapter.go Add flag for debug printing 2020-05-25 17:26:24 +02:00
adapter_linux.go all: change NewAdvertisement to DefaultAdvertisement 2020-06-01 18:09:44 +02:00
adapter_nrf51.go all: change DefaultAdapter function to global 2020-06-01 13:27:56 +02:00
adapter_nrf528xx.go all: change DefaultAdapter function to global 2020-06-01 13:27:56 +02:00
adapter_s110.c Add S110 version 8.0.0 SoftDevice for nrf51822 2020-05-31 15:40:57 +02:00
adapter_s110.go Add S110 version 8.0.0 SoftDevice for nrf51822 2020-05-31 15:40:57 +02:00
adapter_s132.c softdevice: make code more flexible for other SoftDevices 2020-03-06 13:46:42 +01:00
adapter_s132.go softdevice: make code more flexible for other SoftDevices 2020-03-06 13:46:42 +01:00
adapter_s140.c Port current codebase to S140 softdevice 2020-03-06 14:24:50 +01:00
adapter_s140.go Port current codebase to S140 softdevice 2020-03-06 14:24:50 +01:00
adapter_sd.go all: change DefaultAdapter function to global 2020-06-01 13:27:56 +02:00
adapter_windows.go all: change DefaultAdapter function to global 2020-06-01 13:27:56 +02:00
error_sd.go softdevice: make code more flexible for other SoftDevices 2020-03-06 13:46:42 +01:00
gap.go all: simplify advertisement configuration 2020-06-01 14:20:34 +02:00
gap_linux.go all: change NewAdvertisement to DefaultAdvertisement 2020-06-01 18:09:44 +02:00
gap_nrf51.go all: change NewAdvertisement to DefaultAdvertisement 2020-06-01 18:09:44 +02:00
gap_nrf528xx.go all: change NewAdvertisement to DefaultAdvertisement 2020-06-01 18:09:44 +02:00
gap_windows.go Move to github.com/tinygo-org/bluetooth 2020-05-31 16:45:26 +02:00
gatts.go gatts: implement write event for characteristics 2019-11-09 13:55:34 +01:00
gatts_linux.go Add Go module support 2020-05-25 00:10:28 +02:00
gatts_sd.go softdevice: make code more flexible for other SoftDevices 2020-03-06 13:46:42 +01:00
go.mod Move to github.com/tinygo-org/bluetooth 2020-05-31 16:45:26 +02:00
go.sum Add initial Windows support 2020-05-30 21:51:27 +02:00
LICENSE Initial commit 2019-11-08 10:58:02 +01:00
mac.go Add support for scanning for devices 2020-05-28 11:57:02 +02:00
Makefile Add S110 version 8.0.0 SoftDevice for nrf51822 2020-05-31 15:40:57 +02:00
README.md all: simplify advertisement configuration 2020-06-01 14:20:34 +02:00
uuid.go go fmt 2020-05-27 16:50:21 +02:00
uuid_sd.go softdevice: make code more flexible for other SoftDevices 2020-03-06 13:46:42 +01:00
uuid_test.go linux: add support for services 2019-11-16 19:11:59 +00:00

Go Bluetooth

CircleCI GoDoc

This package attempts to build a cross-platform Bluetooth Low Energy module for Go. It currently supports the following systems:

Windows Linux Nordic chips
API used WinRT BlueZ (over D-Bus) SoftDevice
Scanning ✔️ ✔️ ✔️
Advertisement ✔️ ✔️
Local services ✔️ ✔️
Local characteristics ✔️

Baremetal support

As you can see above, there is support for some chips from Nordic Semiconductors. At the moment the following chips are supported:

  • The nRF52832 with the S132 SoftDevice (version 6).
  • The nRF52840 with the S140 SoftDevice (version 7).
  • The nRF51822 with the S110 SoftDevice (version 8). This SoftDevice does not support all features (e.g. scanning).

These chips are supported through TinyGo.

The SoftDevice is a binary blob that implements the BLE stack. There are other (open source) BLE stacks, but the SoftDevices are pretty solid and have all the qualifications you might need. Other BLE stacks might be added in the future.

Flashing the SoftDevice

Flashing the SoftDevice can be tricky. If you have nrfjprog installed, you can erase the flash and flash the new BLE firmware using the following commands. Replace the path to the hex file with the correct SoftDevice, for example s132_nrf52_6.1.1/s132_nrf52_6.1.1_softdevice.hex for S132 version 6.

nrfjprog -f nrf52 --eraseall
nrfjprog -f nrf52 --program path/to/softdevice.hex

After that, don't reset the board but instead flash a new program to it. For example, you can flash the Heart Rate Sensor example using tinygo (modify the -target flag as needed for your board):

tinygo flash -target=pca10040-s132v6 ./examples/heartrate

Flashing will normally reset the board.

For boards that use the CMSIS-DAP interface (such as the BBC micro:bit), this works a bit different. Flashing the SoftDevice is done by simply copying the .hex file to the device, for example (on Linux):

cp path/to/softdevice.hex /media/yourusername/MICROBIT/

Flashing will then need to be done a bit differently, using the CMSIS-DAP interface instead of the mass-storage interface normally used by TinyGo:

tinygo flash -target=microbit-s110v8 -programmer=cmsis-dap ./examples/heartrate

API stability

The API is not stable! Because many features are not yet implemented and some platforms (e.g. MacOS) are not yet supported, it's hard to say what a good API will be. Therefore, if you want stability you should pick a particular git commit and use that. Go modules can be useful for this purpose.

Some things that will probably change:

  • Add options to the Scan method, for example to filter on UUID.
  • Connect/disconnect events.
  • The behavior around advertisement. Nordic SoftDevices stop advertising when a device connects, which is somewhat unintuitive.

License

This project is licensed under the BSD 3-clause license, see the LICENSE file for details.

The SoftDevices from Nordic are licensed under a different license, check the license file in the SoftDevice source directory.