LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

Raspberry PiでBLEを検知してみる

Posted at
  • BLE(Bluetooth Low Energy)の発信機
  • BLE→Bluetoothよりも省電力
  • ビーコンから端末までの距離が10メートルであれば「Far(遠い)」設定、1メートル前後であれば「Near(近い)」設定、数センチであれば「Immediate(非常に近い)」設定という全部で3種類の設定を行うことができる

メリット

  • 効率的な情報配信が可能に
  • 緻密なナビゲーションが実現
  • 受信者に応じた情報の提供が可能
  • 条件に合った人が近くにいるかどうかを知らせてくれる
  • さまざまなデバイスに指令を送れる

参考リンク:RaspberryPiにてBLE(Bluetooth Low Energy)接続のセンサーを試す

RaspberryPi に BlueZ をインストールして BLE を検知してみた

RaspberryPiにおけるBLEの設定

PythonからBLEを制御するライブラリの調査

Raspberry Pi 3の Python BLE pygattlibライブラリで TIの SensorTagに接続して制御する方法

BlueZ:Linux上でBluetoothを扱うためのプロジェクト。RaspberryPiでBluetoothやBluetooth Low Energyを扱う場合にもBlueZが必要

pybluez:Pythonから操作するためのライブラリ

インストール等々

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo reboot
$ sudo apt-get dist-upgrade
※これで勝手にBluezの一通りのセットアップはできている

$ sudo apt-get -y install python-pip
$ sudo apt-get install python-dev libbluetooth-dev libboost-all-dev
$ sudo pip install pybluez

使えそうなコマンド

# Bluetooothドングルの情報出力
$ hciconfig

hci0:   Type: Primary  Bus: UART
    BD Address: B8:27:EB:C2:B7:E0  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING 
    RX bytes:764 acl:0 sco:0 events:47 errors:0
    TX bytes:1771 acl:0 sco:0 commands:47 errors:0

# ペアリング待ちの機器を検索する
$ hcitool scan

Scanning ...
    34:08:BC:AD:B7:B0   iPhone

UP RUNNINGの表示があったらBLE起動中?

なければsudo hciconfig hci0 upで起動させる

bluetoothのバージョン確認

$ apt-cache show bluetooth

Package: bluetooth
Source: bluez
Version: 5.43-2+deb9u1
Installed-Size: 67
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: all
Depends: bluez
Suggests: bluez-cups, bluez-obexd
Size: 41360
SHA256: 50279c10c5725d486862c5dad0eda8d344c363343512310072616f7438aeadca
SHA1: 4d9747084cbd1abc6ac6750d25906ab06b130367
MD5sum: 3ae91a20aca82f0627da6cb847df4769
Description: Bluetooth support
 This package provides all of the different plugins supported
 by the Bluez bluetooth stack.
Description-md5: c9c3278c19393dc7c6582892d29bf660
Homepage: http://www.bluez.org
Section: admin
Priority: optional
Filename: pool/main/b/bluez/bluetooth_5.43-2+deb9u1_all.deb

macとRaspberry PiをBluetoothで接続

参考:Raspberry Pi 3にBluetoothでログインする

Raspberry Pi 3 Bluetoothキーボード マウスを使ってみる

# 接続したい機器をペアリングモードにしてから検索
$ hcitool scan
Scanning ...
    34:08:BC:AD:B7:B0   iPhone

# 対話モードを起動
# 必ず管理者権限で実行すること。出ないと`No default controller available`が頻発する
$ sudo bluetoothctl

[NEW] Controller B8:27:EB:C2:B7:E0 raspi [default]
[bluetooth]#

# おまじない?
[bluetooth]# agent KeyboardOnly
Agent is already registered

[bluetooth]# default-agent
Default agent request successful

# 対話モードで再度検索。エラーがでる
[bluetooth]# scan on
No default controller available

参考:raspberry piのbluetoothを使えるようにする

# Raspberry Pi用のパッケージであるpi-bluetoothをインストール
$ sudo apt-get install -y pi-bluetooth

Reading package lists... Done
Building dependency tree       
Reading state information... Done
pi-bluetooth is already the newest version (0.1.9).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

再度、対話モードを起動させて検索してみる

# 検査の結果、iPhoneを発見
[bluetooth]# scan on
Discovery started
[NEW] Device 34:08:BC:AD:B7:B0 iPhone

# ペアリング
[bluetooth]# pair 34:08:BC:AD:B7:B0
Attempting to pair with 34:08:BC:AD:B7:B0
[CHG] Device 34:08:BC:AD:B7:B0 Connected: yes
Request passkey

# iPhoneに表示されるpasskeyを入力
[agent] Enter passkey (number in 0-999999): 773505
[CHG] Device 34:08:BC:AD:B7:B0 Modalias: bluetooth:v004Cp710Dd0C00

...

# 以下が表示されればペアリング完了
Pairing successful

# iPhoneの設定から接続を試みる
# すると以下が表示されるので「yes」を入力
o): yes Authorize service 0000110e-0000-1000-8000-00805f9b34fb (yes/no

[CHG] Device 43:4B:87:A8:95:6B ManufacturerData Key: 0x004c
...

[iPhone]# 

一度接続を切ってから再度接続しようとすると繋がらず…

iPhone側からペアリングを切ってしまった模様

[bluetooth]# connect 34:08:BC:AD:B7:B0

Attempting to connect to 34:08:BC:AD:B7:B0
Failed to connect: org.bluez.Error.Failed

# iPhone側から接続しようとしてもだめ
[CHG] Device 34:08:BC:AD:B7:B0 Connected: yes
[CHG] Device 34:08:BC:AD:B7:B0 Connected: no
[bluetooth]#

参考:https://forums.gentoo.org/viewtopic-t-987544-start-0.html

# ペアリングをリムーブする
[bluetooth]# remove 34:08:BC:AD:B7:B0
[DEL] Device 34:08:BC:AD:B7:B0 iPhone
Device has been removed

# 再起動
$ sudo reboot

# 再スキャン
$ hcitool scan
Scanning ...
    34:08:BC:AD:B7:B0   iPhone

# 対話モード起動
$ sudo bluetoothctl
[NEW] Controller B8:27:EB:C2:B7:E0 raspi [default]
[NEW] Device 78:4F:43:81:5C:42 西尾悟のMacBook Pro

# おまじない
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:C2:B7:E0 Discoverable: yes
[bluetooth]# agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# scan on
Discovery started
[NEW] Device 34:08:BC:AD:B7:B0 iPhone

# ペアリング
[bluetooth]# pair 34:08:BC:AD:B7:B0
Attempting to pair with 34:08:BC:AD:B7:B0
[CHG] Device 34:08:BC:AD:B7:B0 Connected: yes
Request confirmation
[agent] Confirm passkey 888977 (yes/no): yes

Pairing successful

# iPhoneから接続ボタンを押すと何か聞かれるから「yes」と入力
[iPhone]# 

# 信頼できる機器としてマーク
[iPhone]# trust 34:08:BC:AD:B7:B0
[CHG] Device 34:08:BC:AD:B7:B0 Trusted: yes
Changing 34:08:BC:AD:B7:B0 trust succeeded

# 対話モードの終了
[iPhone]# exit
Agent unregistered
[DEL] Controller B8:27:EB:C2:B7:E0 raspi [default]
pi@raspi:~ $ 

# Raspberry Piをシャットダウン
$ sudo shutdown -h now
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