#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> int main( int argc , char **argv) { struct sockaddr_rc addr={0}; int s,status; char *dest,*buf; //="00:11:67:32:61:62";
if(argc==2) { dest=argv[1]; } else { printf("prarm error/n"); exit(1); } // a l l o c a t e a s oc k e t s=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM); if(s<0) { perror("create socket error"); exit(1); } // s e t the conne c t ion parameter s (who to connect to ) addr.rc_family=AF_BLUETOOTH; addr.rc_channel=(uint8_t)1; str2ba(dest,&addr.rc_bdaddr); // connect to s e r v e r printf("connectting.../n"); status=connect(s,(struct sockaddr *)&addr,sizeof(addr)); // send a message if(status==0){ printf("scuess!/n"); status=write(s,"hello!",6); do{ scanf("%s",buf); status=write(s,buf,strlen(buf)); if(status<0) perror("uh oh"); }while(strcmp(buf,"goodbye")!=0); } else { printf("Failed!/n"); }