Cannot run python-bluez RFCOMM server example script

匆匆过客 提交于 2019-12-23 22:11:48

问题


Im trying to build a bluetooth communication link between my Android App and a Raspberry Pi 3. I try to use pybluez on my Raspberry Pi. My problem is that when I try to run the example-code included with pybluez (see below) i get the following error message:

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')

This is the script I'm trying to run: It seems that the problem is in the advertise_service method...

# 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"

Interestingly, the following script works flawlessly, but I cannot connect from Android because I'm not able to set the port etc. for the socket in 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()

What could be the reason for the error message I get for the official example script??? I have already followed the steps described here: https://stackoverflow.com/a/14827036/6149322

Thanks for your help!


回答1:


This question may be old, but maybe this helps somebody: I think you simply don't have everything you need installed. For pybluez to work correctly you need the two packages bluetooth and libbluetooth-dev.

sudo apt-get install bluetooth libbluetooth-dev


来源:https://stackoverflow.com/questions/36375064/cannot-run-python-bluez-rfcomm-server-example-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!