this program can search near by bluetooth devices...
i have checked it with my nokia 5230 s60. its cool get your phone bdaddr.
use it in client program..and send data ..
1.pairing must be done beforehand
2.u can also use bluetooth_server to listen for client.
3. sudo apt-get install libbluetooth-dev (install in case you dont have lib)
gcc -c scan scan.c -lbluetooth
scanning bluetooth devices program :-scan.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>
int main(int argc,char **argv)
{
inquiry_info *devices=NULL;
int max_rsp,num_rsp;
int adapter_id,sock,len,flags;
int i;
char addr[19]={0};
char name[248]={0};
adapter_id=hci_get_route(NULL);
sock=hci_open_dev(adapter_id);
if(adapter_id<0 || sock <0){
perror("opening socket");
exit(1);
}
len=8;
max_rsp=255;
flags=IREQ_CACHE_FLUSH;
devices=(inquiry_info *)malloc(max_rsp*sizeof(inquiry_info));
num_rsp=hci_inquiry(adapter_id,len,max_rsp,NULL,&devices,flags);
printf("num_rsp= %d \n",num_rsp);
for(i=0;i<num_rsp;i++)
{
ba2str(&(devices+i)->bdaddr,addr);
memset(name,0,sizeof(name));
if(0!=hci_read_remote_name(sock,&(devices+i)->bdaddr,sizeof(name),name,0))
{
strcpy(name,"[unknown]");
}
else
printf ( "%s %s\n" , addr , name ) ;
}
free(devices);
close(sock) ;
return 0;
}
i have checked it with my nokia 5230 s60. its cool get your phone bdaddr.
use it in client program..and send data ..
1.pairing must be done beforehand
2.u can also use bluetooth_server to listen for client.
3. sudo apt-get install libbluetooth-dev (install in case you dont have lib)
gcc -c scan scan.c -lbluetooth
scanning bluetooth devices program :-scan.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>
int main(int argc,char **argv)
{
inquiry_info *devices=NULL;
int max_rsp,num_rsp;
int adapter_id,sock,len,flags;
int i;
char addr[19]={0};
char name[248]={0};
adapter_id=hci_get_route(NULL);
sock=hci_open_dev(adapter_id);
if(adapter_id<0 || sock <0){
perror("opening socket");
exit(1);
}
len=8;
max_rsp=255;
flags=IREQ_CACHE_FLUSH;
devices=(inquiry_info *)malloc(max_rsp*sizeof(inquiry_info));
num_rsp=hci_inquiry(adapter_id,len,max_rsp,NULL,&devices,flags);
printf("num_rsp= %d \n",num_rsp);
for(i=0;i<num_rsp;i++)
{
ba2str(&(devices+i)->bdaddr,addr);
memset(name,0,sizeof(name));
if(0!=hci_read_remote_name(sock,&(devices+i)->bdaddr,sizeof(name),name,0))
{
strcpy(name,"[unknown]");
}
else
printf ( "%s %s\n" , addr , name ) ;
}
free(devices);
close(sock) ;
return 0;
}
No comments:
Post a Comment