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

how to use nordic Connect SDK v1.5.0 to creat two ble service for nrf5340dk?

  I use a sample in NCS1.5.0 to start my project . but examples in NCS1.5.0 are single ble service .always like these: bt_conn_cb_register(&conn_callbacks);  bt_enable(bt_ready_cb_t cb); bt_le_adv_start(); and register a ble service .    

  i want to creat two independent separate  ble service .

  • Hi,

    The function bt_conn_cb_register() is used to register the callback for tracking the state of the connection, and it is not service specific. Same goes for bt_enable and bt_le_adv_start as well. If you want to register multiple services, you only need to add the callbacks for the different services, and initiate the services before you start advertising with bt_le_adv_start(). For example, if you want to use both the NUS service and the LBS service you do that by adding the following code:

    static struct bt_lbs_cb lbs_callbacs = {
    	.led_cb    = app_led_cb,
    	.button_cb = app_button_cb,
    };
    
    static struct bt_nus_cb nus_cb = {
    	.received = bt_receive_cb,
    };
    
    
    ...
    
    
    void main(void)
    {
    
    ...
    
    	err = bt_nus_init(&nus_cb);
    	if (err) {
    		LOG_ERR("Failed to initialize UART service (err: %d)", err);
    		return;
    	}
    
    	err = bt_lbs_init(&lbs_callbacs);
    	if (err) {
    		LOG_ERR("Failed to init LBS (err:%d)\n", err);
    		return;
    	}
    	
    	err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
    			      ARRAY_SIZE(sd));
    			      
    
        ...

    You will of course need to add functionality for the services. In the example above, this can be done simply by merging the Peripheral UART and Peripheral LBS samples.

    Best regards,

    Marte

  •  Marte Myrvold thank you!!

     i want to register two  ble services ,each ble service have it's own receive function and it's own send data function .sample peripheral_uart regist ble service  by bt_nus_init(&nus_cb);  this ble service receive data by function  void bt_receive_cb();
    send ble data by  ble_write_thread(void) ,in this thread ,using bt_nus_send() to send .   now i registe another ble service using function smp_bt_register(),how can i creat this ble service receive ble data cb and send ble data function or thread .how to distinguish /use different ble service 's receive data cb function and send ble data thread or function?

Related