Arch Linux+bluez 5でBluetoothシリアル通信を行おうとしたら、ドキュメントがなくて苦労したのでその時のメモです。色々手間取ったので、順番が違うところがあったり、抜けがあったりするかもしれませんが、やり直して順序を確認する気にもならないので、このままで公開します。
Markdownの書き方もよくわかっていないので、そのあたりもご容赦ください。
環境
PC: Dell Inspiron 11 3000 (3162)
OS: Arch Linux(bluez 5.43-2)
手順
-
Bluetooth関連のパッケージをインストールする
$ sudo pacman -S bluez bluez-utils
-
一旦、手動でbluetoothサービスを起動し、bluetoothctlを使って、ターゲットのアドレス調査と接続確認を行う。
$ sudo systemctl start bluetooth $ bluetoothctl [bluetooth]# power on [bluetooth]# agent on [bluetooth]# scan on (アドレスを確認する) [bluetooth]# scan off [bluetooth]# pair XX:XX:XX:XX:XX:XX [bluetooth]# quit $ sudo modprobe rfcomm $ sudo /usr/bin/rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1
-
問題がなければ、起動時にBluetoothが使えるように設定する。
-
起動時に電源をONにするため、udevルールを追加する。
sudo vi /etc/udev/rules.d/10-local.rules --- # Set bluetooth power up ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"
-
systemd-udevd.serviceのRestrictAddressFamiliesを修正する
$ sudo vi /etc/systemd/system/systemd-udevd.service --- RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6 AF_BLUETOOTH
-
dbus-org.bluez設定を修正する。ExecStart行に--compatオプションをつけて互換モードで動作させる必要がある。
$ sudo vi /etc/systemd/system/dbus-org.bluez.service --- ExecStart=/usr/lib/bluetooth/bluetoothd --compat
-
サスペンド状態から復帰する際に自動で電源をONにするように、bluetooth-auto-power@.serviceを作成する。
$ sudo vi /etc/systemd/system/bluetooth-auto-power@.service --- [Unit] Description=Bluetooth auto power on After=bluetooth.service sys-subsystem-bluetooth-devices-%i.device suspend.target [Service] Type=oneshot ExecStartPre=/usr/bin/sleep 1 ExecStart=/usr/bin/dbus-send --system --type=method_call --dest=org.bluez /org/bluez/%I org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Powered variant:boolean:true [Install] WantedBy=suspend.target
-
rfcomm.serviceを作成する。ExecStart行でBluetoothのアドレスがXX:XX:XX:XX:XX:XXの機器にチャネル1で接続するように設定している。
$ sudo vi /etc/systemd/system/rfcomm.service --- [Unit] Description=RFCOMM service After=bluetooth.service Requires=bluetooth.service [Service] ExecStart=/usr/bin/rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1 [Install] WantedBy=bluetooth.target
-
-
bluetooth/bluetooth-auto-power/rfcommをsystemdへ登録して再起動する。
$ systemctl enable bluetooth $ systemctl enable bluetooth-auto-power\@hci0 $ systemctl enable rfcomm $ sudo reboot