LoginSignup
27
27

More than 3 years have passed since last update.

Raspberry PiにBluetooth機器を接続する

Last updated at Posted at 2020-07-24

Raspberry PiでBluetoothを扱う

備忘録メモです。

Raspberry PiはBluetoothが扱えます。
ちょっと試してみました。

参考:
https://qiita.com/shskwmt/items/fffabf521201f5835214
https://qiita.com/katsuyoshi/items/9d5417495a47c4b15ac1
https://qiita.com/propella/items/6daf3c56e26f709b4141
https://qiita.com/sunaga70/items/89e2d1eecfd73d3fef21

Bluetoothをスキャンする

Bluetoothctl コマンドを利用します。
rootユーザで実行するため、sudo付きです。
プロンプトが[bluetooth]# に変わります。

pi@raspberrypi:~ $ sudo bluetoothctl
Agent registered
[bluetooth]#

スキャンしてみます。
scan onコマンドです。

[bluetooth]# scan on
Discovery started

デバイスが見つかると、ズラズラっと流れてきます。
試しに、SONYのワイヤレスイヤホンを電源ONしてみました。
デバイスが見つかりました。
(MACアドレスはマスクしています)

[NEW] Device AA:BB:CC:AA:BB:CC LE_WI-C600N

スキャンを止めるには、scan offコマンドです。

[bluetooth]# scan off
Discovery stopped

Bluethoothの情報を見る

info <デバイスのMACアドレス> を実行します
(UUIDはマスクしています)

[bluetooth]# info AA:BB:CC:AA:BB:CC
Device CC:98:8B:E1:A5:0D (public)
        Name: LE_WI-C600N
        Alias: LE_WI-C600N
        Paired: no
        Trusted: no
        Blocked: no
        Connected: no
        LegacyPairing: no
        UUID: Unknown                   (0000aabb-0000-0000-0000-001020304050)

利用可能デバイスの一覧

devices コマンドを実行します

[bluetooth]# devices
Device AA:BB:CC:AA:BB:CC WI-C600N

ペアリングする

pair <デバイスのMACアドレス> コマンドを実行します

[bluetooth]# pair AA:BB:CC:AA:BB:CC
Attempting to pair with AA:BB:CC:AA:BB:CC
[CHG] Device AA:BB:CC:AA:BB:CC Connected: yes
[CHG] Device AA:BB:CC:AA:BB:CC Paired: yes
Pairing successful

ペアリング済デバイスを見る

paired-devices コマンドを実行します

[bluetooth]# paired-devices
Device AA:BB:CC:AA:BB:CC WI-C600N

デバイスを削除する

remove <デバイスのMACアドレス> コマンドを実行します

[bluetooth]# remove AA:BB:CC:AA:BB:CC
[DEL] Device AA:BB:CC:AA:BB:CC WI-C600N
Device has been removed

bluetoothプロンプトから抜ける

quit コマンドを実行します

[bluetooth]# quit
pi@raspberrypi:~ $

ヘルプを見る

help コマンドを実行します

[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select <ctrl>                                     Select default controller
devices                                           List available devices
paired-devices                                    List paired devices
system-alias <name>                               Set controller alias
reset-alias                                       Reset controller alias
power <on/off>                                    Set controller power
pairable <on/off>                                 Set controller pairable mode
discoverable <on/off>                             Set controller discoverable mode
agent <on/off/capability>                         Enable/disable agent with given capability
default-agent                                     Set agent as the default one
advertise <on/off/type>                           Enable/disable advertising with given type
set-alias <alias>                                 Set device alias
scan <on/off>                                     Scan for devices
info [dev]                                        Device information
pair [dev]                                        Pair with device
trust [dev]                                       Trust device
untrust [dev]                                     Untrust device
block [dev]                                       Block device
unblock [dev]                                     Unblock device
remove <dev>                                      Remove device
connect <dev>                                     Connect device
disconnect [dev]                                  Disconnect device
menu <name>                                       Select submenu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program
export                                            Print evironment variables
27
27
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
27
27