Bluetooth

提供: ArchWiki
2015年10月26日 (月) 15:31時点におけるKusanaginoturugi (トーク | 投稿記録)による版 (項目の追加(翻訳はしていません))
ナビゲーションに移動 検索に移動

関連記事

Bluetooth は携帯電話やコンピュータなどの電子デバイス用の近距離無線通信規格です。Linux においては、Bluetooth プロトコルスタックの実装として BlueZ が標準的に使われています。

インストール

公式リポジトリから Bluetooth プロトコルスタックの bluez と、bluetoothctl ユーティリティが入っている bluez-utils パッケージをインストールしてください。systemd によって自動的に起動される dbus デーモンが PIN ペアリングや設定の読み込みを行いますが、Bluetooth プロトコルを使うには bluetooth デーモンが必要です。

共通の bluetooth ドライバーがロードされていなかったら、ロードしてください:

# modprobe btusb

bluetooth の systemd サービスを起動するには次のコマンドを使って下さい:

# systemctl start bluetooth

起動時に bluetooth サービスを有効にするには:

# systemctl enable bluetooth
ノート:
  • デフォルトでは bluetooth デーモンは lp グループのメンバーのユーザーだけに bnep0 デバイスを提供します。bluetooth で接続するときはユーザーをグループに追加しておいてください。必要なグループは /etc/dbus-1/system.d/bluetooth.conf ファイルで変更することができます。
  • Bluetooth アダプターには Wi-Fi カードがバンドルされているものもあります (例: Intel Centrino)。これらの Bluetooth アダプターをカーネルから見えるようにするには、まず Wi-Fi カードを有効にする必要があります (大体はラップトップ上のキーボードショートカットを使う)。
  • Bluetooth カードによってはネットワークアダプタと衝突することがあります (例: Broadcom)。したがって、ネットワークサービスを起動する前に Bluetooth デバイスを接続するようにしてください。

CLI による設定

Bluetoothctl

シェルからデバイスをペアリングするのは最もシンプルで信頼性のある方法の1つです。正確な手順は利用するデバイスとその入力機能によります。以下は /usr/bin/bluetoothctl を使ってデバイスをペアリングする一般的な説明です:

bluetoothctl 対話コマンドを実行してください。help と入力することで利用できるコマンドのリストを表示できます。

  • power on と入力してコントローラの電源を入れて下さい。デフォルトではオフになっています。
  • devices と入力してペアリングするデバイスの MAC アドレスを取得してください。
  • デバイスが表示されないときは scan on コマンドでデバイス検出モードにしてください。
  • agent on でエージェントをオンにしてください。
  • pair MAC Address と入力することでペアリングを実行します (タブ補完が使えます)。
  • PIN がないデバイスを使う場合は、手動でデバイスを信頼する必要があるかもしれません。trust MAC Address と入力してこれを行なって下さい。
  • 最後に、connect MAC_address を使って接続を確立してください。

セッションの例は以下のようになります:

# bluetoothctl 
[NEW] Controller 00:10:20:30:40:50 pi [default]
[bluetooth]# agent KeyboardOnly 
Agent registered
[bluetooth]# default-agent 
Default agent request successful
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:10:20:30:40:50 Discovering: yes
[NEW] Device 00:12:34:56:78:90 myLino
[CHG] Device 00:12:34:56:78:90 LegacyPairing: yes
[bluetooth]# pair 00:12:34:56:78:90
Attempting to pair with 00:12:34:56:78:90
[CHG] Device 00:12:34:56:78:90 Connected: yes
[CHG] Device 00:12:34:56:78:90 Connected: no
[CHG] Device 00:12:34:56:78:90 Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 00:12:34:56:78:90 Paired: yes
Pairing successful
[CHG] Device 00:12:34:56:78:90 Connected: no

再起動の後にもデバイスを有効にしておくには、udev ルールが必要です:

/etc/udev/rules.d/10-local.rules
# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig hci0 up"

以下のような systemd サービスを使うことで、サスペンド・復帰の後にデバイスの電源を自動的にオンにすることが可能です:

/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
#We could also do a 200 char long call to bluez via dbus. Except this does not work since bluez does not react to dbus at this point of the resume sequence and I do not know how I get this service to run at a time it does. So we just ignore bluez and force %i up using hciconfig. Welcome to the 21st century.
#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
ExecStart=/usr/bin/hciconfig %i up

