RFCOMM layer communication test program under bluez

Source: Internet
Author: User
Tags socket error


After bluez was installed on the X86 platform last year, in order to test the installation success, a simple Bluetooth server-client string passing program was compiled based on some materials, including
Server
After the client is started, a simple string "hello" is sent to the client. If the client receives the string, it is printed on the screen.
After the end is cross-compiled, it is transplanted to the ARM platform. After that, the communication experiment is directly conducted on the X86 platform, which is very simple. Now I have written the experiment and used two bluetooth settings.
The file transfer program for the slave node has been compiled and transmitted on the X86 platform, but there are still some small problems. During this time, I went to the company for practical training, no. starting next week
I hope that I can quickly solve my graduation project and try my best to get the company's work. another friend, ifq, said that a problem may occur when a large string (more than 1000) is transmitted.
There is a way to find a solution. In fact, Bluetooth communication is actually very similar to establishing a TCP connection in general socket programming. The following is the code of the test program:

Client code:

# 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)
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;
}

Server code:

# 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.