合理主義的グルメブログ

学生起業家の日常をツラツラと書いています。主に食事情報です。

RaspberryPi同士でbluetooth通信した

タイトルどおり,ラズパイ同士で情報のやりとりをしたく, bluetooth通信を行いました.

基本的には,以下の記事通りにやれば動きます.

qiita.com

いくつか引っかかる点があったので,メモっておきます.

sudoで実行しよう

bluetoothctlを動かすとき,sudoで実行しないとペアリングなどのコマンドを受け付けてくれなかったです.

$ sudo bluetoothctl
[bluetooth]# 

Failed to pair: org.bluez.Error.AuthenticationFailed

hciconfigを使って,ペアリングの実験をおこなう際に以下のようなエラーが発生しました.

(server)$ sudo bluetoothctl
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller SERVER_ADDR Discoverable: yes
(client)$ sudo bluetoothctl
[bluetooth]# pair SERVER_ADDR
Attempting to pair with SERVER_ADDR
Failed to pair: org.bluez.Error.AuthenticationFailed

解決策は以下のサイトで見つけました.

askubuntu.com

結局,client側のbluetoothバイスはoffになっていただけでした.
僕の環境では以下のようにしたら動くようになりました.

(client)$ sudo bluetoothctl
[bluetooth]# power on
Changing power on succeeded
[CHG] Controller CLIENT_ADDR Powered: yes
[bluetooth]# pair SERVER_ADDR
Attempting to pair with SERVER_ADDR
[CHG] Device SERVER_ADDR Connected: yes
[CHG] Device SERVER_ADDR Paired: yes
Pairing successful

Bluetooth自動起動設定

「電源切れてました٩( ᐛ )و」なんて,アホっぽい間違いを犯さないように, 自動で電源が入るように設定しておきます.

/etc/bluetooth/main.confに以下を追記.

[Policy]
AutoEnable=true

/etc/systemd/system/bluetooth.service.d/policy.confに以下を追記.

[Service]
ExecStart=
ExecStart=/usr/lib/bluetooth/bluetoothd --plugin=policy

これで電源起動直後に,Bluetoothバイスが自動的にオンになります(ソースはここ).