0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Arch Linux + Bluez 5でシリアル通信

Last updated at Posted at 2016-12-19

Arch Linux+bluez 5でBluetoothシリアル通信を行おうとしたら、ドキュメントがなくて苦労したのでその時のメモです。色々手間取ったので、順番が違うところがあったり、抜けがあったりするかもしれませんが、やり直して順序を確認する気にもならないので、このままで公開します。
Markdownの書き方もよくわかっていないので、そのあたりもご容赦ください。

環境

PC: Dell Inspiron 11 3000 (3162)
OS: Arch Linux(bluez 5.43-2)

手順

  1. Bluetooth関連のパッケージをインストールする

    $ sudo pacman -S bluez bluez-utils
    
  2. 一旦、手動で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
    
  3. 問題がなければ、起動時にBluetoothが使えるように設定する。

    1. 起動時に電源を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"
      
    2. systemd-udevd.serviceのRestrictAddressFamiliesを修正する

      $ sudo vi /etc/systemd/system/systemd-udevd.service
      ---
      RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6 AF_BLUETOOTH
      
    3. dbus-org.bluez設定を修正する。ExecStart行に--compatオプションをつけて互換モードで動作させる必要がある。

      $ sudo vi /etc/systemd/system/dbus-org.bluez.service
      ---
      ExecStart=/usr/lib/bluetooth/bluetoothd --compat
      
    4. サスペンド状態から復帰する際に自動で電源を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
      
    5. 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
      
  4. bluetooth/bluetooth-auto-power/rfcommをsystemdへ登録して再起動する。

    $ systemctl enable bluetooth
    $ systemctl enable bluetooth-auto-power\@hci0
    $ systemctl enable rfcomm
    $ sudo reboot
    
0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?