こちらのページを参考にしました。
BlueZ の example-gatt-server と example-gatt-client の使い方
RaspberryPi (サーバー) の環境構築
sudo apt install bluez
sudo apt install bluez-test-tools
sudo apt install bluez-test-scripts
sudo apt install python3-dbus
Ubuntu 24.04 (クライアント) の環境構築
sudo apt install bluez
sudo apt install bluez-test-tools
sudo apt install bluez-test-scripts
sudo apt install python3-dbus
Bluetoothアダプターの MAC アドレスの調べ方
hciconfig
実行結果
$ hciconfig
hci0: Type: Primary Bus: UART
BD Address: DC:A6:32:70:45:45 ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:3008 acl:21 sco:0 events:151 errors:0
TX bytes:5494 acl:23 sco:0 commands:119 errors:0
RaspberryPi (サーバ)の設定
$ bluetoothctl
# power on
# agent on
# default-agent
# discoverable on
# advertise on
実行結果
$ bluetoothctl
Agent registered
[CHG] Controller DC:A6:32:70:45:45 Pairable: yes
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# agent on
Agent is already registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller DC:A6:32:70:45:45 Discoverable: yes
[bluetooth]# advertise on
[CHG] Controller DC:A6:32:70:45:45 SupportedInstances: 0x04 (4)
[CHG] Controller DC:A6:32:70:45:45 ActiveInstances: 0x01 (1)
Advertising object registered
Tx Power: off
Name: off
Appearance: off
Discoverable: on
[bluetooth]#
Ubuntu 24.04 (クライアント) の設定
RaspberryPi の bluetooth の MAC アドレスを
DC:A6:32:70:45:45 とする。
$ bluetoothctl
# power on
# scan on
# pair DC:A6:32:70:45:45
# connect DC:A6:32:70:45:45
双方で、y を入力する
サーバーの起動
python /usr/share/doc/bluez-test-scripts/examples/example-gatt-server
実行結果
Updating value: [dbus.Byte(6), dbus.Byte(105)]
Updating value: [dbus.Byte(6), dbus.Byte(122)]
Updating value: [dbus.Byte(6), dbus.Byte(99)]
Updating value: [dbus.Byte(6), dbus.Byte(128)]
Updating value: [dbus.Byte(6), dbus.Byte(119)]
Updating value: [dbus.Byte(6), dbus.Byte(113)]
Updating value: [dbus.Byte(6), dbus.Byte(106)]
Updating value: [dbus.Byte(6), dbus.Byte(102)]
クライアントの起動
python /usr/share/doc/bluez-test-scripts/examples/example-gatt-client
実行結果
Getting objects...
Heart Rate Service found: /org/bluez/hci0/dev_DC_A6_32_70_45_45/service004b
Body sensor location value: Chest
HR Measurement notifications enabled
New HR Measurement
HR: 130
Sensor Contact status: contact detected
Energy Expended: 0
New HR Measurement
HR: 106
Sensor Contact status: contact detected
New HR Measurement
HR: 102
Sensor Contact status: contact detected
New HR Measurement
HR: 125
Sensor Contact status: contact detected