Have you already seen this document Intel® Edison Bluetooth User Guide It has lots of information regarding rfcomm. Take a look at it and let me know if you found it helpful.
Thanks for your reply, I've read the document before, but haven't found any information about how to write C code for connecting to BLE device on Edison. I can use bluetoothctl to do that, but can't connect to BLE device with my code. I think the code is correct, since I can connect to classic bluetooth device with it. It's just refers to rfcomm-client.c of RFCOMM sockets .
I’ve found a Bluetooth book named Bluetooth for programmers that describes the process of establishing a connection. Take a look at it:
Once created, a socket must be connected in order to be of any use. The procedure for doing this is
depends on whether the application is accepting incoming connections (server sockets), or whether it’s
creating outbound connections (client sockets). Client sockets are simpler, and the process only requires
making a single call to the connect function.
int connect( int sock, const struct sockaddr *server_info, socklen_t infolen );
The first parameter, sockfd, should be a socket handle created by the socket function. The second
parameter should point to a struct sockaddr_rc addressing structure filled in with the details of the
server’s address and port number. Remember that you’ll have to cast it into a struct sockaddr * to
avoid compiler errors. Finally, the last parameter should always be sizeof( struct sockaddr_rc)
for RFCOMM sockets. The connect function uses this information to establish a connection to the
specified server and returns once the connection has been established, or an error occured.
Server sockets are a bit more complicated and involve three steps instead of just one. After the server
socket is created, it must be bound to a local Bluetooth adapter and port number with the bind function.
int bind( int sock, const struct sockaddr *info, socklen_t infolen );
sock should be the server socket created by connect. info should point to a struct sockaddr_rc
addressing structure filled in with the local Bluetooth adapter to use, and which port number to use.
addrlen should always be sizeof( struct sockaddr_rc ).
Next, the application takes the bound socket and puts it into listening mode with the listen function.
int listen( int sock, int backlog );
In between the time an incoming Bluetooth connection is accepted by the operating system and the time that the server application actually takes control, the new connection is put into a backlog queue. The backlog parameter specifies how big this queue should be. Usually, a value of 1 or 2 is fine. Once these steps have completed, the server application is ready to accept incoming connections using the accept function.
int accept( int server_sock, struct sockaddr *client_info, socklen_t *infolen );
The accept function waits for an incoming connection and returns a brand new socket. The returned socket represents the newly established connection with a client, and is what the server application should use to communicate with the client. If client_info points to a valid struct sockaddr_rc structure, then it is filled in with the client’s information. Additionally, infolen will be set to sizeof( struct sockaddr_rc). The server application can then make another call to accept and accept more connections, or it can close the server socket when finished.
Let me know if this helps.
Thank you for your reply. I've referred to the rfcomm-client.c of RFCOMM sockets to write a bluetooth application. I can connect to the classic bluetooth device, but failed for the BLE device. I use following steps to do that. Allocating socket is fine, but failed when invoking connect, it always return -1 for BLE device. If I use the program to connect with classic bluetooth device, it's fine, and I can do some reading and writing things.
int nSocket = -1;
int nStatus = -1;
string strAddr = xx:xx:xx:xx:xx:xx ( bluetooth address)
struct sockaddr_rc sockAddr;
memset( ( void * )&sockAddr, 0, sizeof( sockAddr ) );
sockAddr.rc_family = AF_BLUETOOTH;
sockAddr.rc_channel = (uint8_t)1;
str2ba( strAddr.c_str(), &sockAddr.rc_bdaddr );
nSocket = socket( PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM );
if( -1 != nSocket )
nStatus = connect( nSocket, (struct sockaddr *)&m_sockAddr, sizeof( m_sockAddr ) );
Hi Sergio, Thror
I've seen the thread https://communities.intel.com/thread/56120 before, and I've told that the BLE device can be connected by the Edison breakout board with the tool bluetoothctl, but failed with my c++ code. I've also checked the code Thror post, it's just same with mine. But I can't connect to BLE device with my Edison breakout board in c++ application. Thank you.
I successfully connected two Edison and shared data between them. Unfortunately I didn't understand why the code, that is the same that I have published in the post, works for me and don't work for other people. I used the last version of Yocto for Edison and before test the code, I paired and connected the two devices with bluetoothctl. The connection is closed before launch the C/C++ code.
If I can help you in other way or i f you have any question, I'm here.
Thank you. The image of Edison I used is edison-image-ww05-15 , and I use the same code like you, but unfortunately I can't connect the target with BLE device. I can connect and read/write data with classic bluetooth device with the same code. Maybe the Edison target is not stable for BLE. I'm waiting for the new release. Thanks a lot.