Closed

bluez bluetooth RSSI Api for distance calculation code snippet

Hi,

I am using bluez apis for 1 of my projects. I need to calculate distance of my laptop running ubuntu linux on it from another laptop with ubuntu over bluetooth. I got some code but it is not working properly. Can someone please help me?

pasting my code:

#include <bluetooth/bluetooth.h>

#include <bluetooth/hci.h>

#include <bluetooth/hci_lib.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <sys/ioctl.h>

#include <sys/socket.h>

#include <iostream>

#include <bluetooth/hci.h>

using namespace std;

//#define HCI_MAX_DEV 64

#define BLUE_ERROR 2

#define BLUE_NOT_CONNECTED -1

int findConn(int s, int dev_id, long arg) {

struct hci_conn_list_req *cl;

struct hci_conn_info *ci;

if ((cl = (hci_conn_list_req*)malloc(HCI_MAX_DEV * sizeof(*ci) + sizeof(*cl))) == NULL) {

perror("malloc");

error = true;

return 0;

}

cl->dev_id = dev_id;

cl->conn_num = HCI_MAX_DEV;

ci = cl->conn_info;

if (ioctl(s, HCIGETCONNLIST, (void *)cl)) {

perror("Could not get connection list");

error = true;

free(cl);

return 0;

}

int i;

for (i = 0; i < cl->conn_num; i++, ci++) {

if (!bacmp((bdaddr_t *)arg, &ci->bdaddr)) {

free(cl);

return 1;

}

}

free(cl);

return 0;

}

int rssi(char *address) {

struct hci_conn_info_req *cr;

int8_t rssi;

int dd, dev_id;

bdaddr_t bdaddr;

str2ba(address, &bdaddr);

error = false;

dev_id = hci_for_each_dev(HCI_UP, findConn, (long)&bdaddr);

if (dev_id < 0) {

if (error) {

return BLUE_ERROR;

}

else {

return BLUE_NOT_CONNECTED;

}

}

dd = hci_open_dev(dev_id);

if (dd < 0) {

perror("Could not open HCI device");

return BLUE_ERROR;

}

if ((cr = (hci_conn_info_req*)malloc(sizeof(struct hci_conn_info_req) + sizeof(struct hci_conn_info))) == NULL) {

perror("malloc");

hci_close_dev(dd);

return BLUE_ERROR;

}

bacpy(&cr->bdaddr, &bdaddr);

cr->type = ACL_LINK;

if (ioctl(dd, HCIGETCONNINFO, (unsigned long)cr) < 0) {

perror("Could not get connection info");

free(cr);

hci_close_dev(dd);

return BLUE_ERROR;

}

if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {

perror("Could not read RSSI");

free(cr);

hci_close_dev(dd);

return BLUE_ERROR;

}

free(cr);

hci_close_dev(dd);

return rssi;

}

int main()

{

cout<<"rssi = "<<rssi("80:A5:89:3D:85:3A")<<endl;

cout<<"rssi = "<<rssi("E4:02:9B:84:A4:76")<<endl;

}

when I run this code I get

rssi = -1

rssi = -1

where BLUE_NOT_CONNECTED = -1

Skills: Bluetooth Low Energy (BLE), C Programming, C++ Programming, Linux

See more: distance calculation gps code, maps api distance calculation, time distance calculation api, distance calculation api, driving distance calculation api, address distance calculation api

About the Employer:
( 0 reviews ) bhubaneswar, India

Project ID: #14857322

3 freelancers are bidding on average ₹1267 for this job

parthitce

RSSI is not the value you want to calculate the distance between two device/controller. We need to get the value from device's RSSI using DBUS interface provided by the device.txt. Relevant Skills and Experience I hav More

₹1250 INR in 3 days
(4 Reviews)
2.9
Albertaks90

A proposal has not yet been provided

₹1050 INR in 2 days
(0 Reviews)
0.0
jiandeng

I believe you'll have to connect to the BLE device if you want query RSSI with your code snippet. Relevant Skills and Experience I have an script (not in C++) which can query RSSI without connecting to the device. Pr More

₹1500 INR in 3 days
(0 Reviews)
0.0