LoginSignup
8
8

More than 5 years have passed since last update.

GolangでBLE (Bluetooth Low Enagy)を使う

Last updated at Posted at 2018-01-18

私が使ったライブラリを紹介します。

最初は以下のものを使用していたのですが、数分後に接続が切れるという症状がでて解決できなかったため上記にのりかえました。

必要なカーネルのパッチ

https://github.com/paypal/gatt に書いてあるとおり、カーネルがv3.14よりも古い場合は以下のパッチをバックポートする必要があります。

On Linux:

To gain complete and exclusive control of the HCI device, gatt uses HCI_CHANNEL_USER (introduced in Linux v3.14) instead of HCI_CHANNEL_RAW. Those who must use an older kernel may patch in these relevant commits from Marcel Holtmann:

Bluetooth: Introduce new HCI socket channel for user operation
Bluetooth: Introduce user channel flag for HCI devices
Bluetooth: Refactor raw socket filter into more readable code

私が最初に試したときには 3.4のカーネルを使用していたので、上記をがんばってバックポートしました。

cgoを使っていない利点

この2つのBLEライブラリはどちらもpure goでcgoを使用していません。そのためクロスコンパイルが楽チンです。

まずはPC Linuxで確認。(BLEで目的のデバイスを発見して、全ての項目をダンプさせる)
次にクロスコンパイルして、ラズベリーパイ(カーネルは4.x)で同じように動作するか確認。
次にカーネル3.4のARM Linuxで同じように動作するか確認。(カーネルパッチがうまくいっているか)
これらが完全に同一のソースコードでできました。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8