Linux bluez l2cap编程

服务端:

#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>
#include <bluetooth/l2cap.h>


int main()
{
    int                 rv,sock,client;
    char                dest[19]="B8:27:EB:04:3C:E8";
    char                buf[1024];
    socklen_t           len;
    struct sockaddr_l2  addr,rem_addr;
   

    sock=socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    if(sock<0)
    {
    	    perror("Socket");
            exit(-1);        
    }	    
    
    memset((void *)&addr,0,sizeof(addr));
    memset((void *)&rem_addr,0,sizeof(rem_addr));

    addr.l2_family=PF_BLUETOOTH;
    addr.l2_psm=htobs(0x1001);
    addr.l2_bdaddr=*BDADDR_ANY;
    rv=bind(sock,(struct sockaddr *)&addr,sizeof(addr));
    if(rv<0)
    {
        perror("BIND");
	exit -1;
    }

    listen(sock,10);
    while(1)
    {
        client=accept(sock,(struct sockaddr *)&rem_addr,&len);
        if(client<0)
        {
            perror("ACCEPT");
	    exit(-1);
        }
        memset(buf,0,sizeof(buf));

        rv=write(client,dest,sizeof(dest));
        if(rv<0)
	{
	    perror("WRITE");
	    exit(-2);
	}
    }
    return 0;
}

客户端

#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>
#include <bluetooth/l2cap.h>

#define BDADDR  "EE:CA:46:65:82:2F"

int  main(int argv,char *argc[])
{
    int                 i,rv;
    int                 sock,newsock;
    char *              dest;
    char                src[18]="B8:27:EB:04:3C:E8";
    char                bufc[256],buf[256];
    struct sockaddr_l2  local_l2_addr;
    struct sockaddr_l2  remote_l2_addr;
   
    if(argv<2)
    {
        printf("usge:%s <bdaddr>\n",argc[0]);
	return -1;
    }

    sock=socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
    if(sock<0)
    {
        perror("socket");
	return -1;
    }
    printf("Socket success!\n");
/*    
    memset(&local_l2_addr, 0 , sizeof(struct sockaddr_l2));
    local_l2_addr.l2_family = PF_BLUETOOTH;
    bacpy(&local_l2_addr.l2_bdaddr , BDADDR_ANY);
    rv=bind(sock,(struct sockaddr *)&local_l2_addr,sizeof(local_l2_addr));
    if(rv<0)
    {
        perror("Bind");
	return -2;
    }
    printf("Bind success!\n");
*/

    dest=argc[1];
    printf("dest %s\n",dest);
    memset(&remote_l2_addr, 0 , sizeof(struct sockaddr_l2));
    remote_l2_addr.l2_family = PF_BLUETOOTH;
    str2ba(dest, &remote_l2_addr.l2_bdaddr);
    remote_l2_addr.l2_psm=0x00017;
    rv=connect(sock, (struct sockaddr*)&remote_l2_addr, sizeof(struct sockaddr_l2));
    if(rv<0)
    {
        perror("connect");
	return -1;
    }
    
    printf("connect success\n");



    while(1)
    {
	memset(buf,0,sizeof(buf));
        rv=read(sock,buf,sizeof(buf));
	if(rv<0)
	{
	    perror("Read");
	    exit(-1);
	}
	printf("Recive %d bit :",rv);
	for(i=0;i<rv;i++)
	{
	    printf("%2x ",buf[i]);
	}
	printf("\n");
	if(buf[0]==0x0a)
	{
	    memset(bufc,0,sizeof(bufc));
	    bufc[0]=0x0b;
	    bufc[1]=buf[1];  //cid  要与服务端一至
	    bufc[2]=0x08;
	    bufc[3]=0x00;
	    bufc[4]=0x02;
            bufc[5]=0x00;
	    bufc[6]=0x00;
	    bufc[7]=0x00;
	    bufc[8]=0x00;
	    bufc[9]=0x00;
	    bufc[10]=0x00;
	    bufc[11]=0x00;
	
	    rv=write(sock,bufc,sizeof(buf));
	    if(rv<0)
	    {
	        perror("WRITE");
	    }
	    else
	    {
	        printf("qwe\n");
	    }
	}
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值