如何将音频文件从树莓派 4 发送到智能手机

问题描述 投票:0回答:1

这是我的代码,我收到错误消息:

Ausnahme: BluetoothError
[Errno 104] Connection reset by peer
  File "<string>", line 3, in sendall
_bluetooth.error: (104, 'Connection reset by peer')

During handling of the above exception, another exception occurred:

  File "/home/eggplant/Dokumente/test.py", line 25, in <module>
    sock.sendall(block)
bluetooth.btcommon.BluetoothError: [Errno 104] Connection reset by peer

执行代码时:

from bluetooth import *

server_mac = '34:29:12:5E:09:10'#20:F4:78:56:EF:7D'
port = 3  # Port für OBEX Object Push Profile

sock = BluetoothSocket(RFCOMM)

try:
    sock.connect((server_mac, port))
    print("Verbunden mit", server_mac)
except BluetoothError as e:
    print("Bluetooth Fehler beim Verbinden:", e)
    sock.close()
    exit(1)

file_path = 'hallo_welt_mp3.wav'
block_size = 1024  # Größe des Datenblocks zum Senden

with open(file_path, "rb") as f:
    while True:
        block = f.read(block_size)
        if not block:
            break  # Ende der Datei erreicht
        try:
            sock.sendall(block)
        except BluetoothError as e:
            print("Bluetooth Fehler beim Senden:", e)
            break

print("Datei erfolgreich gesendet.")
sock.close()

有人发现错误或可以帮助我吗?我真的很感激一些帮助。提前致谢!

python bluetooth raspberry-pi4 bluez
1个回答
0
投票

错误

"connection reset by peer"
通常在远程设备(在本例中为智能手机)意外关闭连接时发生。导致此错误的原因有多种,例如超时、蓝牙配置文件不兼容或蓝牙连接本身存在问题。你可以尝试:

  • 验证智能手机的MAC地址是否正确。
  • 检查智能手机是否支持 OBEX 对象推送配置文件 (OPP) 以及是否已启用。该配置文件用于通过蓝牙传输文件。
  • 尝试在 Raspberry Pi 上使用不同的蓝牙
    library
    module
    。有可用的替代库,例如
    PyBluez
    Bluez
    ,它们可能提供更好的兼容性。
© www.soinside.com 2019 - 2024. All rights reserved.