GATT 通信: RaspberryPi をサーバー、Ubuntu 24.04 をクライアント

こちらのページを参考にしました。
[BlueZ の example-gatt-server と example-gatt-client の使い方](https://jitaku.work/it/category/bluetooth/bluez-gatt-sample/)

## RaspberryPi （サーバー) の環境構築

```bash
sudo apt install bluez
sudo apt install bluez-test-tools
sudo apt install bluez-test-scripts
sudo apt install python3-dbus
```

## Ubuntu 24.04 （クライアント) の環境構築

```bash
sudo apt install bluez
sudo apt install bluez-test-tools
sudo apt install bluez-test-scripts
sudo apt install python3-dbus
```

## Bluetoothアダプターの MAC アドレスの調べ方

```bash
hciconfig
```

実行結果

```text
$ 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 （サーバ）の設定

```text
$ bluetoothctl
# power on
# agent on
# default-agent
# discoverable on
# advertise on
```

実行結果

```text
$ 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 とする。

```text
$ bluetoothctl
# power on
# scan on
# pair DC:A6:32:70:45:45
# connect DC:A6:32:70:45:45
```

双方で、y を入力する

Client
![client.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a1bdaa00-cc71-ca45-88be-1a60035e0261.png)

Server
![server.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/3ea42ef4-93f1-ead9-b744-52191299231d.png)


## サーバーの起動

```bash
python /usr/share/doc/bluez-test-scripts/examples/example-gatt-server
```

実行結果

```text
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)]
```

## クライアントの起動

```bash
python /usr/share/doc/bluez-test-scripts/examples/example-gatt-client
```

実行結果

```text
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
```

## Android の LightBlue でサーバーに接続
![LightBlue_sep21_2024.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/4449477e-ddb4-6e5b-ea7d-3d43b8d897fb.png)