🪥

Raspberry Pi に Bluetooth 経由でログインしよう

に公開

Raspberry Pi をヘッドレスで利用している場合に役立つかもしれません。
イーサネット経由(主に SSH)で Raspberry Pi を操作していて、ネットワーク周りの設定をいじって失敗すると、KVM を繋ぐかシリアルコンソールに繋ぐ必要が生じますが、Bluetooth 経由でのログインを用意しておくとバックアップコンソールとして使えます。

テスト条件

  • ハードウェア:Raspberry Pi 5
  • OS:Raspberry Pi OS Lite (64-bit) 2024-11-19

以下の2つのサービスは有効にしておきます(デフォルトで有効になっているはず)。

  • bluetooth.service
  • hciuart.service

手順

bluetoothd の compat オプションを有効にし、SPP(Serial Port Profile)を有効にします。

/etc/systemd/system/dbus-org.bluez.service
- ExecStart=/usr/libexec/bluetooth/bluetoothd
+ ExecStart=/usr/libexec/bluetooth/bluetoothd -C
+ ExecStartPost=/usr/bin/sdptool add SP

Bluetooth の表示名を hostname とは別にする場合は設定します。

/var/lib/bluetooth/[address]/settings
+ Alias=DeviceName

設定を反映させ、パソコンとペアリングします。パソコンから見ると一旦接続され、すぐに切断されます。

Raspberry Pi shell
# systemctl restart bluetooth
# bluetoothctl
    # discoverable on
    (ここでパソコンを操作してペアリング操作をする)
    # quit

getty と HCI を接続します。

/etc/systemd/system/rfcomm.service
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm watch 0 1 agetty rfcomm0 115200 linux

[Install]
WantedBy=multi-user.target
Raspberry Pi shell
# systemctl enable --now rfcomm

この段階でパソコンと Raspberry Pi の Bluetooth 接続は切断状態で大丈夫です。
Windows の場合は COM4COM5 のように2つの「Standard Serial over Bluetooth link」ができるため、2つ目の仮想ポートにコンソールアプリで接続します。コンソールアプリで接続することによって Windows の Bluetooth 機能も接続状態になります。
Linux や Mac の場合は仮想シリアルコンソール名は /dev/tty* または /dev/rfcomm* などになります。

ログインに成功したら、念のため再起動してもう一度ログインできるか確認しておきます。
以上、SSH 経由でネットワーク設定をいじる心理的ハードルが下がったかも?

Discussion