Skip to content

Commit ca51250

Browse files
authored
docs: a lot more information on how to install/use Go Bluetooth
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent b70c118 commit ca51250

File tree

1 file changed

+172
-26
lines changed

1 file changed

+172
-26
lines changed

README.md

Lines changed: 172 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
[![PkgGoDev](https://pkg.go.dev/badge/pkg.go.dev/github.com/tinygo-org/bluetooth)](https://pkg.go.dev/github.com/tinygo-org/bluetooth)
44
[![CircleCI](https://circleci.com/gh/tinygo-org/bluetooth/tree/dev.svg?style=svg)](https://circleci.com/gh/tinygo-org/bluetooth/tree/dev)
55

6-
This package provides a cross-platform Bluetooth Low Energy module for Go that can be used on operating systems such as Linux, macOS, and Windows.
6+
Go Bluetooth is a cross-platform package for using [Bluetooth Low Energy](https://en.wikipedia.org/wiki/Bluetooth_Low_Energy) hardware from the Go programming language.
77

8-
It can also be used running "bare metal" on microcontrollers such as those produced by Nordic Semiconductor.
8+
It works on typical operating systems such as [Linux](#linux), [macOS](#macos), and [Windows](#windows).
99

10-
This example scans for peripheral devices and then displays information about them as they are discovered:
10+
It can also be used running "bare metal" on microcontrollers produced by [Nordic Semiconductor](https://www.nordicsemi.com/) by using [TinyGo](https://tinygo.org/).
11+
12+
The Go Bluetooth package can be used to create both Bluetooth Low Energy Centrals as well as to create Bluetooth Low Energy Peripherals.
13+
14+
## Bluetooth Low Energy Central
15+
16+
A typical Bluetooth Low Energy Central would be your laptop computer or mobile phone.
17+
18+
This example shows a central that scans for peripheral devices and then displays information about them as they are discovered:
1119

1220
```go
1321
package main
@@ -37,6 +45,50 @@ func must(action string, err error) {
3745
}
3846
```
3947

48+
## Bluetooth Low Energy Peripheral
49+
50+
A typical Bluetooth Low Energy Peripheral would be a temperature sensor or heart rate sensor.
51+
52+
This example shows a peripheral that advertises itself as being available for connection:
53+
54+
```go
55+
package main
56+
57+
import (
58+
"time"
59+
60+
"tinygo.org/x/bluetooth"
61+
)
62+
63+
var adapter = bluetooth.DefaultAdapter
64+
65+
func main() {
66+
// Enable BLE interface.
67+
must("enable BLE stack", adapter.Enable())
68+
69+
// Define the peripheral device info.
70+
adv := adapter.DefaultAdvertisement()
71+
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
72+
LocalName: "Go Bluetooth",
73+
}))
74+
75+
// Start advertising
76+
must("start adv", adv.Start())
77+
78+
println("advertising...")
79+
for {
80+
// Sleep forever.
81+
time.Sleep(time.Hour)
82+
}
83+
}
84+
85+
func must(action string, err error) {
86+
if err != nil {
87+
panic("failed to " + action + ": " + err.Error())
88+
}
89+
}
90+
```
91+
4092
## Current support
4193

4294
| | Linux | macOS | Windows | Nordic Semi |
@@ -53,44 +105,132 @@ func must(action string, err error) {
53105

54106
## Linux
55107

56-
The current support for Linux uses BlueZ via the D-Bus interface. It 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 as well as BLE peripheral. You need to have a fairly recent version of BlueZ, for example v5.48 is the latest released version for Ubuntu/Debian.
108+
Go Bluetooth support for Linux uses [BlueZ](http://www.bluez.org/) via the [D-Bus](https://en.wikipedia.org/wiki/D-Bus) interface thanks to the https://github.com/muka/go-bluetooth package. This should work with most distros that support BlueZ such as Ubuntu, Debian, Fedora, and Arch Linux, among others.
109+
110+
Linux can be used both as a BLE Central or as a BLE Peripheral.
111+
112+
### Installation
113+
114+
You need to have a fairly recent version of BlueZ, for example v5.48 is the latest released version for Ubuntu/Debian.
115+
116+
sudo apt update
117+
sudo apt install bluez
118+
119+
Once you have done this, you can obtain the Go Bluetooth package using Git:
120+
121+
git clone https://github.com/tinygo-org/bluetooth.git
122+
123+
### Compiling
124+
125+
After you have followed the installation, you should be able to compile/run the "scanner" test program:
126+
127+
cd bluetooth
128+
go run ./examples/scanner
57129

58130
## macOS
59131

60-
The current macOS support uses the CoreBluetooth libraries provided by macOS. As a result, it should work with most versions of macOS, although it will require compiling using whatever specific version of XCode is required by your version of the operating system. The macOS support only can only act as a BLE central at this time, with some additional development support needed for full functionality.
132+
Go Bluetooth support for macOS uses the [CoreBluetooth](https://developer.apple.com/documentation/corebluetooth?language=objc) libraries thanks to the https://github.com/JuulLabs-OSS/cbgo package.
133+
134+
As a result, it should work with most versions of macOS, although it will require compiling using whatever specific version of XCode is required by your version of the operating system.
135+
136+
The macOS support only can only act as a BLE Central at this time, with some additional development work needed for full functionality.
137+
138+
### Installation
139+
140+
In order to compile Go Bluetooth code targeting macOS, you must do so on macOS itself. In other words, we do not currently have cross compiler support. You must also have XCode tools installed:
141+
142+
xcode-select --install
143+
144+
Once you have done this, you can obtain the Go Bluetooth package using Git:
145+
146+
git clone https://github.com/tinygo-org/bluetooth.git
147+
148+
### Compiling
149+
150+
After you have followed the installation, you should be able to compile/run the "scanner" test program:
151+
152+
cd bluetooth
153+
go run ./examples/scanner
61154

62155
## Windows
63156

64-
The Windows support is still experimental, and needs additional development to be useful.
157+
Go Bluetooth support for Windows uses the [WinRT Bluetooth](https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter?view=winrt-19041) interfaces by way of the https://github.com/tinygo-org/bluetooth/winbt package that is part of this package.
158+
159+
The Windows support is still experimental, and needs additional development to be useful. At this time, it can only be used to perform scanning operations as a BLE Central.
160+
161+
For specifics please see https://github.com/tinygo-org/bluetooth/issues/13
162+
163+
### Installation
164+
165+
Once you have done this, you can obtain the Go Bluetooth package using Git:
166+
167+
git clone https://github.com/tinygo-org/bluetooth.git
168+
169+
### Compiling
170+
171+
After you have followed the installation, you should be able to compile/run the "scanner" test program:
172+
173+
cd bluetooth
174+
go run .\examples\scanner
65175

66176
## Nordic Semiconductor
67177

68-
As you can see above, there is bare metal support for several chips from Nordic Semiconductors.
178+
Go Bluetooth has bare metal support for several chips from Nordic Semiconductor that include a built-in Bluetooth Low Energy radio.
179+
180+
This support requires compiling your programs using [TinyGo](https://tinygo.org/).
181+
182+
You must also use firmware provided by Nordic Semiconductor known as the "SoftDevice". 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.
69183

70-
These chips are supported through [TinyGo](https://tinygo.org/).
184+
The Nordic Semiconductor SoftDevice can be used both as a BLE Central or as a BLE Peripheral, depending on which chip is being used. See the "Supported Chips" section below.
71185

72-
This support also requires firmware provided by Nordic Semi known as the "SoftDevice". 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.
186+
### Installation
73187

74-
At the moment the following chips are supported:
188+
You must install TinyGo to be able to compile bare metal code using Go Bluetooth. Follow the instructions for your operating system at https://tinygo.org/getting-started/
75189

76-
* [nRF52832](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF52832) with the [S132](https://www.nordicsemi.com/Software-and-Tools/Software/S132) SoftDevice (version 6).
77-
* [nRF52840](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF52840) with the [S140](https://www.nordicsemi.com/Software-and-Tools/Software/S140) SoftDevice (version 6 and 7).
78-
* [nRF51822](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF51822) with the [S110](https://www.nordicsemi.com/Software-and-Tools/Software/S110) SoftDevice (version 8). This SoftDevice does not support all features (e.g. scanning).
190+
Once you have installed TinyGo, you can install the Go Bluetooth package by running:
191+
192+
git clone https://github.com/tinygo-org/bluetooth.git
193+
194+
Check your desired target board for any additional installation requirements.
79195

80196
### Adafruit "Bluefruit" boards
81197

82-
The support for boards created by Adafruit already have the Nordic Semi SoftDevice firmware pre-installed. You can use TinyGo with this package without any additional steps required. Supported boards include:
198+
The line of "Bluefruit" boards created by Adafruit already have the SoftDevice firmware pre-loaded. This means you can use TinyGo and the Go Bluetooth package without any additional steps required. Supported Adafruit boards include:
83199

84200
* [Adafruit Circuit Playground Bluefruit](https://www.adafruit.com/product/4333)
85201
* [Adafruit CLUE Alpha](https://www.adafruit.com/product/4500)
86202
* [Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062)
87203
* [Adafruit ItsyBitsy nRF52840](https://www.adafruit.com/product/4481)
88204

89-
### Flashing the SoftDevice
205+
After you have installed TinyGo and the Go Bluetooth package, you should be able to compile/run code for your device.
206+
207+
For example, this command can be used to compile and flash an Adafruit Circuit Playground Bluefruit board with the example we provide that turns it into a BLE server to control the built-in NeoPixel LEDs:
208+
209+
tinygo flash -target circuitplay-bluefruit ./examples/circuitplay
210+
211+
### BBC micro:bit
90212

91-
Other boards that use supported chips from Nordic Semi that do not already have the SoftDevice firmware must have it installed on the board in order to use this package.
213+
The [BBC micro:bit](https://microbit.org/) uses an nRF51 chip with a CMSIS-DAP interface. This means you must first flash the SoftDevice firmware by copying the .hex file to the device, for example (on Linux):
92214

93-
Flashing the SoftDevice can be tricky. If you have [nrfjprog](https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Command-Line-Tools) 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.
215+
cp ./s110_nrf51_8.0.0/s110_nrf51_8.0.0_softdevice.hex /media/yourusername/MICROBIT/
216+
217+
Once you have copied the SoftDevice firmware to the BBC micro:bit, you can flash your TinyGo program. To do this we must use the CMSIS-DAP interface instead of the default mass-storage interface, by using the `-programmer=cmsis-dap` flag:
218+
219+
tinygo flash -target=microbit-s110v8 -programmer=cmsis-dap ./examples/heartrate
220+
221+
### Supported Chips
222+
223+
The following Nordic Semiconductor chips are currently supported:
224+
225+
* [nRF51822](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF51822) with the [S110](https://www.nordicsemi.com/Software-and-Tools/Software/S110) SoftDevice (version 8). This SoftDevice does not support all features (e.g. scanning).
226+
* [nRF52832](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF52832) with the [S132](https://www.nordicsemi.com/Software-and-Tools/Software/S132) SoftDevice (version 6).
227+
* [nRF52840](https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF52840) with the [S140](https://www.nordicsemi.com/Software-and-Tools/Software/S140) SoftDevice (version 6 and 7).
228+
229+
### Flashing the SoftDevice on Other Boards
230+
231+
To use a board that uses one of the above supported chips from Nordic Semiconductor, other then those already listed, you will probably need to install the SoftDevice firmware on the board yourself in order to use it with TinyGo and the Go Bluetooth package.
232+
233+
Flashing the SoftDevice can sometimes be tricky. If you have [nrfjprog](https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Command-Line-Tools) 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.
94234

95235
nrfjprog -f nrf52 --eraseall
96236
nrfjprog -f nrf52 --program path/to/softdevice.hex
@@ -101,14 +241,6 @@ After that, don't reset the board but instead flash a new program to it. For exa
101241

102242
Flashing will normally reset the board.
103243

104-
For boards that use the CMSIS-DAP interface (such as the [BBC micro:bit](https://microbit.org/)), this works a bit different. Flashing the SoftDevice is done by simply copying the .hex file to the device, for example (on Linux):
105-
106-
cp path/to/softdevice.hex /media/yourusername/MICROBIT/
107-
108-
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:
109-
110-
tinygo flash -target=microbit-s110v8 -programmer=cmsis-dap ./examples/heartrate
111-
112244
## API stability
113245

114246
**The API is not stable!** Because many features are not yet implemented and some platforms (e.g. Windows and macOS) are not yet fully 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.
@@ -122,7 +254,7 @@ This package will probably remain unstable until the following has been implemen
122254

123255
* Scan filters. For example, to filter on service UUID.
124256
* Bonding and private addresses.
125-
* Usable support on at least two desktop operating systems.
257+
* Full support for all features at least two desktop operating systems.
126258
* Maybe some Bluetooth Classic support, such as A2DP.
127259

128260
## Contributing
@@ -131,6 +263,20 @@ Your contributions are welcome!
131263

132264
Please take a look at our [CONTRIBUTING.md](./CONTRIBUTING.md) document for details.
133265

266+
## Frequently Asked Questions
267+
268+
**Q. Where can I get an introduction to Bluetooth Low Energy, GAP, GATT, etc.?**
269+
270+
A. Please see this excellent article from our friends at Adafruit: https://learn.adafruit.com/introduction-to-bluetooth-low-energy
271+
272+
**Q. What is a client and server in BLE?**
273+
274+
A. Please see https://devzone.nordicsemi.com/f/nordic-q-a/71/what-is-a-client-and-server-in-ble
275+
276+
**Q. Can a device be both a GATT client and GATT server?**
277+
278+
A. Yes, but this is not currently supported by Go Bluetooth. Current support is either to act as a central in client mode, or as a peripheral in server mode.
279+
134280
## License
135281

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

0 commit comments

Comments
 (0)