int main(int argc, char*argv[])
{
int fd;
int nwrite;
tcs_dt_init(argc,argv);
while(1)
{
memset(buf,0,1024);
printf("input:\n");
fgets(buf,1024,stdin);
buf[strlen(buf)]='\n';
nwrite = write(s,buf,strlen(buf));
printf("%d bytes written\n",nwrite);
}
刚刚接触嵌入式系统,学习蓝牙传输的时候遇到这样一段程序,目的是两台设备蓝牙连接传输数据。想请教各位前辈用于两台设备间传输或者发送数组的函数是哪一个啊?主函数就这么短而且实验后确实成功了,能通过蓝牙传输任何我输入的数据,难道是在头文件里吗?
附相关函数与头文件:
#ifdef HAVE_CONFIG_H*
#include
#endif
#define DEBUG
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* File control definitions */
#include
/* Error number definitions */
#include
/* POSIX terminal control definitions */
#include
/* Mluti-programming definitions*/
#include "bluetooth/bluetooth.h"
#include "bluetooth/hci.h"
#include "bluetooth/hci_lib.h"
#include "bluetooth/l2cap.h"
#include "bluetooth/sdp.h"
#include "bluetooth/sdp_lib.h"
#ifdef DEBUG
void debug(char * str)
{
printf(str);
}
#else
#define debug(str)
#endif
#define MAX_LEN 32
/*****************GLOBAL VARIABLE***********************************/
struct sockaddr_l2 addr = {0};
//for l2cap socket
int s;
int status;
char *dest;
int bytes_read;
unsigned char buf[1024];
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;
}
void tcs_dt_init(int argc,char*argv[])
{
char c;
int nwrite;
if(argc == 2)
{
dest = argv[1];
}
else
{
printf("usage::clinet addr\n");
exit(1);
}
//unsigned char* addr;
debug("\n||========== get tcs_dt initialized =============||\n");
debug("(1) dt opening l2cap socket...\n");
//allocate socket
printf("s = %d\n",s);
s = socket(PF_BLUETOOTH,SOCK_SEQPACKET,BTPROTO_L2CAP);
printf("s = %d\n",s);
if(s < 0)
{
printf("create socket in dt failed...\n");
exit(1);
}
else
{
debug(" dt open socket success!\n");
}
//bind socket to port 0x1001 of the first available bluetooth adaptor
addr.l2_family = AF_BLUETOOTH;
addr.l2_psm = htobs(0x1001);
str2ba(dest,&addr.l2_bdaddr);
debug("(2) connecting to gateway...\n");
printf("status = %d \n",status);
status = connect(s,(struct sockaddr*)&addr, sizeof(addr));
printf("status = %d \n",status);
if(status < 0)
{
printf("connect to gateway failed...\n");
exit(1);
}
else
{
debug("connect success!\n");
}
c= 0x41;
//debug("||==========initialize tcs_dt end ================||\n\n");
sleep(1);
}