지식/Embeded
큐비트럭 - 블루투스 프로그래밍 - samplescan.c
yahon
2014. 8. 6. 17:28
웹상에서 검색되는 대부분의 블루투스 예제 프로그램은 아래 링크에서 참조된다.
http://people.csail.mit.edu/albert/bluez-intro/
블루투스 스택에 따라 거의 샘플소스를 살펴볼만 하다.
그러나 큐비트럭에서는 샘플소스가 제대로 동작하지 않았다(나의 실수인가...)
samplescan.c 의 예제를 그래도 구현하였으나 주변 블루투스 기기의 갯수는 제대로 스캐닝되나
address 정보와 이름이 나오지 않았다.
그래서 hcitool 의 소스를 참고하여 직접 구현하였다.
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <bluetooth/bluetooth.h>
- #include <bluetooth/hci.h>
- #include <bluetooth/hci_lib.h>
- #include <errno.h>
- int main(int argc, char **argv)
- {
- inquiry_info *info = NULL;
- int dev_id = 0;
- uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
- int num_rsp, length, flags;
- uint8_t cls[3], features[8];
- char addr[18],name[249],oui[9], *comp, *tmp;
- struct hci_version version;
- struct hci_dev_info di;
- struct hci_conn_info_req *cr;
- int resfresh = 0, extcls= 0, extinf = 0, extout = 0;
- int i,n,l,opt,dd, cc,nc;
- length = 8;
- num_rsp = 0;
- flags = 0;
- flags = IREQ_CACHE_FLUSH;
- dev_id = hci_get_route(NULL);
- if(dev_id < 0)
- {
- perror("Device is not available");
- exit(1);
- }
- if( hci_devinfo(dev_id, &di) < 0)
- {
- perror("Can't get device info");
- exit(1);
- }
- info = (inquiry_info*) malloc(sizeof(inquiry_info) * 256);
- memset(info, 0x00, sizeof(inquiry_info) * 256 );
- printf("Scanning....\n");
- //num_rsp = hci_inquiry(dev_id, length, num_rsp, lap,&info, flags);
- num_rsp = hci_inquiry(dev_id, length, num_rsp, NULL ,&info, flags);
- if(num_rsp < 0)
- {
- perror("Inquiry failed");
- exit(1);
- }
- else
- printf("%d dev..\n",num_rsp);
- dd = hci_open_dev(dev_id);
- if(dd < 0)
- {
- perror("HIC device open failed");
- free(info);
- exit(1);
- }
- for(i=0;i < num_rsp;i++)
- {
- memset(name,0x00,sizeof(name));
- memset(addr,0x00,sizeof(addr));
- ba2str( &(info+i)->bdaddr, addr);
- if( hci_read_remote_name_with_clock_offset(dd,
- &(info+i)->bdaddr,
- (info+i)->pscan_rep_mode,
- (info+i)->clock_offset | 0x8000,
- sizeof(name), name, 100000) < 0)
- strcpy(name, "unknown");
- printf("%s : %s\n",addr,name);
- }
- return 0;
- }