Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyBlueZ: Create multiple client connections

I currently have a client/server pair coded against PyBlueZ. Right now the server can connect to sequential clients - it will work until its completed with a client, then it will begin listening for another client.

However, what I really want is to run client communication in separate threads so I have multiple clients at the same time. When I try a 2nd client connection, however, PyBlueZ advertises the same port that the first client is currently using. I am setting up connections like this:

self.port = bluetooth.PORT_ANY
print "Accepting clients..."
self.server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
self.server_sock.bind(("",self.port))
self.server_sock.listen(5)
print "listening on port %d" % self.port

bluetooth.advertise_service( self.server_sock, MY_SERVICE, MY_UUID )

client_sock,address = self.server_sock.accept()
print "Accepted connection from ",address

commThread = ServerThread(client_sock, self.bn_id, self.bn_name, self.bn_thumbnail)

Again, this code works fine for sequential connections, but when I try it in parallel my client gets a "busy" response from the server's bluetooth system. On the client side I output the port its trying to connect to and it always shows port "1".

Is there a limitation in PyBlueZ which only allows for a single connection? Or am I doing something wrong here for parallel connections?

like image 673
Matt Avatar asked Mar 14 '12 21:03

Matt


1 Answers

I think your problem has nothing to do with the Bluetooth client part of the code. You were right to show the Bluetooth server code. What you should try to change:

  • Only advertise a service once, and once only (No need to advertise it for every server thread)
  • Allocate a different server channel for each thread. (On RFCOMM connection, there usually is a RFCOMM manager which allocates a new server channel per each socket. I think in your case you might have to do that manually.) Change this code

    self.port = bluetooth.PORT_ANY
    

    Try channels 1, 2 and so on and see if it works! Then all you have to do is keep track of the allocated channels.

Please let me know if it worked!

like image 119
Radu Avatar answered Nov 13 '22 17:11

Radu