Friday, 29 August 2014

*the server_bluetooth program
 *gcc -o server_blue server_blue.c -lbluetooth


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<bluetooth/bluetooth.h>
#include<bluetooth/rfcomm.h>
#include<bluetooth/hci.h>
#include<bluetooth/hci_lib.h>


int main(int argc,char **argv)
{
  struct sockaddr_rc loc_addr={0};
  struct sockaddr_rc rem_addr={0};
 
  int server_sock,client,bytes_red;
  unsigned int opt=sizeof(rem_addr);
  char buf[248]={0};
  char name[500]={0};
 
  server_sock=socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
 
  loc_addr.rc_family=AF_BLUETOOTH;
  loc_addr.rc_bdaddr= *BDADDR_ANY;
  loc_addr.rc_channel=1;
 
  bind(server_sock,(struct sockaddr *)&loc_addr ,sizeof(loc_addr));
   ba2str(&loc_addr.rc_bdaddr,buf);
  printf("servessr name: %s \n",buf);
  if(0!=hci_read_remote_name(server_sock,&loc_addr.rc_bdaddr,sizeof(name),name,0))
     {
      strcpy(name,"[unknown]");
      printf ( " details: %s %s\n" , buf , name ) ;
    }
      else
    printf ( " details: %s %s\n" , buf , name ) ;
 
  listen(server_sock,1);
 
 
 
  client=accept(server_sock,(struct sockaddr *)&rem_addr,&opt);
  ba2str(&rem_addr.rc_bdaddr,buf);
  printf("client name: %s \n",buf);
  bytes_red=recv(client,buf,sizeof(buf),0);
  if(bytes_red>0)
    printf("bytes send : %d \n",bytes_red);
   
  close(client);
  close(server_sock);
  return 0;
 
 
}
1.first scan from previous program get the baddr
2.replace it and  also replace the file name and get connected
3.send data
4.yahoooooooooooo

*gcc -o client_blue client_blue.c -lbluetooth


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<bluetooth/bluetooth.h>
#include<bluetooth/rfcomm.h>
#include<bluetooth/hci.h>
#include<bluetooth/hci_lib.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//sssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);


int main (int argc , char * * argv )
{
struct sockaddr_rc addr={0} ;

int s,status;
ssize_t bytes_send ;
char dest[18] = "D4:5D:42:91:EE:0A" ;
int fd=open("/home/sourav/Desktop/images_7.jpeg",O_RDONLY);
if(fd<0)
perror ("open:");

size_t count=1528    ;

s = socket (AF_BLUETOOTH, SOCK_STREAM,BTPROTO_RFCOMM) ;
addr.rc_family=AF_BLUETOOTH;
addr.rc_channel=1;
str2ba(dest,&addr.rc_bdaddr) ;
status = connect (s,( struct sockaddr * )&addr,sizeof (addr)) ;
if(status<0)
 perror ("connect");
else
  printf("connected \n");

bytes_send=sendfile(s,fd,NULL,count);
if(bytes_send<0)
  perror("sendfile:");
else
printf("bytes send : %d \n",bytes_send);

close(fd);
close(s) ;
return 0 ;
}

amazing bluetooth socket programming ...

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;
}