[Install]
WantedBy=suspend.target

グラフィカルフロントエンドによる設定

以下のパッケージによって Bluetooth をカスタマイズするのにグラフィカルインターフェイスを使えるようになります。

GNOME Bluetooth

GNOME BluetoothGNOME の Bluetooth ツールです。gnome-bluetooth パッケージにはバックエンドが含まれており、gnome-shell には状態監視アプレットが、gnome-control-center には設定フロントエンド GUI が入っています。アクティビティに Bluetooth と入力するか、gnome-control-center bluetooth コマンドを実行することでアクセスできます。また、bluetooth-sendto コマンドを実行することでリモートデバイスに直接ファイルを送信することが可能です。

ファイルを受信するには、gnome-user-share パッケージをインストールしてください。Settings -> Sharing からペアリングしたデバイスから Bluetooth 経由で送られたファイルを受け取ることができます。

ヒント: Bluetooth のエントリを Thunar のファイルプロパティメニューの Send To に追加するには、こちら の手順を見て下さい (設定する必要があるコマンドは bluetooth-sendto %F です)。

Bluedevil

BluedevilKDE の Bluetooth ツールです。bluedevil (KDE Plasma 5) または bluedevil4 (KDE 4) パッケージでインストールすることができます。

Dolphin やシステムトレイには Bluetooth アイコンが表示されないので、手動で追加してください。アイコンをクリックすることで Bluedevil を設定したり Bluetooth デバイスを検出できます。KDE システム設定からインターフェイスにアクセスすることも可能です。

Blueberry

Blueberry は全てのデスクトップ環境で動作するフロントエンドです。blueberry パッケージでインストールできます。設定ツール (blueberry) とシステムトレイアプレット (blueberry-tray) が含まれています。

Blueman

Blueman を参照。

Obex を使ってファイルの送受信を行う

ObexFS

KDE や Gnome の Bluetooth パッケージを使うのとは別の選択肢として、ObexFS を使うことで他のファイルシステムと同じように携帯をマウントすることができます。

ノート: ObexFS を使うには、ObexFTP サービスを提供するデバイスが必要です。

obexfs をインストールして、次を実行することでサポートされている携帯をマウントします:

$ obexfs -b MAC_address_of_device /mountpoint

作業が終わったら、デバイスをマウントするために次のコマンドを使って下さい:

$ fusermount -u /mountpoint

他のマウントオプションについては http://dev.zuckschwerdt.org/openobex/wiki/ObexFs を見て下さい。

ノート: bluetooth デバイスが読み取り専用でマウントされるように設定されてないことを確認してください。デバイスの設定から確認することができるはずです。デバイスが読み取り専用でマウントされているとファイルをデバイスに転送しようとした時にパーミッションエラーが発生します。

ObexFTP 転送

あなたのデバイスが Obex FTP サービスをサポートしている場合、デバイスをマウントしたくないときに obexftp コマンドを使ってデバイスとファイルをやり取りすることが可能です。

ノート: obexfs をインストールすれば obexftp も依存パッケージとしてインストールされます。

デバイスにファイルを送るには次のコマンドを実行してください:

$ obexftp -b MAC_address_of_device -p /path/to/file

デバイスからファイルを受け取るには次のコマンドを実行してください:

$ obexftp -b MAC_address_of_device -g filename
ノート: Ensure that the file you are retrieving is in the device's exchange folder. If the file is in a subfolder of the exchange folder then provide the correct path in the command.

Obex Object Push

Obex FTP サービスをサポートしていないデバイスの場合、Obex Object Push がサポートされていないか確認してください。

# sdptool browse XX:XX:XX:XX:XX:XX

出力を見て、Obex Object Push を探し、サービスのチャンネルを覚えておいて下さい。サポートされていれば、ussp-push を使ってデバイスにファイルを送信できます:

# ussp-push XX:XX:XX:XX:XX:XX@CHANNEL file wanted_file_name_on_phone

サンプル

サンプルは全て bluez4 の記事に移動しました。bluez5 で使用するには修正が必要です。

トラブルシューティング

bluetoothctl

bluetoothctl でコントローラが見つからない場合、bluetooth デバイスがブロックされている可能性があります。rfkill を使ってブロックを解除してみてください:

