This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to send L2CAP Packet to BLE Device

Hello I have this code that sends some L2CAP packet to bluetooth device. I want to do the same for BLE device. Could someone please tell what I have to change on that code in order to make it possible for BLE Device?

int main(int argc, char **argv)
{
	char *buffer;
	l2cap_cmd_hdr *cmd;	
	struct sockaddr_l2 addr;
	int sock, sent, i;

	if(argc < 2)
	{
		fprintf(stderr, "%s <btaddr>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	if ((sock = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP)) < 0) 
	{
		perror("socket");
		exit(EXIT_FAILURE);
	}

	memset(&addr, 0, sizeof(addr));
        //bind to local address
	addr.l2_family = AF_BLUETOOTH; 

	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) 
	{
		perror("bind");
		exit(EXIT_FAILURE);
	}

	str2ba(argv[1], &addr.l2_bdaddr);
	//connect to remote
	if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) 
	{
		perror("connect");
		exit(EXIT_FAILURE);
	}
	
	if(!(buffer = (char *) malloc ((int) SIZE + 1))) 
	{
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	
	memset(buffer, 'A', SIZE);

	cmd = (l2cap_cmd_hdr *) buffer;
	cmd->code = L2CAP_ECHO_REQ;
	cmd->ident = 1;
	cmd->len = FAKE_SIZE;
	
	if( (sent=send(sock, buffer, SIZE, 0)) >= 0)
	{
		printf("L2CAP packet sent (%d)\n", sent);
	}

	printf("Buffer:\t");
	for(i=0; i<sent; i++)
		printf("%.2X ", (unsigned char) buffer[i]);
	printf("\n");

	free(buffer);
	close(sock);
	return EXIT_SUCCESS;
}

Thank you

Parents Reply Children
  • Hi NadSo!

    So your question is really how you can RECEIVE the packet on the device side???

    Because, how you can send it depends on the platform you are on (Ubuntu) and what device you use to send it with, which is obviously not Nordic! Or?

    As MartinBL correctly states, there are no references to a Nordic chip or SDK in your code.

    The relevant wuestion would be for you to ask how it can be received on the device side, if I get this right.

Related