2
votes

I connect to a bluetooth le peripheral with the following command:

  • gatttool -t random -b xx:xx:xx:xx:xx:xx -I
  • and connect

If the given MAC-address exists, I have no problem.

If the given MAC-Address does not exist, the gatttool trys to connect about 40 seconds, till the gatttool response is "Error: connect error: Connection refused (111)".

My question is, how and where can i change the 40 seconds connection timeout of gatttool?

3
Have you solved your problem?abhiarora

3 Answers

3
votes

The connection timeout is defined in the bluetooth module of the Linux kernel (L2CAP_CONN_TIMEOUT) and there is no way to change it in userspace. (There’s another constant, HCI_LE_CONN_TIMEOUT, but I don’t think that’s used for this purpose.) However, I believe that if you close the connection or kill the process, the kernel should send the proper LE Create Connection Cancel command so that you can connect to someone else shortly thereafter.

1
votes

Unfortunately I don't think there is anyway to do this unless you want to change the gatttool code. I had a look previously and can see in the code that gatttool does a blocking socket "connect" call.

0
votes

If you are ready to change the gatttool code then you will need to add a timeout on the connection socket in Bluez.

I had to do the same workaround when I wrote GattLib to avoid to block the retry.

You can see my change in this commit: https://github.com/labapart/gattlib/commit/ee58e4cb64af6c698dea1fabb5b6d5e0fc174883

But the main changes are these lines:

    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
        fprintf(stderr, "l2cap_connect: Failed to setsockopt for receive timeout.\n");
        return -1;
    }

    if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
        fprintf(stderr, "l2cap_connect: Failed to setsockopt for sending timeout.\n");
        return -1;
    }

You might be interested by the GattLib example ble_scan if you want to write your own code.