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;

  • I managed to make it work. I modified the BLE Blinky Central and BLE Blinky peripheral examples in SDK 15.3

    See the attached zip file, which contains all the files I have modified. Use diffchecker (or similar tools) to see what I have changed.

    two_char_leds.zip

    Best regards,

    Simon

  • Thank you for sharing the code. Actually, I just realized I mentioned the code I shared is client/Switch by mistake, I  meant to say Server/Bulb(I just updated above). Sorry about that.

    To clarify further my issue is that in the Led_service_server.c  on_write(ble_led_service_t * p_led_service, ble_evt_t const * p_ble_evt) function, characteristic handle  prints the right handle value(when I push button 1 ,handle is 16 which correlate to led_2_char_handles characteristic, and when i push button 3 handle is 19 which correlate to led_3_char_handles characteristic).

    But in main.c led_write_handler(uint16_t conn_handle, ble_evt_t* p_evt, ble_led_service_t * p_led_service, uint8_t led_state)  function, p_evt_write->handle prints  handle value as 16 for both button 1 and button 3 in the client.

    Led_service_server.c  on_write()

    static void on_write(ble_led_service_t * p_led_service, ble_evt_t const * p_ble_evt)
    {
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        NRF_LOG_INFO("handle %d",p_evt_write->handle); //prints the right handle according to the button pushed
       
        if ( (p_evt_write->handle == p_led_service->led_2_char_handles.value_handle) || (p_evt_write->handle == p_led_service->led_3_char_handles.value_handle)
            && (p_evt_write->len == 1)
            && (p_led_service->led_write_handler != NULL))
        {
            p_led_service->led_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_led_service, p_evt_write->data[0]);
        }
    
    
    }
    

    main.c led_write_handler()

    static void led_write_handler(uint16_t conn_handle,ble_evt_t const * 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("Handle %d",p_evt_write->handle);    //Print 16 everytime which correlate with led_2_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!");
        }
    }

    I know I am pretty close but there is something I am not doing correctly here.What am i doing wrong?

Reply
  • Thank you for sharing the code. Actually, I just realized I mentioned the code I shared is client/Switch by mistake, I  meant to say Server/Bulb(I just updated above). Sorry about that.

    To clarify further my issue is that in the Led_service_server.c  on_write(ble_led_service_t * p_led_service, ble_evt_t const * p_ble_evt) function, characteristic handle  prints the right handle value(when I push button 1 ,handle is 16 which correlate to led_2_char_handles characteristic, and when i push button 3 handle is 19 which correlate to led_3_char_handles characteristic).

    But in main.c led_write_handler(uint16_t conn_handle, ble_evt_t* p_evt, ble_led_service_t * p_led_service, uint8_t led_state)  function, p_evt_write->handle prints  handle value as 16 for both button 1 and button 3 in the client.

    Led_service_server.c  on_write()

    static void on_write(ble_led_service_t * p_led_service, ble_evt_t const * p_ble_evt)
    {
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        NRF_LOG_INFO("handle %d",p_evt_write->handle); //prints the right handle according to the button pushed
       
        if ( (p_evt_write->handle == p_led_service->led_2_char_handles.value_handle) || (p_evt_write->handle == p_led_service->led_3_char_handles.value_handle)
            && (p_evt_write->len == 1)
            && (p_led_service->led_write_handler != NULL))
        {
            p_led_service->led_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_led_service, p_evt_write->data[0]);
        }
    
    
    }
    

    main.c led_write_handler()

    static void led_write_handler(uint16_t conn_handle,ble_evt_t const * 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("Handle %d",p_evt_write->handle);    //Print 16 everytime which correlate with led_2_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!");
        }
    }

    I know I am pretty close but there is something I am not doing correctly here.What am i doing wrong?

