#author("2026-05-09T13:39:05+09:00","default:TESLA2","TESLA2")
#author("2026-05-09T14:31:53+09:00","default:TESLA2","TESLA2")
[[Python-Servo]]

* BLEを使った Raspberry Pi Zero <-> Raspberry Pi 4 <-> Raspberry Pi Zero 通信 [#b9b515eb]

** BLEのアドレス [#mba34e5d]

 testpip) pi@rpi-202603261445:~/python $ hciconfig
 hci0: Type: Primary  Bus: UART
 BD Address: B8:27:EB:ED:CD:2A  ACL MTU: 1021:8  SCO MTU: 64:1
 UP RUNNING 
 RX bytes:6056 acl:128 sco:0 events:331 errors:0
 TX bytes:37576 acl:118 sco:0 commands:259 errors:0


** [#x08b5382]

 2. 動かない時のチェックリスト(重要)
 プログラムを修正しても NoReply や接続エラーが出る場合、OSレベルの設定が原因です。
 1. Bluetoothサービスの権限
 2. Raspberry PiでBlueZをユーザー権限から操作する場合、権限エラーが出ることがあります。テストとして、両方のプログラムを sudo で実行してみてください。
Bash

sudo python3 peripheral2.py
 3. 




 4. 既存のアドバタイズのクリア
 5. 以前の実行情報が残っていると競合します。一度 Bluetooth をリセットしてください。
Bash

sudo hciconfig hci0 down
 6. sudo hciconfig hci0 up
 7. 




 8. Bluetoothデーモンの設定 (Peripheral側)
 9. bluez-peripheral を使う場合、Bluetoothを「Experimental」モードで動かす必要がある場合があります。
    * /lib/systemd/system/bluetooth.service を開き、
    * ExecStart=/usr/lib/bluetooth/bluetoothd の末尾に -E を追加して ExecStart=/usr/lib/bluetooth/bluetoothd -E にする。
     * その後 sudo systemctl daemon-reload && sudo systemctl restart bluetooth を実行。
 3. Central側 (Pi 4) のヒント
 Central側の NoReply は、相手(Zero)が接続を承認した直後にプロセスが固まった際によく出ます。
 * Zero側の peripheral2.py を起動し、「Peripheral 準備完了」が出てから Pi 4 側を実行してください。
 * もし Bleak で Service not found が出る場合は、一度 bluetoothctl で remove <ZeroのMACアドレス> を実行してペアリング情報を消去してから試してください。
 まずは 「双方 sudo で実行」 と 「Bluetoothサービスのリセット」 を試すのが解決への近道です!


** sudo で実行していても、D-Bus側で拒否されることがあります。もし上記コードでもエラーが出る場合は、以下のファイルを作成(または編集)して、Bluetoothの操作権限を明示的に許可してみてください。 [#w2bf0ece]

- ファイルパス: bluetooth-python.conf (新規作成)
- 内容:
- XML

#code(XML){{
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy user="root">
    <allow send_destination="org.bluez"/>
  </policy>
  <policy user="pi">
    <allow send_destination="org.bluez"/>
  </policy>
  <policy context="default">
    <allow send_destination="org.bluez"/>
  </policy>
</busconfig>
}}
- ※作成後、sudo systemctl restart dbus を実行してください。

** 3. 最終手段の確認:Bluetoothの状態 [#jbb827d0]
hciconfig で UP になっていても、BlueZが管理を放棄している場合があります。以下のコマンドを打ってからプログラムを実行してみてください。

Bash

 # 一旦リセットしてBlueZに再認識させる
 sudo bluetoothctl power off
 sudo bluetoothctl power on
 sudo bluetoothctl scan on  # 数秒動かして Ctrl+C

その上で、再度実行します:

 Bash

sudo ./testpip/bin/python3 peripheral2.py
これで get_first で失敗しても、後半の「手動取得」の方で hci0 を掴めるはずです!

  $ sudo bluetoothctl power on
 Failed to set power on: org.bluez.Error.Failed
 このエラーはBluetoothのハードウェアブロック、ドライバの競合、サービスエラーで発生します。
 まず、ターミナルで rfkill unblock bluetooth を実行し、その後 sudo systemctl restart bluetooth でサービスを再起動してください。


 sudo rfkill unblock bluetooth
 sudo systemctl restart bluetooth
 sudo hciconfig hci0 down
 sudo hciconfig hci0 up

- # ペアリング・エージェントを無効化(一時的)
 sudo bluetoothctl agent off
- # 念のため信頼関係もリセット
 sudo bluetoothctl untrust B8:27:EB:C7:CE:F9

** Raspberry PiでBLE On, Wifi Off にする方法 [#c2ebdb61]
-  https://nosubject.io/raspberry-pi-os-how-do-i-turn-off-wifi-bluetooth/

** xxx [#a2866261]
 sudo bluetoothctl remove <相手のMACアドレス>
 sudo bluetoothctl remove B8:27:EB:C7:CE:F9
 sudo bluetoothctl remove E4:5F:01:B1:D7:F2
 sudo bluetoothctl remove B8:27:EB:ED:CD:2A
 sudo bluetoothctl remove E4:5F:01:B1:D7:78

** 3. 【最重要】Bluetooth デーモンの完全リセット [#b6414f99]

- これまでの「ゴミ」が OS のメモリに残っている可能性があります。以下の手順を両方の Pi で順番に実行してください。
+ プログラムを止める (Ctrl+C)
+ キャッシュフォルダを消去する(これが一番効きます)
 Bash
 sudo rm -rf /var/lib/bluetooth/*
 sudo systemctl restart bluetooth
+  Peripheral 側を起動
+ Central 側を起動
- なぜこれが必要か
-- 先ほどの remove 時のログを見ると、マイク制御やボリューム制御など、今回の目的とは無関係な D-Bus オブジェクトが 50 個近く削除されています。これらが「ゾンビ」のように残って通信を邪魔しているため、/var/lib/bluetooth/* の削除で一度「脳を真っさらにする」のが最も近道です。

- これで NoReply エラーが消え、Discovering services... の先に進めるようになるはずです。

** xxxxx [#a1cb6aa4]
 sudo hciconfig hci0 up
 sudo bluetoothctl power on
 sudo systemctl stop bluetooth
 sudo killall -9 bluetoothd 2>/dev/null
 sudo systemctl start bluetooth
 sudo bluetoothctl show

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS