2 Replies Latest reply on Aug 7, 2017 12:58 PM by Intel Corporation

    get rssi distance in bluetooth uing bluez apis

    mohapatrasandeep60

      my code is

       

      #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

       

       

      bool error;

       

      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;

      }

       

       

       

      In output I am getting rssi = -1(BLUE_NOT_CONNECTED),. Any suggestions , what I should do?

       

      I use assus laptop with intel processor and ubuntu linux

      I want to measure distance between 2 bluetooth devices (approx) and if distance i greater than a threshold then activate an alarm,

        • 1. Re: get rssi distance in bluetooth uing bluez apis
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          : Thank you very much for joining the Intel® NUC communities. We will try our best in order to assist you with this problem.
           
          According to the information you gave us:
          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 the variable is always: error = false, then for you to get that message is expected. Also, on the following function: hci_for_each_dev(HCI_UP, findConn, (long)&bdaddr); we cannot confirm the value of it to locate the error.
           
          Now, in order to better assist you:
          Could you please describe what is it that you are trying to do with the NUC?
          What is the model of the NUC?
          Which operating system are you using?
           
          Any further questions, please let me know.
           
          Regards,
          Alberto R
           

          • 2. Re: get rssi distance in bluetooth uing bluez apis
            Intel Corporation
            This message was posted on behalf of Intel Corporation

            : I just wanted to check if the information posted previously was useful for you and if you need further assistance on this matter?
             
            Any questions, please let me know.
             
            Regards,
            Alberto R