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

Load received BLE data into an array.

Hello.  I am using the nRF51 Development Kit which is working well.  I am using the examples\ble_peripheral\ble_app_uart sample project as it seems to have the features that I am looking for, ie., receive BLE data from and external device.  I do not need Tx capabilities.  In the main.c I feel that the following procedure: static int * nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) receives the BLE data and I would like to capture the BLE data and put it in an array so that I can process it with my code.  I have created an array called ble_data[] which I would like to receive the BLE data in the code from the previous mentioned routine.  I have tried every variation I could think of, but I can not get anything to work.  The following code is your code from the above mentioned routine, and the commented out code is my code.  Could you let me know what I need to use in place of my commented out code so that I will have an array of the received BLE data? 
                              for (uint32_t i = 0; i < length; i++)
    {
      while (app_uart_put(p_data[i]) != NRF_SUCCESS)  ;
     //    ble_data[i] = *p_data + I                                                  ;            
    }
Thank you so much for your assistance!

Parents
  • Typically you use memcpy() for this, something like:

    uint8_t ble_data[BLE_GATT_ATT_MTU_DEFAULT - 3];
    memcpy(ble_data, p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

  • Thank you so much for your answer.  If I place the code in nus_data_handler I get the following errors at the end of this message.  Can you suggest an example program which has the complete working code which would load the array as you are suggesting?  Thanks so much for you assistance.

    *** Using Compiler 'V5.06 update 5 (build 528)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
    Build target 'nrf51422_xxac'
    compiling main.c...
    ..\..\..\main.c(151): error:  #20: identifier "BLE_GATT_ATT_MTU_DEFAULT" is undefined
        uint8_t ble_data[BLE_GATT_ATT_MTU_DEFAULT - 3]                ;
    ..\..\..\main.c(152): error:  #20: identifier "p_evt" is undefined
        memcpy(ble_data, p_evt->params.rx_data.p_data, p_evt->params.rx_data.length) ;

  • #include <ble_gatt.h>
    uint8_t ble_data[BLE_NUS_MAX_DATA_LEN];
    
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        memcpy(ble_data, p_data, length);
        
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
        }
        while (app_uart_put('\r') != NRF_SUCCESS);
        while (app_uart_put('\n') != NRF_SUCCESS);
    }
    /**@snippet [Handling the data received over BLE] */

  • Thanks, it is compiling with no errors, but unfortunately I have to leave the office for a couple days, so I will not be able to test the code for a couple days.  Cheers.

  • Hello,  I have to go now, but shouldn't  the memcpy be inside the for structure like below?  I will test the code when I return.

        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
           memcpy(ble_data, p_data, length)   ;
        }

  • Hello, just got my testing done, and your fix works!

    Since in my application, we will always be receiving a known length of BLE data I changed the code to the following.

    I strongly recommend that you add your solution to the SDK because judging by the number of views to my question, I can't be the only person who would like to process the received BLE data internally instead of sending it out a serial port.

    Thank you for your expert assistance!

    Cheers, Brian

    uint8_t ble_data[22]                                       ;
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
       memcpy(ble_data, p_data, length)                              ;   
        for (uint32_t i = 0; i < length; i++)
        {
            while (app_uart_put(p_data[i]) != NRF_SUCCESS);
        }

  • Hello, I was just doing some testing, and I have found that it is not necessary to include the ble_gatt.h .  All that is required to save the BLE data into a user's array is the memcpy() function.

    Cheers, Brian

    // #include <ble_gatt.h>
    uint8_t ble_data[BLE_NUS_MAX_DATA_LEN]                               ;
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        memcpy( ble_data, p_data, length )                            ;
Reply
  • Hello, I was just doing some testing, and I have found that it is not necessary to include the ble_gatt.h .  All that is required to save the BLE data into a user's array is the memcpy() function.

    Cheers, Brian

    // #include <ble_gatt.h>
    uint8_t ble_data[BLE_NUS_MAX_DATA_LEN]                               ;
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
        memcpy( ble_data, p_data, length )                            ;
Children
No Data
Related