3 Replies Latest reply on Mar 25, 2016 6:41 PM by HC

    ioctl error: Get connection info failed: No such file or directory during bluetooth scan

    HC

      Hello,

      I am currently working on bluetooth project which require scanning of bluetooth/BLE devices and RSSI calculation for distance measurement. I am using Eclipse IDE to write code in C.

      When I tried to run the following code,  always end up with ioctl error:Get connection info failed: No such file or directory.

       

      Following line in the code always returns -1" if (ioctl(sock, HCIGETCONNINFO, (unsigned long) cr) < 0)" .


      Your inputs will be highly valued.

       

       

      Sample Code:bluetooth.c

      #include <stdio.h>

      #include <stdlib.h>

      #include <unistd.h>

      #include <sys/socket.h>

      #include <bluetooth/bluetooth.h>

      #include <bluetooth/hci.h>

      #include <bluetooth/hci_lib.h>

      #include <sys/ioctl.h>

      #include <bluetooth/rfcomm.h>

       

      void connectusingRF(int *sock,char *addr);

      int main(int argc, char **argv)

      {

          inquiry_info *ii = NULL;

          struct hci_conn_info_req *cr;

          bdaddr_t bdaddr;

          int8_t rssi;

          int max_rsp, num_rsp;

          int dev_id, sock, len, flags;

          int i;

          char addr[19] = { 0 };

          char name[248] = { 0 };

       

          dev_id = hci_get_route(NULL);

          printf("device Id %x \n", dev_id);

          sock = hci_open_dev( dev_id );

          if (dev_id < 0 || sock < 0) {

              perror("opening socket");

              exit(1);

          }

       

          len  = 8;

          max_rsp = 255;

          flags = IREQ_CACHE_FLUSH;

          ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));

          cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));

       

          num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);

          if( num_rsp < 0 ) perror("hci_inquiry");

          printf("we are here %d \n",num_rsp);

          for (i = 0; i < num_rsp; i++) {

              ba2str(&(ii+i)->bdaddr, addr);

              memset(name, 0, sizeof(name));

       

              if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),

                  name, 0) < 0)

              strcpy(name, "[unknown]");

              printf("%s  %s\n", addr, name);

          }

       

              str2ba(addr,&bdaddr);

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

              cr->type = ACL_LINK;

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

                perror("Get connection info failed");

          exit(1);

          }

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

          perror("Read RSSI failed");

          exit(1);

          }

          connectusingRF(&sock,addr);

          printf("RSSI return value: %d\n", rssi);

       

          free( cr );

          close( sock );

          return 0;

      }

      void connectusingRF(int *sock,char *addr)

      {

      char dest[18] = "A8:86:DD:8F:6D:5D";

      struct sockaddr_rc rcaddr = {0};

        int newsock = socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);

       

      rcaddr.rc_family = AF_BLUETOOTH;

      rcaddr.rc_channel = (uint8_t)1;

      str2ba(dest,&rcaddr.rc_bdaddr);

       

      int stat = connect(newsock,(struct sockaddr *)&rcaddr,sizeof(rcaddr));

      // send a message

      if( stat == 0 ) {

      printf("workinf\n");

              stat = write(newsock, "hello!", 6);

        } else {

        printf("problem %d\n",stat);

        }

       

      if(stat < 0) {

      perror("Error in connection");

      }

       

      close(newsock);

      }