我正在尝试在我的Android应用程序和Raspberry Pi 3之间建立蓝牙通信链接。我试着在我的树莓派上使用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正常工作,您需要两个包bluetooth
和libbluetooth-dev
。
sudo apt-get install bluetooth libbluetooth-dev