gobot.io/x/gobot@v1.16.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 [ble](https://github.com/currantlabs/ble) package by [@roylee17](https://github.com/roylee17). Thank you!
     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/...
    18  ```
    19  
    20  ### OSX
    21  
    22  You need to have XCode installed to be able to compile code that uses the Gobot BLE adaptor on OSX. This is because the `ble` package uses a CGo based implementation.
    23  
    24  ### Ubuntu
    25  
    26  Everything should already just compile on most Linux systems.
    27  
    28  ## How To Connect
    29  
    30  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.
    31  
    32  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.
    33  
    34  ### OSX
    35  
    36  To run any of the Gobot BLE code you must use the `GODEBUG=cgocheck=0` flag in order to get around some of the issues in the CGo-based implementation.
    37  
    38  If you connect by name, then you do not need to worry about the Bluetooth LE ID. However, if you want to connect by ID, OS X uses its own Bluetooth ID system which is different from the IDs used on Linux. The code calls thru the XPC interfaces provided by OSX, so as a result does not need to run under sudo.
    39  
    40  For example:
    41  
    42      GODEBUG=cgocheck=0 go run examples/minidrone.go 8b2f8032290143e18fc7c426619632e8
    43  
    44  ### Ubuntu
    45  
    46  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`.
    47  
    48  For example:
    49  
    50      go build examples/minidrone.go
    51      sudo ./minidrone AA:BB:CC:DD:EE
    52  
    53  ### Windows
    54  
    55  Hopefully coming soon...
    56  
    57  ## How to Use
    58  
    59  Here is an example that uses the BLE "Battery" service to retrieve the current change level of the peripheral device:
    60  
    61  ```go
    62  package main
    63  
    64  import (
    65  	"fmt"
    66  	"os"
    67  	"time"
    68  
    69  	"gobot.io/x/gobot"
    70  	"gobot.io/x/gobot/platforms/ble"
    71  )
    72  
    73  func main() {
    74  	bleAdaptor := ble.NewClientAdaptor(os.Args[1])
    75  	battery := ble.NewBatteryDriver(bleAdaptor)
    76  
    77  	work := func() {
    78  		gobot.Every(5*time.Second, func() {
    79  			fmt.Println("Battery level:", battery.GetBatteryLevel())
    80  		})
    81  	}
    82  
    83  	robot := gobot.NewRobot("bleBot",
    84  		[]gobot.Connection{bleAdaptor},
    85  		[]gobot.Device{battery},
    86  		work,
    87  	)
    88  
    89  	robot.Start()
    90  }
    91  ```