Search code examples
bluetoothbluez

BlueZ remote device presence


Using BlueZ, which

is the official Linux Bluetooth stack

I'd like to know which of the below two methods are better suited for detecting a device's presence in the nearby. To be more exact, I want to periodically scan for a Bluetooth device (not BLE => no advertisement packets are sent).

I found two ways of detecting it:

1.) Using l2ping

# l2ping BTMAC

2.) Using hcitool

# hcitool name BTMAC

Both approaches working.

I'd like to know, which approach would drain more battery of the scanned device?

Looking at solution #1 (l2ping's source):

It uses a standard socket connect call to connect to the remote device, then uses the send command to send data to it:

send(sk, send_buf, L2CAP_CMD_HDR_SIZE + size, 0)

Now, L2CAP_CMD_HDR_SIZE is 4, and default size is 44, so altogether 48 bytes are sent, and received back with L2CAP_ECHO_REQ.

For hcitool I just have found the entrypoint:

int hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name, int to);

My questions:

  • which of these approaches are better (less power-consuming) for the remote device? If there is any difference at all.
  • shall I reduce the l2ping's size? What shall be the minimum?
  • is my assumption correct that hci_read_remote_name also connects to the remote device and sends some kind of request to it for getting back its name?

Solution

  • To answer your questions:-

    which of these approaches are better (less power-consuming) for the remote device? If there is any difference at all.

    l2ping BTMAC is the more suitable command purely because this is what it is meant to do. While "hcitool name BTMAC" is used to get the remote device's name, "l2ping" is used to detect its presence which is what you want to achieve. The difference in power consumption is really minimal, but if there is any then l2ping should be less power consuming.

    shall I reduce the l2ping's size? What shall be the minimum?

    If changing the l2ping size requires modifying the source code then I recommend leaving it the same. By leaving it the same you are using the same command that has been used countless times and the same command that was used to qualify the BlueZ stack. This way there's less chance for error and any change would not result in noticeable performance or power improvements.

    is my assumption correct that hci_read_remote_name also connects to the remote device and sends some kind of request to it for getting back its name?

    Yes your assumption is correct. According the Bluetooth Specification v5.2, Vol 4, Part E, Section 7.1.19 Remote Name Request Command:

    If no connection exists between the local device and the device corresponding to the BD_ADDR, a temporary Link Layer connection will be established to obtain the LMP features and name of the remote device.

    I hope this helps.