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

BLE service with multiple characteristics(client/switch and bulb/server)

I am new to C programming and BLE in general.I am trying to create a service with two characteristics,(the application simply is a modification of BLE bulb and switch). How the original application work is, on the client/switch if you push button 1, it turns on the LED 3 on the server/bulb, and when you push button 2 it turns it off.So it is only one service and one characteristic. The client acts as central and server acts as a peripheral.

What I want is to add another characteristic so that If I push button 3 on the client to turn on  LED 4 on the server .(So in short button 1 will turn LED 3 on and button 3 will turn LED4 on).So I added additional characteristic on both server and client, but the problem is I don't know how to check/distinguish whether the incoming request  on the server is which characteristic.

Question

1. How do I inspect the incoming request whether is coming from characteristic 1 or 2 in the server's main.c  led_write_handler function?

Here is the code

static void led_write_handler(uint16_t conn_handle, ble_led_service_t * p_led_service, uint8_t led_state)
{
   //HERE I WANT TO DO IF THE REQUEST IS CHARACTERSIC 1 TURN LED3 AND IF 2 TURN LED 4
     //NRF_LOG_INFO("led handled %d", p_led_service->led_3_char_handles);
    if (led_state)
    {
        bsp_board_led_on(LIGHTBULB_LED3);
        NRF_LOG_INFO("Received LED ON!");
    }
    else
    {
        bsp_board_led_off(LIGHTBULB_LED3);
        NRF_LOG_INFO("Received LED OFF!");
    }
}

