Raspberry PiでBluetoothを扱う

具体的にはBluetoothのキーボード(Nintendo Wireless Keyboard)とヘッドセット(LBT-AR200C2)もつないでみる。

Bluetooth関連のパッケージのインストール

$ sudo apt-get install bluetooth bluez-utils

BDアドレスの取得

各パッケージのインストールが完了したらBluetoothドングルをRaspberry Piに接続し,lsusbコマンドで接続確認。

$ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 014: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)
Bus 001 Device 015: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

Bluetooth機器をRaspbianに登録するにはBD(Bluetooth Device)アドレスが必要なので,各Bluetooth機器を検出可能状態にして,hcitoolコマンドを実行

$ hcitool scan
Scanning ...
    00:1B:41:03:CD:33   LBT-AR200C2
    A4:C0:E1:46:7F:A6   Nintendo Wireless Keyboard

ペアリング

↑で出てきたBDアドレスを利用して↓のようにペアリング。コマンド実行後に任意のPIN番号を入力します。ただし,機種によっては番号が固定されているものもあるので注意!

$ sudo bluez-simple-agent hci0 BDアドレス

僕の使用しているBluetoothのヘッドセット(LBT-AR200C2)のPIN番号は「0000」で固定されていました。 Bluetoothのキーボードは特にPIN番号の指定がなかったので,RaspberryPi側で任意のPIN番号を入力した後に,Bluetoothキーボード側でも「PIN番号+Enter」を入力するとペアリング完了。例えばキーボードのペアリングは↓のような感じ。

$ sudo bluez-simple-agent hci0 A4:C0:E1:46:7F:A6
RequestPinCode (/org/bluez/9061/hci0/dev_A4_C0_E1_46_7F_A6)
Enter PIN Code: 9876
Release
New device (/org/bluez/9061/hci0/dev_A4_C0_E1_46_7F_A6)

Bluetooth機器の自動接続許可

先程ペアリングが完了しましたが,このままでは毎回手動で接続しないといけないので,自動接続を許可します。

$ sudo bluez-test-device trusted BDアドレス yes
$ sudo bluez-test-input connect BDアドレス

これで次回以降は自動接続されます。