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

I am a beginner for BLE. I want my ble to communicate with my nrf android app.

I am using Keil uvision. Working with example codes, with uart example I got the code to send a data to mobile app through ble. But I cannot find code for receiving a data by ble which is send from the mobile app. Please help me. I need a code, such that when I send a data (let it be "ON") from my mobile to the connected ble , a led must ON. And similarly when I send "OFF" the led must OFF.

Parents Reply Children
  • You are right but I flashed 2 example pgms, UART and Beacon. It is perfectly ok. It is sending a String continuously in the UART pgm. But I need to send a data back to BLE. so I need that code only.

    OK lets you give me a whole pgm: By sending a data '1' to BLE through nRF UART application from mobile, one of the GPIO led must ON, and when a '0' is send, that LED must OFF.

  • Hey,

    With UART program, i am guessing you have tried the ble_uart program.

    In that case, you should have a nus_data_handler function defined. Inside that function you can look for BLE_NUS_EVT_RX_DATA event. You will get your data sent from the mobile device which you can check and actuate accordingly.

  • 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('\r') != NRF_SUCCESS);
    while (app_uart_put('\n') != NRF_SUCCESS);
    }

    There is no BLE_NUS_EVT_RX_DATA .

  • Are you referring to UART & Blinky programs provided by ST-Link? Or the ones in the nRF5 SDK?

    There are multiple ways you can do this. As mentioned, you can modify the ble_uart program to blink a led when you write 0x0 or 0x1 from the mobile phone to the peripheral device. Otherwise, you could also take a look at the ble_app_blinky example provided in the nRF5 SDK & create a new boards file that will work with your MDBT40.

  • I got the answer. I am now able to send data (string) from my mobile to BLE device.

    In the main loop I didn't write any code. In main() function there are some function call statements as it is in the BLE UART example pgm.

    while(1)
    	 {
    		 if(i==500)
    		 {
    		  i=0;
    		 }
    		power_manage();
    	 }

    But I coded inside the nus_data_handler() function.

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
    
    	uint8_t k=0;
    	if (strstr((char*)(p_data), "on")) {
        nrf_gpio_pin_write(1,1);
    		printf("ON");
      }
    	
    	else if (strstr((char*)(p_data), "off")) {
        nrf_gpio_pin_write(1,0);
    		printf("OFF");
      }
    	
    	else if(strstr((char*)(p_data), "toggle")) {
    		while(k<10){
    		nrf_gpio_pin_toggle(1);
    		printf("TOGGLE");
    			Delay_ms(100);
    			k++;
    		}
      }
    	
    }

    When I send "on" from my mobile, then the GPIO pin 1 will become high.

    And when I send "off" , the GPIO pin 1 will set low.

    And at last, when I send "toggle", the pin 1 will start toggle for 10 times, as I coded.

Related