基于rfcomm模拟obex协议收发数据
服务端:
#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[])
{
int i,newsock,sock,status,local_chan=9;
char buf[1024];
socklen_t len;
struct sockaddr_rc local_addr={0},addr={0};
sock=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(sock<0)
{
perror("SOCKET");
return -1;
}
if(local_chan>0)
{
local_addr.rc_family=PF_BLUETOOTH;
local_addr.rc_channel=(uint8_t)local_chan;
bacpy(&local_addr.rc_bdaddr, BDADDR_ANY);
status=bind(sock,(struct sockaddr *)&local_addr,sizeof(local_addr));
if(status<0)
{
perror("BIND");
close(sock);
return -1;
}
}
else
{
printf("channel error\n");
close(sock);
return -1;
}
status=listen(sock,4);
if(status<0)
{
perror("LISTEN");
close(sock);
return -1;
}
while(1)
{
printf("Waiting client connect...\n");
newsock=accept(sock,(struct sockaddr *)&addr,&len);
if(newsock<0)
{
perror("ACCEPT");
close(sock);
return -1;
}
printf("connect success\n");
while(1)
{
memset(buf,0,sizeof(buf));
status=read(newsock,buf,sizeof(buf));
if(status<0)
{
perror("READ");
}
for(i=0;i<status;i++)
{
printf(" %x",buf[i]);
}
printf("\n");
if(buf[0]=0x80)
status=write(newsock,buf,sizeof(buf));
printf("%s\n",buf);
}
}
close(sock);
return 0;
}
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#define LOCAL_CID 9
unsigned char OBEX_CONNECT_REQUEST[]={0x80,0x00,0x07,0x10,0x00,0x60,0x00};
unsigned char PUT[]={
0x02,0x00,0x59,0xcb,0x00,0x00,0x00,0x01,
0xc0,0x00,0x00,0x00,0x01,0x01,0x00,0x29,
0x00,0x32,0x00,0x30,0x00,0x31,0x00,0x38,
0x00,0x2d,0x00,0x30,0x00,0x33,0x00,0x2d,
0x00,0x30,0x00,0x34,0x00,0x2e,0x00,0x6c,
0x00,0x6f,0x00,0x67,0x00,0x2e,0x00,0x74,
0x00,0x78,0x00,0x74,0x00,0x00,0x42,0x00,
0x0e,0x74,0x65,0x78,0x74,0x2f,0x70,0x6c,
0x61,0x69,0x6e,0x00,0xc3,0x00,0x00,0x00,
0x0d,0x48,0x00,0x10,0x68,0x65,0x6c,0x6c,
0x6f,0x77,0x20,0x62,0x6f,0x79,0x21,0x0d,
0x0a
};
/*
void fun(unsigned char * put,char *name,unsigned short int len)
{
int num=0;
char buf[64];
memset(buf,0,sizeof(buf));
while(name)
{
if(num/2)
{
buf[num]=name[0];
name=name++;
num++;
}
else{
buf[num]=0x00;
}
}
unsigned short int len;
put[0]=0x02;
put[2]=(unsigened char)len;
put[1]=(unsigened char)(len>>8);
put[3]=0xcd;
put[7]=0x01;
}
*/
int main(int argc,char *argv[])
{
int a,remote_cid;
int newsock,sock,status;
unsigned char buf[1024];
unsigned char put[]={0x82,0x00,0x0b,0xcb,0x00,0x00,0x00,0x01,0x49,0x00,0x03};
char name[32];
socklen_t len;
struct sockaddr_rc local_addr={0},re_addr={0};
if(argc<3)
{
printf("%s usage: <bdaddr> <channel>\n",argv[0]);
return -1;
}
printf("%d\n",sizeof(PUT));
sock=socket(PF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(sock<0)
{
perror("SOCKET");
return -1;
}
local_addr.rc_family=PF_BLUETOOTH;
local_addr.rc_channel=(uint8_t)LOCAL_CID;
bacpy(&local_addr.rc_bdaddr, BDADDR_ANY);
status=bind(sock,(struct sockaddr *)&local_addr,sizeof(local_addr));
if(status<0)
{
perror("BIND");
close(sock);
return -1;
}
remote_cid=atoi(argv[2]);
re_addr.rc_family=PF_BLUETOOTH;
re_addr.rc_channel=(uint8_t)remote_cid;
str2ba(argv[1],&re_addr.rc_bdaddr);
status=connect(sock,(struct sockaddr *)&re_addr,sizeof(re_addr));
if(status<0)
{
perror("connect");
return -1;
}
printf("connect success\n");
while(1)
{
printf("OBEX connect :");
for(a=0;a<sizeof(OBEX_CONNECT_REQUEST);a++)
{
int i=1;
printf("%x ",OBEX_CONNECT_REQUEST[a]);
if(a==i*10)
{
printf("\n");
i++;
}
}
printf("\n");
memset(buf,0,sizeof(buf));
status=write(sock,(char *)&OBEX_CONNECT_REQUEST,sizeof(OBEX_CONNECT_REQUEST));
if(status<0)
{
perror("write");
return -1;
}
printf("write: %d\n",status);
status=read(sock,buf,sizeof(buf));
if(status<0)
{
perror("READ");
return -1;
}
printf("RECIVE %d BYTE: \n",status);
for( a=0;a<status;a++)
{
printf("%x ",buf[a]);
}
printf("\n");
if(buf[0]!=0xa0)
{
printf("connect reject!\n");
return 0;
}
printf("connect success!\n");
printf("send\n"); //start put
status=write(sock,PUT,sizeof(PUT));
if(status<0)
{
perror("send");
return -1;
}
memset(buf,0,sizeof(buf)); //remote information
status=read(sock,buf,sizeof(buf));
for( a=0;a<status;a++)
{
printf("%x ",buf[a]);
}
printf("\n");
printf("end\n");
status=write(sock,put,sizeof(put)); //end
if(status<0)
{
perror("write");
return -1;
}
memset(buf,0,sizeof(buf)); //
status=read(sock,buf,sizeof(buf));
if(status<0)
{
perror("end send");
return -1;
}
if(buf[0]=0xa0)
{
printf("put success\n");
}
for( a=0;a<status;a++)
{
printf("%x ",buf[a]);
}
printf("\n");
memset(buf,0,sizeof(buf));
char disc[]={0x81,0x00,0x08,0xcb,0x00,0x00,0x00,0x01};
status=write(sock,disc,sizeof(disc));
if(status<0)
{
perror("disconnect");
return -1;
}
status=read(sock,buf,sizeof(buf));
if(status<0)
{
perror("read disconnect");
return -1;
}
break;
}
close(sock);
return 0;
}