본문 바로가기

지식/Embeded

큐비트럭 - 블루투스 프로그래밍 - samplescan.c

웹상에서 검색되는 대부분의 블루투스 예제 프로그램은 아래 링크에서 참조된다.


http://people.csail.mit.edu/albert/bluez-intro/


블루투스 스택에 따라 거의 샘플소스를 살펴볼만 하다.

그러나 큐비트럭에서는 샘플소스가 제대로 동작하지 않았다(나의 실수인가...)


samplescan.c 의 예제를 그래도 구현하였으나 주변 블루투스 기기의 갯수는 제대로 스캐닝되나

address 정보와 이름이 나오지 않았다.


그래서 hcitool 의 소스를 참고하여 직접 구현하였다.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <bluetooth/bluetooth.h>
  5. #include <bluetooth/hci.h>
  6. #include <bluetooth/hci_lib.h>
  7. #include <errno.h>
  8.  
  9.  
  10. int main(int argc, char **argv)
  11. {
  12.  
  13.     inquiry_info *info = NULL;
  14.     int dev_id = 0;
  15.     uint8_t lap[3] = { 0x330x8b0x9e };
  16.     int num_rsp, length, flags;
  17.     uint8_t cls[3], features[8];
  18.  
  19.     char addr[18],name[249],oui[9]*comp, *tmp;
  20.  
  21.     struct hci_version version;
  22.     struct hci_dev_info di;
  23.     struct hci_conn_info_req *cr;
  24.  
  25.     int resfresh = 0, extcls= 0, extinf = 0, extout = 0;
  26.     int i,n,l,opt,dd, cc,nc;
  27.  
  28.     length = 8;
  29.     num_rsp = 0;
  30.     flags = 0;
  31.  
  32.     flags = IREQ_CACHE_FLUSH;
  33.  
  34.     dev_id = hci_get_route(NULL);
  35.     if(dev_id < 0)
  36.     {
  37.         perror("Device is not available");
  38.         exit(1);
  39.     }
  40.  
  41.     if( hci_devinfo(dev_id, &di) < 0)
  42.     {
  43.         perror("Can't get device info");
  44.         exit(1);
  45.     }
  46.     info = (inquiry_info*) malloc(sizeof(inquiry_info) * 256);
  47.     memset(info, 0x00sizeof(inquiry_info) * 256 );
  48.  
  49.     printf("Scanning....\n");
  50.    
  51.     //num_rsp = hci_inquiry(dev_id, length, num_rsp, lap,&info, flags);
  52.     num_rsp = hci_inquiry(dev_id, length, num_rsp, NULL ,&info, flags);
  53.    
  54.     if(num_rsp < 0)
  55.     {
  56.         perror("Inquiry failed");
  57.         exit(1);
  58.     }
  59.     else
  60.         printf("%d dev..\n",num_rsp);
  61.  
  62.     dd = hci_open_dev(dev_id);
  63.     if(dd < 0)
  64.     {
  65.         perror("HIC device open failed");
  66.         free(info);
  67.         exit(1);
  68.     }
  69.  
  70.     for(i=0;< num_rsp;i++)
  71.     {
  72.         memset(name,0x00,sizeof(name));
  73.         memset(addr,0x00,sizeof(addr));
  74.  
  75.         ba2str( &(info+i)->bdaddr, addr);
  76.  
  77.         if( hci_read_remote_name_with_clock_offset(dd,
  78.                 &(info+i)->bdaddr,
  79.                 (info+i)->pscan_rep_mode,
  80.                 (info+i)->clock_offset | 0x8000,
  81.                 sizeof(name), name, 100000) < 0)
  82.                     strcpy(name, "unknown");
  83.  
  84.         printf("%s : %s\n",addr,name);
  85.                  
  86.  
  87.     }
  88.    
  89.     return 0;
  90. }
  91.