int hci_get_route(bdaddr_t *bdaddr)
: bdaddr의 값으로 NULL값을 주면 사용가능한 Local의 첫 번째 Adapter의 Index값을 반환해 준다.
: local에 2개 이상의 Adapter가 존재하며 해당 bdaddr의 값을 알고 있을 경우 다음과 같은 함수를 사용하여 ID값을 반환 받을 수 있다.
ex) int dev_id = hci_devid("01:23:45:67:89:AB")
typedef struct {
uint8_t b[ 6 ];
} --attribute--( (packed) ) bdaddr_t;
int hci_devid ( const char *addr ) ;
int hci_open_dev(int dev_id)
: socket을 생성하고 성공시 device descriptor를 리턴하게 된다. Error일 경우 -1을 리턴하게 된다.
int hci_inquiry(int dev_id, int len, int max_rsp, const uint8_t *lap, inquiry_info **ii, long flags)
#2 len : inquiry 동작 시간을 설정하는 것이며 약 1.28s * len 이 된다.
#6 flags : IREQ_CACHE_FLUSH(1)의 값이 들어가면 기존의 cache에 있는 값을 flush한 다음 다시 inquiry 하게
되며 0의 값을 가지게 되면 Remote Device가 범위내에 없더라도 cache에 그 정보가 있다면 출력하게 된다.
typedef struct {
bdaddr_t bdaddr;
uint8_t pscan_rep_mode;
uint8_t pscan_period_mode;
uint8_t pscan_mode;
uint8_t dev_class[3];
uint16_t clock_offset;
} --attribute-- ( (packed) ) inquiry_info;
int hci_read_remote(int dd, const bdaddr_t *bdaddr, int len, char *name, int timout)
마지막 Paramter인 timeout의 단위는 ms 이다.
* Compile
arm-none-linux-gnueabi-gcc -o simplescan simplescan.c --static -lbluetooth
* simplescan.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h>
#include <sys/socket.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h>
int main(int argc, char **argv) { inquiry_info *ii = NULL; 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); if (dev_id < 0) { fprintf(stderr, "error code %d: %s\n", errno, strerror(errno)); exit(1); }
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));
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); if( num_rsp < 0 ) perror("hci_inquiry");
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); }
free( ii ); close( sock ); return 0; }
|
춮처 : Bluetooth Essentials