C/C++  

Example

: Scanning bluetooth devices


Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. #include <bluetooth/bluetooth.h>
  6. #include <bluetooth/hci.h>
  7. #include <bluetooth/hci_lib.h>
  8. int main(int argc, char **argv)
  9. {
  10. inquiry_info *ii = NULL;
  11. int max_rsp, num_rsp;
  12. int dev_id, sock, len, flags;
  13. int i;
  14. char addr[19] = { 0 };
  15. char name[248] = { 0 };
  16. dev_id = hci_get_route(NULL);
  17. sock = hci_open_dev( dev_id );
  18. if (dev_id < 0 || sock < 0) {
  19. perror("opening socket");
  20. exit(1);
  21. }
  22. len = 8;
  23. max_rsp = 255;
  24. flags = IREQ_CACHE_FLUSH;
  25. ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
  26. num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
  27. if( num_rsp < 0 ) perror("hci_inquiry");
  28. for (i = 0; i < num_rsp; i++) {
  29. ba2str(&(ii+i)->bdaddr, addr);
  30. memset(name, 0, sizeof(name));
  31. if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),
  32. name, 0) < 0)
  33. strcpy(name, "[unknown]");
  34. printf("%s %s\n", addr, name);
  35. }
  36. free( ii );
  37. close( sock );
  38. return 0;
  39. }


Returns
        00:17:E5:F8:2B:A0       Tarantula
        00:1F:5D:BF:F0:01       Cucaracha
        00:1E:3B:15:74:16       Libelula
        00:25:47:33:97:05       Mosquito
        00:24:90:4B:0B:71       Mantis
        


To compile:

gcc simplescanBT.c -o simplescanBT -lbluetooth


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use