gobot.io/x/gobot/v2@v2.1.0/platforms/ble/README.md (about)

     1  # Bluetooth LE
     2  
     3  The Gobot BLE adaptor makes it easy to interact with Bluetooth LE aka Bluetooth 4.0 using Go.
     4  
     5  It is written using the [TinyGo Bluetooh](tinygo.org/x/bluetooth) package.
     6  
     7  Learn more about Bluetooth LE at http://en.wikipedia.org/wiki/Bluetooth_low_energy
     8  
     9  This package also includes drivers for several well-known BLE Services:
    10  
    11  - Battery Service
    12  - Device Information Service
    13  - Generic Access Service
    14  
    15  ## How to Install
    16  ```
    17  go get -d -u gobot.io/x/gobot/v2/...
    18  ```
    19  
    20  ### macOS
    21  
    22  You need to have XCode installed to be able to compile code that uses the Gobot BLE adaptor on macOS. This is because the `bluetooth` package uses a CGo based implementation.
    23  
    24  ### Ubuntu
    25  
    26  Everything should already just compile on most Linux systems.
    27  
    28  ### Windows
    29  
    30  You will need to have a GCC compiler such as [mingw-w64](https://github.com/mingw-w64/mingw-w64) installed in order to use BLE on Windows.
    31  
    32  ## How To Connect
    33  
    34  When using BLE a "peripheral" aka "server" is something you connect to such a a pulse meter. A "central" aka "client" is what does the connecting, such as your computer or mobile phone.
    35  
    36  You need to know the BLE ID of the peripheral you want to connect to. The Gobot BLE client adaptor also lets you connect to a peripheral by friendly name.
    37  
    38  ### Ubuntu
    39  
    40  On Linux the BLE code will need to run as a root user account. The easiest way to accomplish this is probably to use `go build` to build your program, and then to run the requesting executable using `sudo`.
    41  
    42  For example:
    43  
    44      go build examples/minidrone.go
    45      sudo ./minidrone AA:BB:CC:DD:EE
    46  
    47  ### Windows
    48  
    49  Hopefully coming soon...
    50  
    51  ## How to Use
    52  
    53  Here is an example that uses the BLE "Battery" service to retrieve the current change level of the peripheral device:
    54  
    55  ```go
    56  package main
    57  
    58  import (
    59  	"fmt"
    60  	"os"
    61  	"time"
    62  
    63  	"gobot.io/x/gobot/v2"
    64  	"gobot.io/x/gobot/v2/platforms/ble"
    65  )
    66  
    67  func main() {
    68  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    69  	battery := ble.NewBatteryDriver(bleAdaptor)
    70  
    71  	work := func() {
    72  		gobot.Every(5*time.Second, func() {
    73  			fmt.Println("Battery level:", battery.GetBatteryLevel())
    74  		})
    75  	}
    76  
    77  	robot := gobot.NewRobot("bleBot",
    78  		[]gobot.Connection{bleAdaptor},
    79  		[]gobot.Device{battery},
    80  		work,
    81  	)
    82  
    83  	robot.Start()
    84  }
    85  ```