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

How to send simple string data ?

I am receiving data from a phone, i want to send back a word .

So , this is probably wrong but , to send :

  void sendBluetoothData( uint8_t data[], uint16_t length)
  {
      uint32_t       err_code;
      static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
      err_code = ble_nus_string_send(&m_nus, data, length);
       if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
        {
             //APP_ERROR_CHECK(err_code);
        }
  }

and use it with :

  uint8_t tosend[5];
     tosend[0]=7;
     tosend[1]=2;
     tosend[2]=7;
     tosend[3]=6;
     sendBluetoothData(tosend,4);

I think it sends 0 twice back to the phone.

How should I re-write this function to send strings out ?

(using SDK 13 with the uart example which I try to write by my self)

Parents
  • This is from the ble_peripheral/ble_app_uart example in SDK 12.2 in my case, I guess it's not very different from SDK13.

    Simple equivalent of hello world:

    /**@snippet [Handling the data received over BLE] */
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length){
        if(p_data[0] == '?'){
            unsigned char data_arr[20] = {"Hello world!\t-Pong!\n"}; // 20 byte msg
            ble_nus_string_send(&m_nus, data_arr, 20);
        }
    }
    

    p_data[0] contains the first character in whatever you recieved from your phone, in this case if it's a "?" it replies with a string..

    I prefer to use nRF Toolbox on my iphone with the UART profile for testing

    From here it was fairly easy to go to strtok() and compare strings/characters in switches to handle incomming text, it's not so pretty but it works.

    Screenshot from phone: UART APP

Reply
  • This is from the ble_peripheral/ble_app_uart example in SDK 12.2 in my case, I guess it's not very different from SDK13.

    Simple equivalent of hello world:

    /**@snippet [Handling the data received over BLE] */
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length){
        if(p_data[0] == '?'){
            unsigned char data_arr[20] = {"Hello world!\t-Pong!\n"}; // 20 byte msg
            ble_nus_string_send(&m_nus, data_arr, 20);
        }
    }
    

    p_data[0] contains the first character in whatever you recieved from your phone, in this case if it's a "?" it replies with a string..

    I prefer to use nRF Toolbox on my iphone with the UART profile for testing

    From here it was fairly easy to go to strtok() and compare strings/characters in switches to handle incomming text, it's not so pretty but it works.

    Screenshot from phone: UART APP

Children
Related