1 2 Previous Next 20 Replies Latest reply on Sep 25, 2015 12:55 AM by hopstone

    Connect to BLE device failed on Edison breakout board

    hopstone

      Hi,

      I'm writing an application for Edison to connect to a BLE(Bluetooth Low Energy) device and read/write data from/to the device. I've referred to rfcomm-client.c of RFCOMM sockets , but always failed with -1 when connect. I also called "rfkill unblock bluetooth" and "hciconfig hci0 up" before connect, but failed again.  The pin code of the BLE device is "1234", I referred to the steps of http://www.linuxquestions.org/questions/linux-wireless-networking-41/setting-up-bluez-with-a-passkey-pin-to-be-used-as-h… to run simple-agent of bluez5 on target, also failed with -1 when calling connect. I'm sure that the address of BLE device is correct. I can use bluetoothctl to connect with it. I've flashed the target with latest image, "uname -v" shows "#1 SMP PREEMPT Fri Jan 30 14:16:35 CET 2015, which downloaded from Edison - Software Downloads .  Why Edison can't connect to the BLE device with C code?  Does anyone realized it?  Thanks.

        • 1. Re: Connect to BLE device failed on Edison breakout board
          Intel_Alvarado

          Hi hopstone,

           

          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.

           

          Regards

          Sergio

          • 2. Re: Connect to BLE device failed on Edison breakout board
            hopstone

            Hi Alvarado,

             

            Thanks for your quick reply. I checked the website you mentioned, but it's the user guide of wifi, not for bluetooth. Would you please help us to update it with the bluetooth user guide for development? Thank you very much.

             

            wifi-bt.png

            • 3. Re: Connect to BLE device failed on Edison breakout board
              hopstone

              Hi,

               

              Is it problem of Edison bluetooth module? I can connect to classic bluetooth device and read/write data successfully with the same code, but failed when connecting to BLE device. Does anyone meet the same problem?

              • 5. Re: Connect to BLE device failed on Edison breakout board
                hopstone

                Dear David,

                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 .

                • 6. Re: Connect to BLE device failed on Edison breakout board
                  Intel_Alvarado

                  Hi hopstone,

                   

                  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.

                  Regards

                  Sergio

                  • 7. Re: Connect to BLE device failed on Edison breakout board
                    hopstone

                    Hi Sergio,

                     

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

                    }

                     

                    • 8. Re: Connect to BLE device failed on Edison breakout board
                      Intel_Alvarado

                      Hi hopstone,

                       

                      Have you already seen this thread https://communities.intel.com/thread/56120 on BLE?

                       

                      Sergio

                      • 9. Re: Connect to BLE device failed on Edison breakout board
                        Thror

                        Hi hopstone,

                         

                        I was able to exchange messages between two Edison through bluetooth, programming in c/c++. I post the code in intel Edison Bluetooth connection refuse . It is pretty similar to the code provided by RFCOMM sockets.

                         

                        I hope that is helpful.

                         

                        Best Regards,

                         

                        Thror

                        • 10. Re: Connect to BLE device failed on Edison breakout board
                          hopstone

                          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.

                          • 11. Re: Connect to BLE device failed on Edison breakout board
                            Thror

                            Hi hopstone,

                             

                            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.

                             

                            Best,

                             

                            Thror

                            • 12. Re: Connect to BLE device failed on Edison breakout board
                              hopstone

                              Hi Thror

                               

                              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.

                              • 13. Re: Connect to BLE device failed on Edison breakout board
                                Intel_Alvarado

                                Hi hopstone,

                                 

                                If you run the command configure_edison –version the output is 120 if you are running the latest image version. What is your output of this command?

                                 

                                Sergio

                                • 14. Re: Connect to BLE device failed on Edison breakout board
                                  hopstone

                                  Hi Sergio,

                                  I tried the command configure_edison --version , it shows 120 .

                                   

                                  Regards,

                                  Rock

                                  1 2 Previous Next