Raspberry Pi 5　Ubuntu の環境でのBLEを調べてみた 

　調べただけです。
 　記憶では、自分でドライバとか入れていないと思います。素のままです。



### 環境

- Raspberry Pi 5 8GB
- 追加ボード；NVMe Base for Raspberry Pi 5 (NVMe Base by Pimoroni)
- Crucial クルーシャル P2シリーズ 500GB 3D NAND NVMe PCIe M.2 SSD CT500P2SSD8
- Ubuntu Desktop ***24.04LTS***(64-bit)


 ### 準備
 　Windows10のコマンドプロンプトからsshで入って作業をしています。
  
```
$ bluetoothctl show
Controller 2C:CF:67:30:D7:91 (public)
        Manufacturer: 0x0131 (305)
        Version: 0x09 (9)
        Name: yoshi
        Alias: yoshi
        Class: 0x006c0000 (7077888)
        Powered: yes
        Discoverable: no
        DiscoverableTimeout: 0x000000b4 (180)
        Pairable: no
        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
        UUID: Handsfree Audio Gateway   (0000111f-0000-1000-8000-00805f9b34fb)
        UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
        UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
        UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
        UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
        UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
        UUID: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
        Modalias: usb:v1D6Bp0246d0548
        Discovering: no
        Roles: central
        Roles: peripheral
Advertising Features:
        ActiveInstances: 0x00 (0)
        SupportedInstances: 0x05 (5)
        SupportedIncludes: tx-power
        SupportedIncludes: appearance
        SupportedIncludes: local-name
```
　BLEスキャンが動作します。
 
```
$ sudo hcitool lescan
[sudo] password for yoshi:
LE Scan ...
C5:B6:89:FA:2A:40 (unknown)
F4:9E:6C:7D:BA:41 (unknown)
```


```
$ hciconfig -a
hci0:   Type: Primary  Bus: UART
        BD Address: 2C:CF:67:30:D7:91  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING
        RX bytes:4577 acl:0 sco:0 events:445 errors:0
        TX bytes:68228 acl:0 sco:0 commands:443 errors:0
        Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH SNIFF
        Link mode: PERIPHERAL ACCEPT
        Name: 'yoshi'
        Class: 0x6c0000
        Service Classes: Rendering, Capturing, Audio, Telephony
        Device Class: Miscellaneous,
        HCI Version: 5.0 (0x9)  Revision: 0x17e
        LMP Version: 5.0 (0x9)  Subversion: 0x6119
        Manufacturer: Cypress Semiconductor (305)
```
　仮想環境にします。
 
```
$ python3 -V
Python 3.12.3
$ python3 -m venv env
$ source env/bin/activate

(env) yoshi@yoshi:~$ pip list
Package Version
------- -------
pip     24.0
(env) yoshi@yoshi:~$ pip install bluepy
Collecting bluepy
  Downloading bluepy-1.3.0.tar.gz (217 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 217.9/217.9 kB 5.1 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: bluepy
  Building wheel for bluepy (pyproject.toml) ... done
  Created wheel for bluepy: filename=bluepy-1.3.0-cp312-cp312-linux_aarch64.whl size=586489 sha256=c3c252d3b109f18416f5cafaf957bfa30d8ba621cf3f22847d602171f41550b7
  Stored in directory: /home/yoshi/.cache/pip/wheels/02/13/09/aa10ef1c6fdbd9c45c9e75f371f7e2aab6c947a950f1965e01
Successfully built bluepy
Installing collected packages: bluepy
Successfully installed bluepy-1.3.0

(env) yoshi@yoshi:~$ pip list
Package Version
------- -------
bluepy  1.3.0
pip     24.0

```
```
(env) yoshi@yoshi:~$ bluetoothd -v
5.72
```
　セントラル用ライブラリ
```
(env) yoshi@yoshi:~$ pip install pybleno
Collecting pybleno
  Downloading pybleno-0.11.tar.gz (24 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pybleno
  Building wheel for pybleno (pyproject.toml) ... done
  Created wheel for pybleno: filename=pybleno-0.11-py3-none-any.whl size=31351 sha256=5c3be6b182365dedf9ff1b65cc95c1372b6056547afae65a5e75eb0aabfb3fe5
  Stored in directory: /home/yoshi/.cache/pip/wheels/45/ea/82/69a2557465f89a75aaa4932c2da7a954fe7572ee3ac74f7456
Successfully built pybleno
Installing collected packages: pybleno
Successfully installed pybleno-0.11
(env) yoshi@yoshi:~$ pip list
Package Version
------- -------
bluepy  1.3.0
pip     24.0
pybleno 0.11
```
　うごかないですね。。。ラズパイOSでも動かなくなっていました。

 ### bluez
　プリインストールされているらしいので、上記のコマンドが動いたみたいですが、D-Busライブラリなどはインストールされていません。
 ```
 $ sudo apt -y install bluetooth bluez
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
bluez is already the newest version (5.72-0ubuntu5).
bluez set to manually installed.
Suggested packages:
  bluez-meshd
The following NEW packages will be installed:
  bluetooth
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 7,210 B of archives.
After this operation, 41.0 kB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports noble/universe arm64 bluetooth all 5.72-0ubuntu5 [7,210 B]
Fetched 7,210 B in 1s (9,741 B/s)
Selecting previously unselected package bluetooth.
(Reading database ... 190118 files and directories currently installed.)
Preparing to unpack .../bluetooth_5.72-0ubuntu5_all.deb ...
Unpacking bluetooth (5.72-0ubuntu5) ...
Setting up bluetooth (5.72-0ubuntu5) ...
```
　いや、全部インストールされているようですね。

 ```
 $ pip install dbus-python
Collecting dbus-python
  Downloading dbus-python-1.3.2.tar.gz (605 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 605.5/605.5 kB 10.2 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: dbus-python
  Building wheel for dbus-python (pyproject.toml) ... done
  Created wheel for dbus-python: filename=dbus_python-1.3.2-cp312-cp312-linux_aarch64.whl size=122334 sha256=1ab88e467d4f08dd82abcbc51156186cd93c7a82711961675b4d90028c604c8a
  Stored in directory: /home/yoshi/.cache/pip/wheels/af/10/cf/199cea3104083e08fd54f5f1d61656138c62b9677b0f78f1b6
Successfully built dbus-python
Installing collected packages: dbus-python
Successfully installed dbus-python-1.3.2
(env) yoshi@yoshi:~$ pip list
Package     Version
----------- -------
bluepy      1.3.0
dbus-python 1.3.2
pip         24.0
pybleno     0.11
```

https://github.com/bluez/bluez/blob/master/test/example-gatt-client
ダウンロードし、実行します。
3か月前に修正が入っています。
2020年4月の内容とはそうとうかわっています。

　https://www.denshi.club/cookbook/wireless/ble/post-13.html

　エラーが出る。
```
 $ python ex03python.py
Traceback (most recent call last):
  File "/home/yoshi/ex03python.py", line 5, in <module>
    from gi.repository import GLib
ModuleNotFoundError: No module named 'gi'
```
　GLibは入っているようだが、giが呼び出せていない。。。
 あきらめました。