I’m trying to setup a RFCOMM client-server communication system between my PC and my Nokia E63. I’m using Python with PyBluez for setting up the server and using PyS60 to program the client on my phone.
However PyBluez is not accepting the connection.
Here the code for the server:
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"
And here is the code for the client
#!/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()
When I run both the programs I get this output
1) Server (PC):
Waiting for connection on RFCOMM channel 1 [And it stays there. It doesn’t acknowledge the connection. I’ve checked using WINPDB, even after the phone shows that it has established a connection, the server script remains in the server_sock.accept() loop.]
2) Client (phone):
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')
What is causing the problem ??
You should re-read the Rfcomm-client.py example of the PyBluez library.
I don’t know why you are using some random socket instead of the BluetoothSocket object…
However I suspect that is the error. To detail for further readers:
If you look at the client code, he was importing the socket module and using that – that has NOTHING to do with pybluez and is wrong.
As a hint, try to make the 2 examples in the PyBluez library work (client and server) and then make your application.