AnilJ
AnilJ

Reputation: 2121

BlueZ Bluetooth API and distance calibration precision

I am using BlueZ C API to program my bluetooth mouse to read the distance. I have set up a bluetooth dongle. At the moment, I have to move the mouse at least 5-10 feet from the laptop (bluetooth dongle) to get some reading of RSSI. Below this distance, I get most of the readings as 0.

Is there any way to use this API to get more precise value of RSSI so that we can track the distance in this range?

int8_t Bluetooth::read_rssi(int to) {

    int dd = hciSocket;
    struct hci_conn_info_req *cr;
    bdaddr_t bdaddr;
    int8_t rssi;

    str2ba(bt_addr, &bdaddr);

    if (dd < 0) {
            perror("HCI device open failed");
            exit(1);
    }   

    cr = (hci_conn_info_req *)(malloc(sizeof(*cr) + sizeof(struct hci_conn_info)));
    if (!cr) {
            perror("Can't allocate memory");
            exit(1);
    }   

    bacpy(&cr->bdaddr, &bdaddr);
    cr->type = ACL_LINK;
    if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
            perror("Get connection info failed");
            exit(1);
    }   

    if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
            perror("Read RSSI failed");
            exit(1);
    }   

    return rssi;
}

Upvotes: 1

Views: 4068

Answers (1)

kaylum
kaylum

Reputation: 14046

hci_read_rssi is probably not what you want. It's not the actual remote RSSI. From the BT spec section describing the HCI_Read_RSSI command:

The RSSI parameter returns the difference between the measured Received Signal Strength Indication (RSSI) and the limits of the Golden Receive Power Range for a Connection Handle to another BR/EDR Controller. Any positive RSSI value returned by the Controller indicates how many dB the RSSI is above the upper limit, any negative value indicates how many dB the RSSI is below the lower limit. The value zero indicates that the RSSI is inside the Golden Receive Power Range.

I believe the value you want is the one contained in the inquiry/scan. I know of a way to get that but not sure whether it's acceptable to you or whether it is the best way.

The bluez dbus device API has RSSI as one of the properties. The api doc can be found here.

UPDATE: I haven't tried it myself but looks like pybluez supports getting the inquiry RSSI. See this pybluez example.

Here's a simple example for bluez4:

https://bitbucket.org/kaylum/bluez-rssi-example/src

Upvotes: 1

Related Questions