我正在尝试使用python在我的覆盆子pi上设置蓝牙服务器。我收到错误' BluetoothSocket'未定义,但是,我在bluez repository上跟踪了示例代码。
但是,我已经安装了所需的库。
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")
这是我的版本:
pi@TolotraPi:~ $ sudo apt-get install python-bluez
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-bluez is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 161 not upgraded.
pi@TolotraPi:~ $ sudo apt-get install bluez
Reading package lists... Done
Building dependency tree
Reading state information... Done
bluez is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 161 not upgraded.
pi@TolotraPi:~ $ python --version
Python 2.7.9
我做错了什么?