# rfkill unblock bluetooth

gnome-bluetooth

bluetooth-properties でファイルの受信を有効にしようとしたときに以下のメッセージが表示される場合:

Bluetooth OBEX start failed: Invalid path
Bluetooth FTP start failed: Invalid path

xdg-user-dirs をインストールして次を実行してください:

$ xdg-user-dirs-update

次のコマンドでパスを編集できます:

$ vi ~/.config/user-dirs.dirs

Bluetooth USB ドングル

USB ドングルを使う場合は、Bluetooth ドングルが認識されているか確認してください。USB ドングルを挿入する時に /var/log/messages.log を表示する (もしくは systemd で journalctl -f を実行する) ことで確認できます。以下のような表示があるはずです (hci を探して下さい):

Feb 20 15:00:24 hostname kernel: [ 2661.349823] usb 4-1: new full-speed USB device number 3 using uhci_hcd
Feb 20 15:00:24 hostname bluetoothd[4568]: HCI dev 0 registered
Feb 20 15:00:24 hostname bluetoothd[4568]: Listening for HCI events on hci0
Feb 20 15:00:25 hostname bluetoothd[4568]: HCI dev 0 up
Feb 20 15:00:25 hostname bluetoothd[4568]: Adapter /org/bluez/4568/hci0 has been enabled

最初の2行しか表示されていなかった場合は、デバイスを立ち上げる必要があります。 例:

hciconfig -a hci0
hci0:	Type: USB
	BD Address: 00:00:00:00:00:00 ACL MTU: 0:0 SCO MTU: 0:0
	DOWN 
	RX bytes:0 acl:0 sco:0 events:0 errors:0
        TX bytes:0 acl:0 sco:0 commands:0 errors:
# hciconfig hci0 up
hciconfig -a hci0
hci0:	Type: USB
	BD Address: 00:02:72:C4:7C:06 ACL MTU: 377:10 SCO MTU: 64:8
	UP RUNNING 
	RX bytes:348 acl:0 sco:0 events:11 errors:0
        TX bytes:38 acl:0 sco:0 commands:11 errors:0

これが以下のようなエラーで失敗する場合:

Operation not possible due to RF-kill

おそらくコンピュータのハードウェアスイッチか、rfkill ユーティリティが原因です。ユーティリティが原因の場合は次のコマンドで解決できます:

# rfkill unblock all

(場合によって) bluetooth のハードウェアスイッチは USB の bluetooth ドングルへのアクセスも操作します。スイッチを押したり動かしてもう一度デバイスを立ち上げてみてください。

デバイスが検出されているか確認するために bluez-utils に含まれている hcitool を使うことができます。次のコマンドで利用可能なデバイスとその ID・MAC アドレスのリストを表示することが可能です:

$ hcitool dev
Devices:
        hci0	00:1B:DC:0F:DB:40

デバイスに関するもっと詳細な情報は hciconfig を使うことで取得できます。

$ hciconfig -a hci0
hci0:   Type: USB
        BD Address: 00:1B:DC:0F:DB:40 ACL MTU: 310:10 SCO MTU: 64:8
        UP RUNNING PSCAN ISCAN
        RX bytes:1226 acl:0 sco:0 events:27 errors:0
        TX bytes:351 acl:0 sco:0 commands:26 errors:0
        Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH HOLD SNIFF PARK
        Link mode: SLAVE ACCEPT 
        Name: 'BlueZ (0)'
        Class: 0x000100
        Service Classes: Unspecified
        Device Class: Computer, Uncategorized
        HCI Ver: 2.0 (0x3) HCI Rev: 0xc5c LMP Ver: 2.0 (0x3) LMP Subver: 0xc5c
        Manufacturer: Cambridge Silicon Radio (10)

Logitech Bluetooth USB ドングル

Logitech のドングルには Embedded と HCI という二つのモードで動作するものがあります (例: Logitech MX5000)。embedded モードではドングルは USB デバイスをエミュレートするので PC は普通の USB マウス・キーボードを使っていると認識します。

USB BT ミニレシーバの小さな赤いボタンを押すと他のモードが有効になります。BT ドングルの赤いボタンを押しながらコンピュータに接続してください。ボタンを押して3-5秒で、Bluetooth アイコンがシステムトレイに表示されます [1]

