This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

long write samples

Hi All,

Are there any updated examples for nrf51822 for writing long charecteristics. My Application has to receive more than 20 bytes at a time and send over uart.

I am using experimental/ble_app_uart as starting point. I tried modifying the on_write function in service to check

if(p_evt_write->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) {
if ((p_evt_write->handle == p_nus->tx_handles.value_handle)) { p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len); } else { // Do Nothing. This event is not relevant to this service. } }

But not getting anything on console. Without any modification app works fine for 20 bytes write. Even If send more than 20 bytes from android app it prints only 20 bytes.

Also my app requirement is to receive variable length writes from client to server.

What/Where changes needs to be made to make it working for writing more than 20 bytes ?

Am new to BLE development. Trying to learn by doing.

Thanks in Advance

  • as per your suggestion I have set reliable_wr flag for android to detect and following the sequence you have suggested. But I have one doubt on nordic app side. I think I will have to modify on_write(): if ( (p_evt_write->handle == p_nus->rx_handles.cccd_handle) && (p_evt_write->len == 2) ) { if (ble_srv_is_notification_enabled(p_evt_write->data)) { p_nus->is_notification_enabled = true; } else { p_nus->is_notification_enabled = false; } } else if ( (p_evt_write->handle == p_nus->tx_handles.value_handle) && (p_nus->data_handler != NULL) ) { p_nus->data_handler(p_nus, p_evt_write->data, p_evt_write->len); } else { // Do Nothing. This event is not relevant to this service. }

    and data_handler function void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) { for (int i = 0; i < length; i++) { simple_uart_put(p_data[i]); } simple_uart_put('\n'); }

    As per my understanding whatever mem_block we pass is populated in reliable write. So in on write function Can I just send contents of mem_block byte by byte to console using simple_uart_put ?

  • If you choose to let the stack handle the memory for you, you will not be able to see the individual chunks coming in until the execute happens. Then all of them will arrive in a single write event. If you let your application handle each request, they will arrive as BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST which you have to authorize one by one. You can choose between these behaviors by giving a mem block in mem_reply (stack handled) or NULL (app handled). The MSCs for the APP handled variant can be found here: devzone.nordicsemi.com/.../a00856.html

  • Thanks your detailed response. I will test with both approach where stack handles request and where application handles request. In case of stack handling the memory, I would have to pass address and length of memory block to my data handler from on_write function in applicatio right ? .. will try this things and update.

  • Hello, I have tried parsing the mem_block we are passing to SD. But getting confused on the parser implementation. I have tried different methods but I am not getting result. Referring to the user mem layout given here

    But I am getting inconsistent results. If I read length of memblock it shows proper value for less than 20 bytes value but above 20 bytes it shows same value.

    This is my understanding of user mem layout. So SD writes total value provided to it in chunks to user_mem_block. In case of long write to same characteristic with same handle but with increasing offset changed. So parser we will have to read first chunk then move chunk bytes ahead ..and check handle value again read chunk and so on till we get BLE_GATT_HANDLE_INVALID. I implemented a parser like this but didn't get proper result.

    Then I just printed the contents of mem_queue.I was getting proper values till 35-36 bytes but then again some random bytes then remaining bytes.

    case BLE_GATTS_EVT_WRITE:
           								
    					simple_uart_putstring("In Write Event\n\r");
    
    					if (p_ble_evt->evt.gatts_evt.params.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
    					{
    						  simple_uart_putstring("In Write Req Now Op\n\r");	
    						  int i;
    							for (i = 0; i < MEM_BLOCK_SIZE ; i++)
    							{
    								simple_uart_put(m_mem_block.p_mem[i]);
    							}
    							
    					}
    					simple_uart_put('\n');
    					simple_uart_put('\r');
            break;
    

    If you can give me a simple illustration of a parser then it would be great. Forgot to mention I am using dongle running MCP firmware to send long write values over BLE.

    Please ignore if there are any typos (I haven't had much sleep in last 2 days).

  • Your understanding is correct, as long as you remember to skip "length" number of bytes to find the next chunk. I do not have the capacity to recreate your project, so maybe you could provide some sample data and the associated uart prints you got from it? Maybe in a private message if you do not want to show too much of your work.

Related