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

Receiving data from Mobile APP

Hi

Firstly i am coming from 8 bit world(PIC and Arduino) and not so good at c programming. ( just writing all code in main and sometimes using function) (not even use pointer much but knowing principle)

But now, i start to work with 32 bit microprocessor and my first training is nrf51822 beacon module. I am trying to figure out this module, i wrote some spi, timer, gpiote application with copying code parts etc. They are working fine for me. (project is pov display with 32 RGB)

But i really screw up Bluetooth part. My problem is, i can't figure out how to make a basic char transfer over mobile app to beacon. I just want to send 'a' and this will blink a led for me on beacon.

In this problem, things i do;

  • Firstly i am using ble_app_uart example and ble_app_blinky example.

  • I wrote a code part that sends char to mobile app from beacon. (the opposite of what i want) i use ble_nus_string_send function for it. I desperately search for ble_nus_string_recieve but.. Nothing

  • I try to use app_uart_get and app_uart_put functions.. to be honest i don't even understand what they did.

To simplfy, i can not receive and handle a character which come from mobile app over bluetooth to NRF51822 Beacon. I try to analysis ble_app_blinky example and can not find the function which is doing recieving procedure from mobile app. Which functions do that job really ?

To be honest i can't analysis examples well. Because they all written in with structs, pointers, callbacks etc. One function always call another or being inside another function as well. I really want to learn writing code like that but i don't know how to deal with this difficulty. So i am open for all guidance.

Thanks.

  • If you set all BLE stack (Soft Device) basics (like setting parameters, enabling it, provisioning GAP and GATT layer parameters and starting advertising or scanning - depending on what GAP role you've chosen) then you will be getting asynchronous events to handler function which you've provided to SD in softdevice_ble_evt_handler_set function call. then depending on phase of the BLE connection and roles/objects you've defined you will be acting upon receiving of certain events like BLE_GAP_EVT_CONNECTED (connection just established, still some basic establishment of higher layers is needed), BLE_GATTS_EVT_WRITE (this is basically what you are looking for if you implement GATT Server on nRF5x = Client writes to some GATT object like Characteristic's Value handle or Descriptor, you should check where it wrote and then interpret written data accordingly), BLE_GAP_EVT_DISCONNECTED (connection was terminated) etc.

    This of course gets little bit more complicated if you use other BLE layers like Security manage (pairing/bonding, connection link encryption) etc. But if you choose your GAP architecture (which device is Central/Master and which Peripheral/Slave) and GATT architecture (which side will be Server and which Client) then the rest can be found after little bit of archeology in SDK examples and Infocenter sequence charts (you need to find the right API matching your SD version, e.g. here are MSCs for latest production version for nRF52832 aka S132 V4.0.x.

  • Note that actual events linked to data transfer over Nordic BLE UART (NUS) protocol in examples\ble_peripheral\ble_app_uart SDK example are little bit hidden from you because they are not handled in main.c but in ble_nus_on_ble_evt() function which is implemented in components\ble\ble_services\ble_nus\ble_nus.c file. The idea of Nordic BLE UART project is that in your "main" program it behaves like classic serial UART line where you call PUT(char) and GET(char) meta-functions and all the magic to translate it to BLE logic happens underneath and you don't need to care. But of course you can see it and debug it as it comes in source code if you want.

  • Thank you for information. I am going to check it out this in examples and surely will come back to ask questions about mentioned event handlers. This type of information little hard for me to understand. I am really new at that kind of structures.

    Also, do you got any advice about how to learn basic of BLE ( like GAP, GATT or how to set up Nordic's basic BLE stack purely) I saw "hello world of ble tutorials" but little bit scared when i read.

  • Sure indeed, here are links to several entry points, it will give you material for several weeks of study;) To be honest if you want to achieve any "production" quality BLE app it's hard to avoid that.

  • Hi again.

    I am here to confess my answer. (Cersei Talkin')

    To remember, my main goal is transmitting a char from mobile app to nrf51822 Beacon module.

    So last 3-4 days, i decide to sharpen my C programming skills, I worked on pointers, structs, callback functions etc.

    And i returned to the ble_app_uart examples and caught this.

        static void services_init(void) {
           uint32_t       err_code;
           ble_nus_init_t nus_init;
        
           memset(&nus_init, 0, sizeof(nus_init));
    
           nus_init.data_handler = nus_data_handler;
        
           err_code = ble_nus_init(&m_nus, &nus_init);
           APP_ERROR_CHECK(err_code); 
    }
    

    and this.

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) {
    for (uint32_t i = 0; i < length; i++)
    {
    		getData(p_data, bleRecData, i);
        //while(app_uart_put(p_data[i]) != NRF_SUCCESS);
    }
    //while(app_uart_put('\n') != NRF_SUCCESS); }
    

    This functions handle receiving process.

    As you can see i changed something little bit.

    What i have learn is, app_uart_put function is transmitting data which is received from mobile app, TO THE UART (TERMINAL).

    So that means (not sure), when you send characters from mobile app, this default functions take the data and send it to the terminal. I think i need some debugger for that terminal to open but not sure.

    Anyway, i don't need a terminal for my job, so I move on and create a new char string which is

     uint8_t bleRecData[256];
    

    with this i have a global data area. I can read and write in it easily now.

    As you can see above, there is a function which is not in ble_app_uart example. i wrote this getData function.

    void getData(uint8_t* recData,uint8_t* saveData, uint8_t index){
    		saveData[index] = recData[index];
    }
    

    It is so simple, maybe sounds stupid meh :) no need for that but i did this. it takes data from nus_data_handler and putting it to the my bleRecData[]

    So guys, this is how you can get data to anywhere you want.

    Summary : nus_data_handler default version is just getting character from mobile app and send it to UART module. If you want to use data directly just delete app_uart_put section and try to get p_data[] to anywhere else.

    As I say, I am a new learner, so some part maybe sounds ridiculous. I am still open for guidance and advices.

    See ya.

Related