LoginSignup
20
22

More than 5 years have passed since last update.

Raspberry PiでBluetooth 4 (Bluetooth Low Energy) を使えるようにする

Last updated at Posted at 2016-02-07

はじめに

iBeaconを使ってやりたいことがあり、Raspberry Pi (Model B)でBluetoothを使えるようにしました。

1.Bluetooth アダプタ

このために購入したBluetoothアダプタは、Bluetooth 4.0+EDR/LE対応のIODATA USB-BT40LE
使用チップがBroadcom BCM20702なので、Raspbian wheezyでもそのままで大丈夫でしょう。lsusbコマンドで確認してみます。

$ lsusb
Bus 001 Device 002: ID 0424:9514 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 004: ID 0a5c:21e8 Broadcom Corp. 

認識されていますね。

2.Bluetoothドライバのインストール

hcitoolが入っていなかったので、Bluetoothパッケージをインストールします。
参考)Install Bluetooth Dongle on Raspberry Pi

$sudo apt-get install bluetooth

hcitoolコマンドでiBeacon(Bluetooth Low Energy)デバイスをスキャンしてみます。

$ sudo hcitool lescan
LE Scan ...
D1:7F:7E:12:34:56 (unknown)

無事検出できました。
注)Bluetoothアドレスは改変しています。

3.Python 2.7.9のインストール

PythonでiBeanconを検出するプログラムを作るつもりです。
このRaspberry Piには2.7.3がインストールされていたので2.7系の最新版である、2.7.9をインストールします。
参考)Raspberry Piに最新版の各種ツール・言語・ライブラリをインストールする

$ wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
$ tar xvf Python-2.7.9.tgz
$ cd Python-2.7.9
$ ./configure
$ make
$ sudo make install

pythonを起動してバージョンを確認します。

$ python
Python 2.7.9 (default, Feb  6 2016, 12:56:09) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

バージョン2.7.9になっていました。

4.BlueZのインストール

PythonでBluetoothを扱うため、Bluetoothプロトコルスタック BlueZをインストールします。
参考)iBeacon Raspberry Pi Scanner in PythonのInstalling blueZ on the Raspberry Pi

最新版は5.37なので、それをインストールします。

$ sudo apt-get install libusb-dev 
$ sudo apt-get install libdbus-1-dev 
$ sudo apt-get install libglib2.0-dev --fix-missing
$ sudo apt-get install libudev-dev 
$ sudo apt-get install libical-dev
$ sudo apt-get install libreadline-dev
$ sudo apt-get install libdbus-glib-1-dev

$ sudo mkdir bluez
$ cd bluez
$ sudo wget www.kernel.org/pub/linux/bluetooth/bluez-5.37.tar.gz
$ sudo gunzip bluez-5.37.tar.gz
$ sudo tar xvf bluez-5.37.tar
$ cd bluez-5.37
$ sudo ./configure --disable-systemd
$ sudo make
$ sudo make install

$ sudo apt-get install python-bluez

$ sudo shutdown -r now

インストールが完了したらhciconfigコマンドを見てみましょう。

$ hciconfig
hci0:   Type: BR/EDR  Bus: USB
    BD Address: 5C:F3:70:01:23:45  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING PSCAN 
    RX bytes:1724 acl:0 sco:0 events:92 errors:0
    TX bytes:1364 acl:0 sco:0 commands:87 errors:0
20
22
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
20
22