Question

Is it possible with bluez under Linux to connect to multiple classic and low energy devices at the same time? The bluez site isn't very helpful providing information like this.

Was it helpful?

Solution

Yes, I've managed to connect to 7 low energy devices at the same time. The maximum varies depending on the hardware you're using. You can also connect to multiple classic devices as well.

Here's some pseudo/snippet of C I used for connecting via L2CAP:

#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>

char *bdaddr;
int cid = 0;
int psm = 0;
int bdaddr_type = BDADDR_LE_PUBLIC;
int err;
struct sockaddr_l2 addr;
int sock_fd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)

memset(&addr, 0, sizeof(addr));
addr.l2_family = sock->sock_family;
str2ba(bdaddr, &addr.l2_bdaddr);

if (cid)
    addr.l2_cid = htobs(cid);
else
    addr.l2_psm = htobs(psm);

addr.l2_bdaddr_type = bdaddr_type;


err = connect(sock_fd, (struct sockaddr *) &addr, sizeof(addr));

My code is a mix of C and Python so I tried to restructure it so it's just the C parts. Everything was taken from reading the Bluez source code, specifically the gatttool.

UPDATE: There's a bug in the linux kernel's bluez code in versions 3.4 and prior when dealing with L2CAP sockets. Essentially, if you have more than one connection, it will mix them up so you get all your data on the last connection made. So, the code I gave will only work on kernels 3.4 and prior if you only make one L2CAP connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top