4

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 ??

James Henstridge
  • 42,244
  • 6
  • 132
  • 114
Utsav
  • 536
  • 2
  • 6
  • 18

3 Answers3

2

I know its a year later, but I have another solution. On certain versions of Debian (Raspbian etc) and maybe some others distros. The server_sock.accept() will by default just hang and never accept a connection - even from a paired device! However, a solution to this is really simple. If you go to /etc/bluetooth/main.conf there is a line which is called Disable Plugins. Add a line or change so it looks like this:-

 DisablePlugins = pnat

Then restart the Bluetooth service:-

 sudo invoke–rc.d bluetooth restart

It now MAY have been fixed.

Good luck!

Reference: RFCOMM without pairing using PyBluez on Debian?

Community
  • 1
  • 1
user1840255
  • 287
  • 1
  • 6
  • 15
  • Hi, the space between invoke and -rc.d should not be there, it didn't work when I used it. I followed the link you included however, and used the one the OP posted sans the space. It worked. Thanks! – RoboCop87 Feb 26 '14 at 19:55
  • 1
    Excellent, this saved me a few hours! Worked for Raspberry Pi with Asus USB-BT400 bluetooth dongle – Trent Seed Apr 06 '14 at 00:32
0

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.

Radu
  • 2,076
  • 2
  • 20
  • 40
0

Sometimes the server pops a window and asks the user to approve the connection. If you run without an X-server then you don't have the chance to see the dialog window and the connection is refused..!! (have spent half a day in the past on this...!!!)

thanasio2
  • 151
  • 2
  • 6
  • I had hard coded the MAC address of the Bluetooth adapter in my code. I decided not to use the service discovery mechanism. I didn't try to solve the problem actually, I needed a quick workaround for it. – Utsav Jun 25 '13 at 08:28