在去年完成了Bluez在X86平台下安装后,为了测试安装十分成功,根据一些资料编写了一个简单的蓝牙server—client传递字符串的程 序,有server 端启动后发送一个简单的字符串"hello"给client端,client端如果能够接收到就打印在屏幕上.本来那个时候想将server 端交叉编译后移植到ARM平台的,后来还是没有这样做,就直接在X86平台上进行了通信实验,很简单的就通过了,现在写好了利用两个Bluetooth设 备进行文件传输的程序,在X86平台上已经编译和传输文件通过,但是还是存在一些小的问题,这段时间又在公司来实习培训了,都没有去弄了.下周开始要开始 弄自己的这些程序了,希望能够很快的解决掉自己的毕业设计好全力弄公司的工作.还有朋友ifq说在传输很大的字符串(超过1000)时会出现问题,我还没 有去寻找解决的办法.实际上Bluetooth 的通信其实和普通socket编程中建立tcp连接很相似.下面是测试程序的代码:
客户端代码:
#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";
void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
register unsigned char *d = (unsigned char *) dst;
register const unsigned char *s = (const unsigned char *) src;
register int i;
for (i = 0; i < 6; i++)
d[i] = s[5-i];
}
int str2ba(const char *str, bdaddr_t *ba)
{
uint8_t b[6];
const char *ptr = str;
int i;
for (i = 0; i < 6; i++) {
b[i] = (uint8_t) strtol(ptr, NULL, 16);
if (i != 5 && !(ptr = strchr(ptr, ':')))
ptr = ":00:00:00:00:00";
ptr++;
}
baswap(ba, (bdaddr_t *) b);
return 0;
}
if(argc==2)
{
dest=argv[1];
}
else
{
printf("prarm error/n");
exit(1);
}
// allocatea socket
s=socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(s<0)
{
perror("create socket error");
exit(1);
}
// set the connection parameters (who to connect to )
addr.rc_family=AF_BLUETOOTH;
addr.rc_channel=(uint8_t)1;
str2ba(dest,&addr.rc_bdaddr);
// connect to server
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);
close(s);
return 0;
}
服务器端代码:
#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 loc_addr ={0},rem_addr={0};
char buf[1024] ={0},*addr;
int s,client, bytes_read,result;
int opt = sizeof(rem_addr);
printf("Creating socket.../n");
s=socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(s<0)
{
perror("create socket error");
exit(1);
}
else
{
printf("success!/n");
}
loc_addr.rc_family=AF_BLUETOOTH;
loc_addr.rc_bdaddr=*BDADDR_ANY;
loc_addr.rc_channel=(uint8_t)1;
printf("Binding socket.../n");
result=bind(s,(struct sockaddr *)&loc_addr, sizeof(loc_addr));
if(result<0)
{
perror("bind socket error:");
exit(1);
}
else
{
printf("success!/n");
}
void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
register unsigned char *d = (unsigned char *) dst;
register const unsigned char *s = (const unsigned char *) src;
register int i;
for (i = 0; i < 6; i++)
d[i] = s[5-i];
}
int ba2str(const bdaddr_t *ba, char *str)
{
uint8_t b[6];
baswap((bdaddr_t *) b, ba);
return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
b[0], b[1], b[2], b[3], b[4], b[5]);
}
/*result=ba2str(&loc_addr.rc_bdaddr,addr);
if(result<0)
{
perror("addr convert error");
exit(1);
}
printf("local addr is:%s/n",addr);*/
printf("Listen... ");
result=listen(s,1);
if(result<0)
{
printf("error:%d/n:",result);
perror("listen error:");
exit(1);
}
else
{
printf("requested!/n");
}
printf("Accepting.../n");
client= accept(s,(struct sockaddr *)&rem_addr,&opt);
if(client<0)
{
perror("accept error");
exit(1);
}
else
{
printf("OK!/n");
}
ba2str(&rem_addr.rc_bdaddr,buf);
fprintf(stderr,"accepted connection from %s /n",buf);
memset(buf,0,sizeof(buf));
while(1)
{
bytes_read = read(client,buf,sizeof(buf));
if(bytes_read>0){
printf("received[%s]/n",buf);
exit(1);
}
}
close(client);
close(s);
return 0 ;
}