Parents
  • Hi,

    Assuming that your code uses SDK functions look for the call to characteristic_add().

    When the BLE is initialised all services, characteristics, attributes are registered before they can be used. On registration a handle is returned (this allows for the same characteristic type to be used in multiple services).

    For example you may have:

     

    characteristic_add(p_led_service->service_handle, &add_char, &p_led_service->my_char_button1_handle);
    
    
    // ... and later
    
    characteristic_add(p_led_service->service_handle, &add_char, &p_led_service->my_char_button2_handle);

    p_led_service->my_char_button1_handle is the handle registration data for one characteristic and p_led_service->my_char_button2_handle for the other.

    When the BLE event is triggered you'll get some event data and you can check for this (in ble_evt_t parameter) You could pass this into your led_write_handler 

    Then you could have:

    static void led_write_handler(uint16_t conn_handle, ble_evt_t* p_evt, ble_led_service_t * p_led_service, uint8_t led_state)
    {
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    	
    	if(p_evt_write->handle == p_led_service->my_button3_handles.value_handle)
        {
    		if (led_state)
    		{
    			bsp_board_led_on(LIGHTBULB_LED3);
    			NRF_LOG_INFO("Received LED ON!");
    		}
    		else
    		{
    			bsp_board_led_off(LIGHTBULB_LED3);
    			NRF_LOG_INFO("Received LED OFF!");
    		}
    	}
    }

    Personally, I wouldn't use multiple characteritics for this. You have at least 20 bytes in each characteristic to play with, so why not use just the one characteristic and pass led number as well as led state? Then your handler can be passed both values and the code becomes simpler.

  • Thank you @cbd I tried to inspect the handle on an incoming request, but regardless which button I press I keep getting value 16 in the log (shown below),I was expecting value 16 for button1 and 19 for button3(at least different value for each button.) What am I doing wrong?

    static void led_write_handler(uint16_t conn_handle,ble_evt_t* p_evt, ble_led_service_t * p_led_service, uint8_t led_state)
    {
     ble_gatts_evt_write_t const * p_evt_write = &p_evt->evt.gatts_evt.params.write;
         NRF_LOG_INFO("Value Handle %d",p_evt_write->handle);//This prints 16 for both button1 and bnutton3
     
        if (led_state)
        {
            bsp_board_led_on(LIGHTBULB_LED3);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LIGHTBULB_LED3);
            NRF_LOG_INFO("Received LED OFF!");
        }
    }
    Below is the code from the Server/Bulb(Note that I have only shared the most important snippets of the application)

    Server main.c

    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
    
        switch (pin_no)
        {
            case LEDBUTTON_ON_BUTTON_PIN:
                err_code = ble_led_service_led2_setting_send(&m_ble_led_service_client, 1);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                if (err_code == NRF_SUCCESS)
                {
                    NRF_LOG_INFO("LED Service write LED2 state %d", button_action);
                }
                break;
    
            case LEDBUTTON_OFF_BUTTON_PIN:
                err_code = ble_led_service_led2_setting_send(&m_ble_led_service_client, 0);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                if (err_code == NRF_SUCCESS)
                {
                    NRF_LOG_INFO("LED Service write LED2 state %d", button_action);
                }
                break;
    
            case LEDBUTTON_ON_BUTTON_PIN3:
                err_code = ble_led_service_led3_setting_send(&m_ble_led_service_client, 1);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                if (err_code == NRF_SUCCESS)
                {
                    NRF_LOG_INFO("LED Service write LED3 state %d", button_action);
                }
                break;
    
            case LEDBUTTON_OFF_BUTTON_PIN3:
                err_code = ble_led_service_led3_setting_send(&m_ble_led_service_client, 0);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                if (err_code == NRF_SUCCESS)
                {
                    NRF_LOG_INFO("LED Service write LED2 state %d", button_action);
                }
                break;
    
    
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
            
        }
    }
    

    Led_service_server.c

    
    
    void ble_led_service_on_db_disc_evt(ble_led_service_client_t * p_ble_led_service_client, ble_db_discovery_evt_t const * p_evt)
    {
        // Check if the Led Button Service was discovered.
        if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
            p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_LED_SERVICE_UUID &&
            p_evt->params.discovered_db.srv_uuid.type == p_ble_led_service_client->uuid_type)
        {
            ble_led_service_client_evt_t evt;
    
            evt.evt_type    = BLE_LED_SERVICE_CLIENT_EVT_DISCOVERY_COMPLETE;
            evt.conn_handle = p_evt->conn_handle;
    
            for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
            {
                const ble_gatt_db_char_t * p_char = &(p_evt->params.discovered_db.charateristics[i]);
                switch (p_char->characteristic.uuid.uuid)
                {
                    case BLE_UUID_LED_2_CHAR_UUID:
                        NRF_LOG_INFO("in case UIID2 ",p_char->characteristic.uuid.uuid);
                        evt.peer_db.led2_handle = p_char->characteristic.handle_value;
                        break;
                   case BLE_UUID_LED_3_CHAR_UUID:
                    NRF_LOG_INFO("in case UIID3 ",p_char->characteristic.uuid.uuid);
                        evt.peer_db.led3_handle = p_char->characteristic.handle_value;
                        break;
    
                    default:
                        break;
                }
            }
    
            NRF_LOG_DEBUG("Led Service discovered at peer.");
            //If the instance has been assigned prior to db_discovery, assign the db_handles
            if (p_ble_led_service_client->conn_handle != BLE_CONN_HANDLE_INVALID)
            {
                if (p_ble_led_service_client->peer_led_service_db.led2_handle         == BLE_GATT_HANDLE_INVALID)
                {
                    p_ble_led_service_client->peer_led_service_db = evt.peer_db;
                }
                 if (p_ble_led_service_client->peer_led_service_db.led3_handle         == BLE_GATT_HANDLE_INVALID)
                {
                    p_ble_led_service_client->peer_led_service_db = evt.peer_db;
                }
            }
    
            p_ble_led_service_client->evt_handler(p_ble_led_service_client, &evt);
    
        }
    }
    
    uint32_t ble_led_service_client_init(ble_led_service_client_t * p_ble_led_service_client, ble_led_service_client_init_t * p_ble_led_service_client_init)
    {
        uint32_t      err_code;
        ble_uuid_t    led_service_uuid;
        ble_uuid128_t led_service_base_uuid = {BLE_UUID_LED_SERVICE_BASE_UUID};
    
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client);
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client_init);
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client_init->evt_handler);
    
        p_ble_led_service_client->peer_led_service_db.led2_handle   = BLE_GATT_HANDLE_INVALID;
        p_ble_led_service_client->peer_led_service_db.led3_handle   = BLE_GATT_HANDLE_INVALID;
        p_ble_led_service_client->conn_handle                      = BLE_CONN_HANDLE_INVALID;
        p_ble_led_service_client->evt_handler                      = p_ble_led_service_client_init->evt_handler;
    
        err_code = sd_ble_uuid_vs_add(&led_service_base_uuid, &p_ble_led_service_client->uuid_type);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
        VERIFY_SUCCESS(err_code);
    
        led_service_uuid.type = p_ble_led_service_client->uuid_type;
        led_service_uuid.uuid = BLE_UUID_LED_SERVICE_UUID;
    
        return ble_db_discovery_evt_register(&led_service_uuid);
    }
    
    
    uint32_t ble_led_service_led2_setting_send(ble_led_service_client_t * p_ble_led_service_client, uint8_t status)
    {
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client);
    
        if (p_ble_led_service_client->conn_handle == BLE_CONN_HANDLE_INVALID)
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        NRF_LOG_DEBUG("writing LED2 status 0x%x", status);
      
    
        tx_message_t * p_msg;
    
        p_msg              = &m_tx_buffer[m_tx_insert_index++];
        m_tx_insert_index &= TX_BUFFER_MASK;
    
        p_msg->req.write_req.gattc_params.handle   = p_ble_led_service_client->peer_led_service_db.led2_handle;
        p_msg->req.write_req.gattc_params.len      = sizeof(status);
        p_msg->req.write_req.gattc_params.p_value  = p_msg->req.write_req.gattc_value;
        p_msg->req.write_req.gattc_params.offset   = 0;
        p_msg->req.write_req.gattc_params.write_op = BLE_GATT_OP_WRITE_CMD;
        p_msg->req.write_req.gattc_value[0]        = status;
        p_msg->conn_handle                         = p_ble_led_service_client->conn_handle;
        p_msg->type                                = WRITE_REQ;
    
        tx_buffer_process();
        return NRF_SUCCESS;
    }
    
    uint32_t ble_led_service_led3_setting_send(ble_led_service_client_t * p_ble_led_service_client, uint8_t status)
    {
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client);
    
        if (p_ble_led_service_client->conn_handle == BLE_CONN_HANDLE_INVALID)
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        NRF_LOG_DEBUG("writing LED3 status 0x%x", status);
        NRF_LOG_DEBUG("CON HANDLE %d", p_ble_led_service_client->conn_handle);
    
        tx_message_t * p_msg;
    
        p_msg              = &m_tx_buffer[m_tx_insert_index++];
        m_tx_insert_index &= TX_BUFFER_MASK;
    
        p_msg->req.write_req.gattc_params.handle   = p_ble_led_service_client->peer_led_service_db.led3_handle;
        p_msg->req.write_req.gattc_params.len      = sizeof(status);
        p_msg->req.write_req.gattc_params.p_value  = p_msg->req.write_req.gattc_value;
        p_msg->req.write_req.gattc_params.offset   = 0;
        p_msg->req.write_req.gattc_params.write_op = BLE_GATT_OP_WRITE_CMD;
        p_msg->req.write_req.gattc_value[0]        = status;
        p_msg->conn_handle                         = p_ble_led_service_client->conn_handle;
        p_msg->type                                = WRITE_REQ;
    
        tx_buffer_process();
        return NRF_SUCCESS;
    }

    Led_service_server.h

    #define BLE_UUID_LED_SERVICE_BASE_UUID  {0x6C, 0xCE, 0x98, 0x91, 0xB9, 0xB3, 0x11, 0x87, 0x9E, 0x47, 0xF5, 0x67, 0x00, 0x00, 0x4B, 0xE5}
    
    // Service & characteristics UUIDs
    #define BLE_UUID_LED_SERVICE_UUID  0x0001
    #define BLE_UUID_LED_2_CHAR_UUID   0x0002
    #define BLE_UUID_LED_3_CHAR_UUID   0x0003
    
    
    /**@brief Structure containing the handles related to the LED Button Service found on the peer. */
    typedef struct
    {
        uint16_t led2_handle;          /**< Handle of the LED characteristic as provided by the SoftDevice. */
        uint16_t led3_handle; 
    } led_service_db_t;
    
    /**@brief LED Event structure. */
    typedef struct
    {
        ble_led_service_client_evt_type_t evt_type;        /**< Type of the event. */
        uint16_t                        conn_handle;     /**< Connection handle on which the event occured.*/
        led_service_db_t         peer_db;         /**< LED Service related handles found on the peer device. This will be filled if the evt_type is @ref BLE_LED_SERVICE_CLIENT_EVT_DISCOVERY_COMPLETE.*/
    } ble_led_service_client_evt_t;
    
    // Forward declaration of the ble_led_service_client_t type.
    typedef struct ble_led_service_client_s ble_led_service_client_t;
    
    
    typedef void (* ble_led_service_client_evt_handler_t) (ble_led_service_client_t * p_led_service_client, ble_led_service_client_evt_t * p_evt);
    
    /**@brief LED Service Client structure. */
    struct ble_led_service_client_s
    {
        uint16_t                              conn_handle;                 /**< Connection handle as provided by the SoftDevice. */
        led_service_db_t                      peer_led_service_db;  /**< Handles related to LED Service on the peer*/
        ble_led_service_client_evt_handler_t  evt_handler;                 /**< Application event handler to be called when there is an event related to the LED service. */
        uint8_t                               uuid_type;                   /**< UUID type. */
    };
    
    /**@brief LED Service Client initialization structure. */
    typedef struct
    {
        ble_led_service_client_evt_handler_t evt_handler;  /**< Event handler to be called by the LED Service Client module whenever there is an event related to the LED Service. */
    } ble_led_service_client_init_t;

  • Simon ,

    I did what you advised but It seems like I am still experiencing the same issue. On main.c led_write_handler() the uuid always prints 65632 when I press button 2 or 3.

    But on led_server_service.c on_write() uuid prints correctly 2 for button 2 and 3 for button 3 which are the uuid's I defined. So the issue is still on main.c led_write_handler()

    static void led_write_handler(uint16_t conn_handle, ble_led_service_t * p_led_service, uint8_t led_state,uint16_t led_uuid)
    {
    
         NRF_LOG_INFO("led uuid= %d",led_uuid); //Always prints 65632
    
        if (led_state)
        {
            bsp_board_led_on(LIGHTBULB_LED3);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LIGHTBULB_LED3);
            NRF_LOG_INFO("Received LED OFF!");
        }
    }

  • I managed to make it work (led_write_handler prints correct uuid). Follow these steps exactly to make it work:

    • Download SDK 15.3.0 and place it into an appropriate location
    • Download the file two_char_leds.rar (which I have provided)
    • For the peripheral example
      • Swap <..>two_char_leds\led_periph_server/ble_lbs.c and <..>two_char_leds\led_periph_server/ble_lbs.h with nRF5_SDK_15.3.0_59ac345\components\ble\ble_services\ble_lbs\ble_lbs.c and nRF5_SDK_15.3.0_59ac345\components\ble\ble_services\ble_lbs\ble_lbs.h 
      • Swap <..>\two_char_leds\led_periph_server\main.c with nRF5_SDK_15.3.0_59ac345\examples\ble_peripheral\ble_app_blinky\main.c
    • For the central example
      • Swap <..>two_char_leds\led_central_client/ble_lbs_c.c and <..>two_char_leds\led_central_client/ble_lbs_c.h with nRF5_SDK_15.3.0_59ac345\components\ble\ble_services\ble_lbs_c\ble_lbs_c.c and nRF5_SDK_15.3.0_59ac345\components\ble\ble_services\ble_lbs_c\ble_lbs_c.h 
      • Swap <..>\two_char_leds\led_central_client\main.c with nRF5_SDK_15.3.0_59ac345\examples\ble_central\ble_app_blinky_c\main.c
    • Connect two nRF52832 DK (that is what I tested with, but it should work with nRF52840 as well) to the computer and build and flash the ble_app_blinky_c example onto one of the boards and the ble_app_blinky example onto the other board
    • Press button 1 on the central board, which should turn on/off  led 3 on the peripheral board
    • Press button 2 on the central board, which should turn on/off led 4 on the peripheral board

    two_char_leds.rar

    Best regards,

    Simon

  • Thank you, Simon, for sharing the code and taking the time to help. I am going to compare your code with mine. Right off the bat, I can see your advertising_init implementation is a little different from mine. But I am still continuing to look for discrepancies.

  • No problem, I'm always happy to help. Hopefully you are able to get to the bottom of it now.

    Best regards,

    Simon

  • +1 Simon.

    Just back from vacation. I'm glad that things are sorted now.

Reply Children
No Data
Related