カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

Raspberry Pi 2にUSB-Bluetoothアダプタを接続しWindows・Macとペアリングさせてみた

Raspberry Pi 3ではすでにBluetoothは標準機能で搭載されているが、手元Raspberry Pi 2にはBluetooth機能がないので、
市販のUSB-Bluetoothアダプタを購入して試しにMacbookやWindows7のPCとペアリングさせてみた。

Bluetoothアダプタ選定

まず、Raspberry Pi 2に接続するためのUSB-Bluetoothアダプタを買いに行く。
Bluetoothにもいくつか規格があるが、最新のBluetooth 4.0はBLE(Bluetooth Low Energy)と呼ばれ省電力化され、IoT機器などの領域で注目を集めるようになった。
BLEはそれ以前のバージョンとは互換性がないが、アダプタ製品によってBLEしかサポートしないもの、従来のBluetoothしかサポートしないもの、両方サポートするものの3種類に分類され、それぞれ製品パッケージに以下のように表記されている。

  • Bluetooth: 従来のBluetoothのみサポート
  • Bluetooth Smart: BLEのみサポート
  • Bluetooth Smart Ready: 両方サポート

よって、今回は両方サポートしているBuffaloのBSBT4D09BK というモデルのBluetoothアダプタを購入。
価格も家電量販店で1,680円とお手頃。
確かにBluetooth Smart Readyと書いてある。

iBUFFALO Bluetooth4.0+EDR/LE対応 USBアダプター ブラック BSBT4D09BK

iBUFFALO Bluetooth4.0+EDR/LE対応 USBアダプター ブラック BSBT4D09BK

f:id:osn_th:20170109121508j:plain

Bluetoothアダプタ接続・確認

USBに買ったアダプタを取り付ける。
接続後、lsusbでデバイスを認識していることを確認する。

pi@raspberrypi:~ $ lsusb
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 005: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode) ★デバイス確認
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Bluetoothアダプタがデバイスとして認識されていることが確認できる。

続いてBluetooth機器を操作するツール(Bluetoothスタックというらしい)で有名なBluezを使って、
Bluetoothアダプタが認識されているか確認する。
Bluezはhcitoolhciconfigといったコマンドを使うのだが、Raspbian標準についているようで、すでにコマンドが実行できた。

pi@raspberrypi:~ $ hcitool | grep ver
hcitool - HCI Tool ver 5.23

もしコマンドが使えなかった場合は以下のコマンドでBluetooth系ツール一式をインストールする。

pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get install bluetooth bluez blueman

hichconfigでデバイスがUP RUNNINGであることを確認する。

pi@raspberrypi:~ $ hciconfig
hci0:   Type: BR/EDR  Bus: USB
        BD Address: 00:1B:DC:XX:XX:XX  ACL MTU: 310:10  SCO MTU: 64:8
        UP RUNNING
        RX bytes:676 acl:0 sco:0 events:47 errors:0
        TX bytes:2472 acl:0 sco:0 commands:47 errors:0

Raspberry PiからWindows7-PCとペアリング

手元のWindows 7ノートPCはBluetoothアダプタがついているので、Raspberry Pi側からとりあえずこれとペアリングさせてみる。

まずbluetooth設定ツールを起動する。

pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller 00:1B:DC:XX:XX:XX raspberrypi [default]

対話式のツールになっているので、scan onコマンドでBluetoothをスキャンする。

[bluetooth]# scan on
Discovery started
[CHG] Controller 00:1B:DC:XX:XX:XX Discovering: yes
[NEW] Device 28:C2:DD:XX:XX:XX MY-WINDOWS-PC ★
(・・・略)
[bluetooth]# scan off

scan onコマンドを実行すると、検知したBluetoothデバイスを次々にバックグラウンドで表示する。
目的のWindows-PCが検知できたらscan offコマンドでスキャンを停止する。

続いてペアリングを行う。
pairコマンドで上で確認したWindows-PCのBluetooth MACアドレスを引数に実行する。

[bluetooth]# pair 28:C2:DD:XX:XX:XX
Attempting to pair with 28:C2:DD:XX:XX:XX
[CHG] Device 28:C2:DD:XX:XX:XX Connected: yes

この時点でWindows-PC側でBluetooth接続許可通知が開くので、許可する。
f:id:osn_th:20170109121511p:plain
しばらく待つと、正常にBluetooth接続が完了。
f:id:osn_th:20170109121505p:plain
次回以降に自動接続するようにtrustしておく。

[bluetooth]# trust 28:C2:DD:XX:XX:XX
[CHG] Device 28:C2:DD:XX:XX:XX Trusted: yes
Changing 28:C2:DD:XX:XX:XX trust succeeded

Bluetooth設定ツール終了するにはexitと入力。

[bluetooth]# exit
[DEL] Controller 00:1B:DC:XX:XX:XX raspberrypi [default]

これでWindows7-PCとは正常にペアリングできた!

Macとペアリング

今度は手元のMacBook Proとペアリングさせてみる。
まずMacでBluetoothを検知可能な状態にするため、「システム環境設定」 > 「Bluetooth」のウィンドウを開く。
このウィンドウを開いた状態にしておくと、外部デバイスからBluetoothが検知可能な状態になる
f:id:osn_th:20170109121518p:plain
さきほどWindows-PCとペアリングしたときと同じようにbluetoothctlでスキャン・ペアリングを行う。

pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller 00:1B:DC:XX:XX:XX raspberrypi [default]
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:1B:DC:XX:XX:XX Discovering: yes
[NEW] Device F4:0F:24:XX:XX:XX MacBook Pro ★
[bluetooth]# scan off
[bluetooth]# pair 28:C2:DD:XX:XX:XX
Attempting to pair with 28:C2:DD:XX:XX:XX
[CHG] Device 28:C2:DD:E5:A2:19 Connected: yes

Mac側にペアリング要求画面が開くので、「ペアリング」を行う。
f:id:osn_th:20170109121529p:plain
f:id:osn_th:20170109121524p:plain
次回以降に自動接続するようにtrustし、終了。

[bluetooth]# trust F4:0F:24:XX:XX:XX
[CHG] Device F4:0F:24:XX:XX:XX Trusted: yes
Changing F4:0F:24:XX:XX:XX trust succeeded
[bluetooth]# exit
[DEL] Controller 00:1B:DC:XX:XX:XX raspberrypi [default]

これでMacともペアリングできた。

Raspberry Pi のペアリング状態確認

最後に、ペアリング状態を確認するには以下のコマンドで見ることができる。

pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller 00:1B:DC:XX:XX:XX raspberrypi [default]
[NEW] Device F4:0F:24:XX:XX:XX MacBook Pro
[NEW] Device 28:C2:DD:XX:XX:XX MY-WINDOWS-PC 

ペアリングした二つの機器がDeviceとして表示されていることが確認できる!

まとめ

Raspberry Pi 2に市販のUSB-Bluetoothアダプタを接続し、WindowsとMacのPCとペアリングできるところまで見た。
まだ疎通しただけでBluetoothを使ってデータ送信など何かしたわけではないが、これで同じようにBluetoothデバイスとも連携できるはず。

2017/3/15追記

Bluetoothを使ってシリアル通信によるデータ送受信を試した。
totech.hateblo.jp

以上

iBUFFALO Bluetooth4.0+EDR/LE対応 USBアダプター ブラック BSBT4D09BK

iBUFFALO Bluetooth4.0+EDR/LE対応 USBアダプター ブラック BSBT4D09BK