我正在尝试在我的 PC 和诺基亚 E63 之间建立一个 RFCOMM 客户端-服务器通信系统。我正在使用 Python 和 PyBluez 来设置服务器并使用 PyS60 在我的手机上对客户端进行编程。
但是 PyBluez 不接受连接。
这里是服务器的代码:
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"
这是客户端的代码
#!/usr/bin/env python
import socket
class BTReader:
def connect(self):
self.sock=socket.socket(socket.AF_BT, socket.SOCK_STREAM)
addr, services=socket.bt_discover()
print "Discovered: %s, %s" % (addr, services)
port = services[services.keys()[0]]
print port
address=(addr,port)
print "Connecting to", str(address), "..."
self.sock.connect(address)
print "OK."
data = "Hello, World!"
print "Sending data:", data
self.sock.send("Hello World!");
print "SENT."
def close(self):
self.sock.close()
def main():
bt=BTReader()
bt.connect()
bt.close()
if __name__ == '__main__':
main()
当我运行这两个程序时,我得到了这个输出
1)服务器(PC):等待RFCOMM通道1上的连接[并且它停留在那里。它不承认连接。我已经使用 WINPDB 进行了检查,即使手机显示已建立连接,服务器脚本仍保留在 server_sock.accept() 循环中。]
2) 客户(电话):
Discoverd: xx:xx:xx:xx:xx:xx {u'SampleServer':1}
1
Connecting to ('xx:xx:xx:xx:xx:xx', 1) ...
OK.
Sending data: Hello World!
Traceback (most recent call last):
...
...
File "c;\resource\socket.py" , line 409, in send return self._sock.send(data, f, cb)
error: (0, 'Error')
是什么导致了问题??