hybridgroup.gobot/platforms/bleclient
Ron Evans ab2cc0ffdf
ble(module): update to 0.10 (#1084)
2024-11-05 09:22:08 +01:00
..
LICENSE ble: introduce in drivers folder (#1057) 2024-02-04 18:50:43 +01:00
README.md examples: fix missing checks of return values (#1060) 2024-02-11 15:34:50 +01:00
ble_client_adaptor.go ble(client): add scan timout (#1051) 2024-02-11 17:01:24 +01:00
ble_client_adaptor_options.go ble(client): add scan timout (#1051) 2024-02-11 17:01:24 +01:00
ble_client_adaptor_options_test.go ble(client): add scan timout (#1051) 2024-02-11 17:01:24 +01:00
ble_client_adaptor_test.go ble(module): update to 0.10 (#1084) 2024-11-05 09:22:08 +01:00
btwrapper.go ble(module): update to 0.10 (#1084) 2024-11-05 09:22:08 +01:00
doc.go doc: update links to release or tagged branch (#1069) 2024-02-13 15:58:31 +01:00
helpers_test.go ble(module): update to 0.10 (#1084) 2024-11-05 09:22:08 +01:00
uuid.go ble(client): add scan timout (#1051) 2024-02-11 17:01:24 +01:00
uuid_test.go ble(client): add scan timout (#1051) 2024-02-11 17:01:24 +01:00

README.md

Bluetooth LE

The Gobot BLE adaptor makes it easy to interact with Bluetooth LE aka Bluetooth 4.0 using Go.

It is written using the TinyGo Bluetooth package.

Learn more about Bluetooth LE at http://en.wikipedia.org/wiki/Bluetooth_low_energy

Drivers for several BLE Services can be found in the according driver folder.

How to Install

Please refer to the main README.md

macOS

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.

Ubuntu

Everything should already just compile on most Linux systems.

Windows

You will need to have a GCC compiler such as mingw-w64 installed in order to use BLE on Windows.

How To Connect

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.

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.

Connect on Ubuntu

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.

For example:

go build examples/minidrone.go
sudo ./minidrone AA:BB:CC:DD:EE

Connect on Windows

Hopefully coming soon...

How to Use

Here is an example that uses the BLE "Battery" service to retrieve the current change level of the peripheral device:

package main

import (
  "fmt"
  "os"
  "time"

  "gobot.io/x/gobot/v2"
  "gobot.io/x/gobot/v2/drivers/ble"
  "gobot.io/x/gobot/v2/platforms/bleclient"
)

func main() {
  bleAdaptor := bleclient.NewAdaptor(os.Args[1])
  battery := ble.NewBatteryDriver(bleAdaptor)

  work := func() {
    gobot.Every(5*time.Second, func() {
      level, err := battery.GetBatteryLevel()
      if err != nil {
        fmt.Println(err)
      }
      fmt.Println("Battery level:", level)
    })
  }

  robot := gobot.NewRobot("bleBot",
    []gobot.Connection{bleAdaptor},
    []gobot.Device{battery},
    work,
  )

  if err := robot.Start(); err != nil {
		panic(err)
	}
}