Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Can't proproly recive commands form mobil

I'm just trying to make a simple communicatión between my nrf52 dk and my mobil using Serial Bluetooth Terminal app. I took the ble_app_uart example and I added a function that turn on or off the Led 2 if the DK recives a 'a' or a 's', but I don't know why it didn't work, and I can't find out where is the error.

The only thing I changed is the for loop for a while loop, where I added the idle_state_handle function and the function I developed  

static void communication(void)
{
	uint8_t command;
	
	while(app_uart_get(&command) != NRF_SUCCESS);
	
	if(command == 'a')
	{
		bsp_board_led_on(1);
		printf("Leds turned on!!\r\n");
	}
	else if(command == 's')
	{
		bsp_board_led_off(1);
		printf("Leds turned off!!\r\n");
	}
	else
	{
		printf("Invalid command!!\r\n");
	}
	
}

  • I suggest to contine the debugging, for new development you should check out the nRF Connect SDK.

    Start with https://academy.nordicsemi.com/ and the follow up with https://webinars.nordicsemi.com/developing-bluetooth-low-energy-6 

  • Okay, I finaly solve it. In the nus_data_handler function I implement this code for the reciving command part:

    static void nus_data_handler(ble_nus_evt_t * p_evt) // data handler
    {
    	char letra = 0;
    	
      if (p_evt->type == BLE_NUS_EVT_RX_DATA)
      {
    		
        NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
        NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
    
        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
    		{            
    			letra = p_evt->params.rx_data.p_data[i];
    			if(letra == 97)
    			{
    				bsp_board_led_on(3);
    			}
    			if(letra == 98)
    			{
    				bsp_board_led_off(3);
    			}
    		}
        if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
        {
    			while (app_uart_put('\n') == NRF_ERROR_BUSY);
        }
    	}
    }

    And then, to send messages to the DK I implement this one:

    static void send_command(uint8_t data_array[BLE_NUS_MAX_DATA_LEN]) // data sending
    {
    
    	uint32_t err_code;
    	
    	idle_state_handle();
    	
    	uint16_t length = 0; // data_array length
    	
    	for(int i=0; i<BLE_NUS_MAX_DATA_LEN; i++)
    	{
    		if(data_array[i] != 0)
    		{
    			length ++;
    		}
    	}
    	
      err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
    			
      if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_RESOURCES) && (err_code != NRF_ERROR_NOT_FOUND))
    	{
    		APP_ERROR_CHECK(err_code);
      }
    }

Related