Children
  • Just to clarify - you have two devkits, one as server and one as client?

    The handles for registering the characteristics don't have to match between units, it's there just for the code within each unit. Data will be presented over BLE associated the appropriate UUID.

    Are you sure that you're updating the correct characteristic - best to check UUIDs using nRF Connect tool.

  • Yes, I have two devkits ,one as a server and another as a client.

    I think I am updating the right characteristics, because in Led_service_server.c  on_write()  function     p_evt_write->handle reflects  16 for led_2_char_handles and 19 for led_3_char_handles.The issue only happens in main.c led_write_handler() function ( updating led_2_char_handles and led_3_char_handles both prints 16). What do you suspect is happening here?

  • Not knowing how much code re-use you have between your two units, could you have made a cut and paste error with the registration of the two characteristics on the one side or perhaps reregistered the same UUID to the two characteristics?

    Edit:

    I typed my reply in using a mobile. Now I've had a look at some of your code snippets I see that you don't appear to be registering anything for my_button_3_handles is this still the case.

    From your first snippet:

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

  • Thanks cbd The code I shared above is for the server. I did register the Led_2 and Led_3. Here is the code registering led_handles in  led_service_server.c .

    static uint32_t led_2_char_add(ble_led_service_t * p_led_service)
    {
        ble_gatts_char_md_t char_md;
        ble_gatts_attr_t    attr_char_value;
        ble_gatts_attr_md_t attr_md;
        ble_uuid_t          ble_uuid;
    
        memset(&char_md, 0, sizeof(char_md));
        memset(&attr_md, 0, sizeof(attr_md));
        memset(&attr_char_value, 0, sizeof(attr_char_value));
    
        char_md.char_props.read          = 1;
        char_md.char_props.write         = 1;
        char_md.p_char_user_desc         = LED2CharName;
        char_md.char_user_desc_size      = sizeof(LED2CharName);
        char_md.char_user_desc_max_size  = sizeof(LED2CharName);
        char_md.p_char_pf                = NULL;
        char_md.p_user_desc_md           = NULL;
        char_md.p_cccd_md                = NULL;
        char_md.p_sccd_md                = NULL;
    
        // Define the LED 2 Characteristic UUID
        ble_uuid.type = p_led_service->uuid_type;
        ble_uuid.uuid = BLE_UUID_LED_2_CHAR_UUID;
    
        // Set permissions on the Characteristic value
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    
        // Attribute Metadata settings
        attr_md.vloc       = BLE_GATTS_VLOC_STACK;
        attr_md.rd_auth    = 0;
        attr_md.wr_auth    = 0;
        attr_md.vlen       = 0;
    
        // Attribute Value settings
        attr_char_value.p_uuid       = &ble_uuid;
        attr_char_value.p_attr_md    = &attr_md;
        attr_char_value.init_len     = sizeof(uint8_t);
        attr_char_value.init_offs    = 0;
        attr_char_value.max_len      = sizeof(uint8_t);
        attr_char_value.p_value      = NULL;
    
        return sd_ble_gatts_characteristic_add(p_led_service->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_led_service->led_2_char_handles);
    }
    
    static uint32_t led_3_char_add(ble_led_service_t * p_led_service)
    {
        ble_gatts_char_md_t char_md3;
        ble_gatts_attr_t    attr_char_value3;
        ble_gatts_attr_md_t attr_md3;
        ble_uuid_t          ble_uuid3;
    
        memset(&char_md3, 0, sizeof(char_md3));
        memset(&attr_md3, 0, sizeof(attr_md3));
        memset(&attr_char_value3, 0, sizeof(attr_char_value3));
    
        char_md3.char_props.read          = 1;
        char_md3.char_props.write         = 1;
        char_md3.p_char_user_desc         = LED3CharName;
        char_md3.char_user_desc_size      = sizeof(LED2CharName);
        char_md3.char_user_desc_max_size  = sizeof(LED2CharName);
        char_md3.p_char_pf                = NULL;
        char_md3.p_user_desc_md           = NULL;
        char_md3.p_cccd_md                = NULL;
        char_md3.p_sccd_md                = NULL;
    
        // Define the LED 2 Characteristic UUID
        ble_uuid3.type = p_led_service->uuid_type;
        ble_uuid3.uuid = BLE_UUID_LED_3_CHAR_UUID;
    
        // Set permissions on the Characteristic value
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md3.write_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md3.read_perm);
    
        // Attribute Metadata settings
        attr_md3.vloc       = BLE_GATTS_VLOC_STACK;
        attr_md3.rd_auth    = 0;
        attr_md3.wr_auth    = 0;
        attr_md3.vlen       = 0;
    
        // Attribute Value settings
        attr_char_value3.p_uuid       = &ble_uuid3;
        attr_char_value3.p_attr_md    = &attr_md3;
        attr_char_value3.init_len     = sizeof(uint8_t);
        attr_char_value3.init_offs    = 0;
        attr_char_value3.max_len      = sizeof(uint8_t);
        attr_char_value3.p_value      = NULL;
    
        return sd_ble_gatts_characteristic_add(p_led_service->service_handle, &char_md3,
                                               &attr_char_value3,
                                               &p_led_service->led_3_char_handles);
    }

    If you want to see the Client Code(Switch) here is full led_service_client.c

    static void tx_buffer_process(void)
    {
        if (m_tx_index != m_tx_insert_index)
        {
            uint32_t err_code;
    
            if (m_tx_buffer[m_tx_index].type == READ_REQ)
            {
                err_code = sd_ble_gattc_read(m_tx_buffer[m_tx_index].conn_handle,
                                             m_tx_buffer[m_tx_index].req.read_handle,
                                             0);
            }
            else
            {
                err_code = sd_ble_gattc_write(m_tx_buffer[m_tx_index].conn_handle,
                                              &m_tx_buffer[m_tx_index].req.write_req.gattc_params);
            }
            if (err_code == NRF_SUCCESS)
            {
                NRF_LOG_DEBUG("SD Read/Write API returns Success..");
                m_tx_index++;
                m_tx_index &= TX_BUFFER_MASK;
            }
            else
            {
                NRF_LOG_DEBUG("SD Read/Write API returns error. This message sending will be "
                    "attempted again..");
            }
        }
    }
    
    
    /**@brief Function for handling write response events.
     *
     * @param[in] p_ble_led_service_client Pointer to the Led Service Client structure.
     * @param[in] p_ble_evt                Pointer to the BLE event received.
     */
    static void on_write_rsp(ble_led_service_client_t * p_ble_led_service_client, ble_evt_t const * p_ble_evt)
    {
        // Check if the event if on the link for this instance
        if (p_ble_led_service_client->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
        {
            return;
        }
        // Check if there is any message to be sent across to the peer and send it.
        tx_buffer_process();
    }
    
    
    /**@brief Function for handling Disconnected event received from the SoftDevice.
     *
     * @details This function check if the disconnect event is happening on the link
     *          associated with the current instance of the module, if so it will set its
     *          conn_handle to invalid.
     *
     * @param[in] p_ble_led_service_client Pointer to the Led Service Client structure.
     * @param[in] p_ble_evt                Pointer to the BLE event received.
     */
    static void on_disconnected(ble_led_service_client_t * p_ble_led_service_client, ble_evt_t const * p_ble_evt)
    {
        if (p_ble_led_service_client->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
        {
            p_ble_led_service_client->conn_handle                    = BLE_CONN_HANDLE_INVALID;
            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;
        }
    }
    
    
    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);
    }
    
    void ble_led_service_client_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        if ((p_context == NULL) || (p_ble_evt == NULL))
        {
            return;
        }
    
        ble_led_service_client_t * p_ble_led_service_client = (ble_led_service_client_t *)p_context;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GATTC_EVT_WRITE_RSP:
                on_write_rsp(p_ble_led_service_client, p_ble_evt);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                on_disconnected(p_ble_led_service_client, p_ble_evt);
                break;
    
            default:
                break;
        }
    }
    
    
    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;
    }
    
    uint32_t ble_led_service_client_handles_assign(ble_led_service_client_t    * p_ble_led_service_client,
                                                   uint16_t                      conn_handle,
                                                   const led_service_db_t      * p_peer_handles)
    {
        VERIFY_PARAM_NOT_NULL(p_ble_led_service_client);
    
        p_ble_led_service_client->conn_handle = conn_handle;
        if (p_peer_handles != NULL)
        {
            p_ble_led_service_client->peer_led_service_db = *p_peer_handles;
        }
        return NRF_SUCCESS;
    }
    

  • Have you made this work? If not, please tell me, and I will look into it.

Related