Listening for connections on port: 0
Traceback (most recent call last):
File "server.py", line 29, in <module>
runServer()
File "server.py", line 19, in runServer
profiles = [ bluetooth.SERIAL_PORT_PROFILE ]
File "/usr/lib/python2.7/dist-packages/bluetooth/bluez.py", line 176, in advertise_service
raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
这是我在树莓派 3 模型 B 上运行以下脚本时遇到的错误。
import bluetooth
name="bt_server"
target_name="siggen"
uuid="222705e1-e1e6-47f5-aa92-1c5ae849f3f1"
def runServer():
serverSocket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port=bluetooth.PORT_ANY
serverSocket.bind(("",port))
print "Listening for connections on port: ", port
serverSocket.listen(1)
port=serverSocket.getsockname()[1]
#the missing piece
bluetooth.advertise_service( serverSocket, "SampleServer",
service_id = uuid,
service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ],
profiles = [ bluetooth.SERIAL_PORT_PROFILE ]
)
inputSocket, address=serverSocket.accept()
print "Got connection with" , address
data=inputSocket.recv("1024")
print "received [%s] n " % data
inputSocket.close()
serverSocket.close()
runServer()
我有 bluez 版本 5.37。我点击了这些链接(链接1)和(链接2)。他们似乎没有解决问题。
我可以使用以下终端命令将我的安卓连接到树莓派
SDPtool Add SP
它没有给出任何回应。
sudo rfcomm listen hci0&
设备已连接(使用蓝色术语应用程序)
cat/dev/rfcomm0
此端口用于接收数据
此外,以下脚本有效
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()
因此,蓝牙是完全正常工作的。我唯一的问题是个人资料。我无法弄清楚问题是什么。感谢任何形式的帮助。
提前感谢您的帮助!
你需要安装 libbluetooth-dev,就像下面这样:
sudo apt-get install libbluetooth-dev