我正在尝试在我的覆盆子pi上创建一个程序,它接受来自Android设备的蓝牙连接。但构建总是失败。这是我的代码:
from bluetooth import *
def initServer():
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
uuid = "00001101-0000-1000-8000-00805F9B34FB"
advertise_service(server_sock, "Echo Server",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ]
)
return server_sock
def getClientConnection(server_sock):
print "Waiting for connection"
client_sock, client_info = server_sock.accept()
print "accepted connection from ", client_info
return client_sock
def manageConnection(socket):
try:
while True:
data = socket.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
socket.send("Echo from Pi: [%s]\n" % data)
except IOError:
pass
server=initServer()
while True:
client=getClientConnection(server)
manageConnection(client)
client.close()
server.close()
print "terminating..."
这是错误:
Traceback (most recent call last):
File "bluetooth.py", line 1, in <module>
from bluetooth import *
File "/home/pi/Desktop/bluetooth.py", line 33, in <module>
server=initServer()
File "/home/pi/Desktop/bluetooth.py", line 4, in initServer
server_sock=BluetoothSocket( RFCOMM )
NameError: global name 'BluetoothSocket' is not defined
已经安装了python-bluetooth。但是什么可能导致此错误,我该如何解决?
答案 0 :(得分:1)
不要调用你的文件bluetooth.py,它会与你试图导入的模块混淆。
另请查看您的缩进,希望它与您提交的内容不同。