PyBlueZ: creating multiple client connections

I currently have a client / server pair encoded with PyBlueZ. Right now, the server can connect to serial clients - it will work until it is completed with the client, after which it will begin to listen to another client.

However, I really want to run client communication in separate threads in order to have multiple clients at the same time. However, when I try to connect to the 2nd client, PyBlueZ advertises the same port that the first client uses. I configure such connections as follows:

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 is great for serial connections, but when I try it in parallel, my client receives a β€œbusy” response from the bluetooth server system. On the client side, I display the port to which it is trying to connect, and it always shows port "1".

Is there a limitation in PyBlueZ that allows only one connection? Or am I doing something wrong for parallel connections?

+4
source share
1 answer

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

  • Advertise the service only once and once (no need to advertise it for each server stream)
  • Allocate a different server channel for each thread. (An RFCOMM manager usually exists in an RFCOMM connection, which allocates a new server channel for each socket. I think that in your case you may need to do this manually.) Change this code

     self.port = bluetooth.PORT_ANY 

    Try channels 1, 2 and so on and see if it works! Then you only need to follow the selected channels.

Please let me know if this worked!

+1
source

Source: https://habr.com/ru/post/1401481/


All Articles