python – PyBluez不接受连接

我正在尝试在我的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')

是什么导致了这个问题?

解决方法:

您应该重新阅读PyBluez库的Rfcomm-client.py示例.
我不知道为什么你使用一些随机套接字而不是BluetoothSocket对象…

但我怀疑这是错误.详细了解更多读者:

如果您查看客户端代码,他正在导入套接字模块并使用它 – 与pybluez没有任何关系并且是错误的.

作为提示,尝试使2 examples in the PyBluez library工作(客户端和服务器),然后制作您的应用程序.

上一篇:c – 为什么malloc / new会捕获callstack?


下一篇:c#-PostSharp将调用堆栈显示为“方面代码”,并使调用堆栈的可用性降低