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

required Bluetooth ON and OFF functions in "ble_app_uart" example

Hi,

I want to write Bluetooth ON and OFF functions for the project purpose,

what are all the steps needs to follow while creating ON function and same for OFF function.

Thanks in advance

Kind Regards,

Thiru.

Parents
  • Define two command "BT ON"&"BT OFF"

    if client get nus_data_handler()

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {

    char* str_cmp;

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
    uint32_t err_code;

    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);

     str_cmp =strstr(p_evt->params.rx_data.p_data,""BT ON");

     if (str_cmp!=NULL)   do_on_func();

    str_cmp =strstr(p_evt->params.rx_data.p_data,""BT OFF");

    if (str_cmp!=NULL)   do_off_func();

    }

    }

  • Hi Henry_Chou

    I tried lots of time but i am not able to ON and OFF 

    Receive data via UART data_array[].It get's OFF but not getting ON i don't have Idea

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t err_code;
    /****************************************************************/
    if(data_array[0] == '1')
    {
         // ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
         ble_advertising_restart_without_whitelist(&m_advertising);

    }
    else if(data_array[0] == '2')
    {
         sleep_mode_enter();
    }
    /****************************************************************/
    switch (p_event->evt_type)
    {
    case APP_UART_DATA_READY:
    UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    index++;
    if ((data_array[index - 1] == '\n') ||
    (data_array[index - 1] == '\r') ||
    (index >= m_ble_nus_max_data_len))
    {
    if (index > 1)
    {
    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

    do
    {
    uint16_t length = (uint16_t)index;
    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);
    }
    } while (err_code == NRF_ERROR_RESOURCES);
    }

    index = 0;
    }
    break;

    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }
Reply
  • Hi Henry_Chou

    I tried lots of time but i am not able to ON and OFF 

    Receive data via UART data_array[].It get's OFF but not getting ON i don't have Idea

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t err_code;
    /****************************************************************/
    if(data_array[0] == '1')
    {
         // ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
         ble_advertising_restart_without_whitelist(&m_advertising);

    }
    else if(data_array[0] == '2')
    {
         sleep_mode_enter();
    }
    /****************************************************************/
    switch (p_event->evt_type)
    {
    case APP_UART_DATA_READY:
    UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    index++;
    if ((data_array[index - 1] == '\n') ||
    (data_array[index - 1] == '\r') ||
    (index >= m_ble_nus_max_data_len))
    {
    if (index > 1)
    {
    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

    do
    {
    uint16_t length = (uint16_t)index;
    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);
    }
    } while (err_code == NRF_ERROR_RESOURCES);
    }

    index = 0;
    }
    break;

    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }
Children
No Data
Related