gpt4 book ai didi

android - 无法运行 python-bluez RFCOMM 服务器示例脚本

转载 作者:行者123 更新时间:2023-12-03 11:55:21 24 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序和 Raspberry Pi 3 之间建立蓝牙通信链接。我尝试在我的 Raspberry Pi 上使用 pybluez。我的问题是,当我尝试运行 pybluez 附带的示例代码(见下文)时,我收到以下错误消息:

Traceback (most recent call last):
File "/usr/share/doc/python-bluez/examples/simple/rfcomm-server.py", line 20, in <module>
profiles = [ SERIAL_PORT_PROFILE ],
File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service
raise BluetoothError (str (e))
BluetoothError: (2, 'No such file or directory')

这是我正在尝试运行的脚本:问题似乎出在 advertise_service 方法中......

# file: rfcomm-server.py
# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a server application that uses RFCOMM sockets
#
# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $


from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"

advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)

print "Waiting for connection on RFCOMM channel %d" % port

client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
except IOError:
pass

print "disconnected"

client_sock.close()
server_sock.close()
print "all done"

有趣的是,以下脚本可以完美运行,但我无法从 Android 连接,因为我无法为 Android 中的套接字设置端口等。

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print("Accepted connection from ",address)

data = client_sock.recv(1024)
print("received [%s]" % data)

client_sock.close()
server_sock.close()

我收到官方示例脚本错误消息的原因可能是什么???我已经按照此处描述的步骤进行操作:https://stackoverflow.com/a/14827036/6149322

感谢您的帮助!

最佳答案

这个问题可能很老,但也许这对某些人有帮助:我认为您根本没有安装所需的一切。要使 pybluez 正常工作,您需要 bluetoothlibbluetooth-dev 这两个包。

sudo apt-get install bluetooth libbluetooth-dev

关于android - 无法运行 python-bluez RFCOMM 服务器示例脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36375064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com