また、bluez-hid2hci パッケージをインストールすることもできます。Logitech のドングルを接続すると自動的に切り替えます。

hcitool scan: Device not found

  • Dell ラップトップ (例: Studio 15) では Bluetooth モードを HID から HCI に切り替える必要があります。bluez-hid2hci パッケージをインストールすれば、udev が自動的に切り替えを行うはずです。また、次のコマンドを実行して手動で HCI に切り替えることもできます:
# /usr/lib/udev/hid2hci
  • デバイスが表示されない場合、マシンに Windows オペレーティングシステムがあるときは Windows から bluetooth アダプターを有効にして見て下さい。
  • 場合によっては次のコマンドで有効にできます:
# hciconfig hci0 up

rfkill unblock: Do not unblock

デバイスがソフトブロックされていて connman を動作させている場合、次を試してみて下さい:

$ connmanctl enable bluetooth

コンピュータが表示されない

携帯からコンピュータが表示されないときは、PSCAN と ISCAN を有効にしてください:

# enable PSCAN and ISCAN
$ hciconfig hci0 piscan 
# check it worked
$ hciconfig
hci0:   Type: USB
        BD Address: 00:12:34:56:78:9A ACL MTU: 192:8 SCO MTU: 64:8
        UP RUNNING PSCAN ISCAN
        RX bytes:20425 acl:115 sco:0 events:526 errors:0
        TX bytes:5543 acl:84 sco:0 commands:340 errors:0
ノート: /etc/bluetooth/main.conf の DiscoverableTimeout と PairableTimeout を確認してください。

以下のように /etc/bluetooth/main.conf 内のデバイスクラスを変更してみて下さい:

# Default device class. Only the major and minor device class bits are
# considered.
#Class = 0x000100 (from default config)
Class = 0x100100

This was the only solution to make my computer visible for my phone.

Logitech のキーボードがペアリングできない

Logitech キーボードとペアリングするときにパスキーがない場合は、次のコマンドを入力してください:

# hciconfig hci0 sspmode 0

ペアリング後も、キーボードが接続されないときは、hcidump -at の出力を確認してください。以下のメッセージのように接続・切断が繰り返し表示される場合:

   status 0x00 handle 11 reason 0x13
   Reason: Remote User Terminated Connection

今のところの対処方法としては昔の Bluetooth スタックをインストールするしかありません。

HSP/HFP プロファイル

bluez5 では HSP/HFP プロファイル (TeamSpeak, Skype などのための通信ヘッドセット) のサポートが削除されています。PulseAudio (バージョン6以上) などの HSP/HFP を自力で実装しているアプリケーションをインストールする必要があります。

Thinkpad Bluetooth レーザーマウス

Thinkpad Bluetooth レーザーマウスが数秒の間に (マウスを動かしたりボタンを押したときに) 何度も接続と切断を繰り返す場合、コード 0000 を使ってペアリングしてみてください。

Lite-On Broadcom device

Some of these devices require the firmware to be flashed into the device at boot. The firmware is not provided but can converted from a Microsoft Windows .hex file into a .hcd using hex2hcd.

In order to get the right .hex file, try searching the device vendor:product code obtained with lsusb, for example:

   ...
   Bus 002 Device 004: ID 04ca:2006 Lite-On Technology Corp. Broadcom BCM43142A0 Bluetooth Device
   ...

Alternatively, boot into Windows (a virtual machine installation will suffice) and get the firmware name from the Device Manager utility.

Once you have the .hcd file, copy it into /lib/firmware/brcm/BCM.hcd - this filename is suggested by dmesg and it may change in your case so check your dmesg output in order to verify. Then, flash the .hcd file with the brcm_patchram_plus utility, provided by brcm_patchram_plus-gitAUR[リンク切れ: アーカイブ: aur-mirror]. Load the btusb module:

# modprobe btusb

Make sure that the device is recognized as a bluetooth device. Replace 04ca 2006 with your vendor product pair:

# echo '04ca 2006' > /sys/bus/usb/drivers/btusb/new_id
    

Turn on the device:

# hciconfig hci0 up

Flash the firmware:

# brcm_patchram_plus_usb --patchram fw-04ca_2006.hcd hci0

The device should now be available. See BBS#162688 for information on making these changes persistent.