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
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サービスのリセット」 を試すのが解決への近道です!
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
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 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