Handle BLE from golang at Edison

Intel Edison is popular. In particular, it is very suitable for IoT that both WiFi and BLE can be used.

The Edison standard NodeJS you have a development environment that uses is mounted, but still here is where you want to handle in golang.

Paypal / gatt

To handle the BLE in Golang, github.com/paypal/gatt is the best. This library is implemented by golang all from the operation of BLE's peripheral etc. It works with pure golang.

The sample program is described below. In this example, the Main gatt.NewDevice create a device in, onStateChanged and onPeriphDiscovered only to register the two functions of the handler, you can the search for BLE equipment.

func onStateChanged(d gatt.Device, s gatt.State) {
     fmt.Println("State:", s)
     switch s {
     case gatt.StatePoweredOn:
             fmt.Println("scanning...")
             d.Scan([]gatt.UUID{}, false)
             return
     default:
             d.StopScanning()
     }
}
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
     fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
     fmt.Println("  Local Name        =", a.LocalName)
     fmt.Println("  TX Power Level    =", a.TxPowerLevel)
     fmt.Println("  Manufacturer Data =", a.ManufacturerData)
     fmt.Println("  Service Data      =", a.ServiceData)
}
func main() {
     d, err := gatt.NewDevice(option.DefaultClientOptions...)
     if err != nil {
             log.Fatalf("Failed to open device, err: %s\n", err)
     }
     // Register handlers.
     d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))
     d.Init(onStateChanged)
     select {}
}

Using paypal / gatt makes it easy to handle BLE from golang, but there is one problem.

paypal / gatt is, HCI USER CHANNEL are based on the assumption, which are equipped only with Linux 3.14 or later. In other words, in Edison running on Linux 3.10, paypal / gatt will not work.

Noble

However, to work with NodeJS noble Do I work is, and I think examined go and noble is build during the installation of the following two small helper process, run I understood what I was using at the time.

When installing noble, the following two binaries are completed.

  • Node_modules / noble / build / Release / hci-ble
  • Node_modules / noble / build / Release / l2cap-ble

As its name implies, hci-ble deals with HCI and l2cap-ble deals with L2CAP. The two are Bluez for that link directly to the library and work with kernel 3.10.

Noblechild

I came up with the examination so far.

"If you use the noble helper process you can handle BLE in kernel 3.10 even in Pure golang"

That was made in that noblechild is.

Noblechild starts the helper process of noble and handles the helper process like noble, so that Pure golang can handle BLE from kernel 3.10 as well.

How to use

  1. Install noble.
  2. NOBLE_TOPDIR the environment variable, and set it to the top of the directory where you installed the noble.

that's all. After that, we provide almost the same interface as paypal / gatt, so you can use it as it is.

func main() {
     d, err := noblechild.NewDevice(DefaultClientOptions...)
     if err != nil {
             log.Fatalf("Failed to open device, err: %s\n", err)
     }
     d.Handle(noblechild.PeripheralDiscovered(onPeriphDiscovered))
     d.Init(onStateChanged)
}

Summary

To handle the BLE from golang in Edison Intel noblechild it has created a library called.

Since it is a huge hack, the line is not good. If Edison goes up to 3.14, we can directly use paypal / gatt, so we think that it is the position as a connection between the past.

It is useful to handle BLE with golang. We also MQTT gateways that are associated with fuji is also mounted on, so we work without a problem, I think you would like to publish them.

Looking for work

ツキノワ株式会社 In, BLE and MQTT, is looking for your work on Golang. Please feel free to contact us.