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

Check notification status / toggle notification status Mutilink Central Example

Hello,

can someone share a piece of code or a hint to the right functions that can enable/disable notifications on all peers? All I want is that the central toggles notifications for all peers on key press. All I've got so far is the keypress.

As far as I digged through the client handler it seems I could use

p_client = &m_client[index]; in a for loop with index 0-3 for 4peers e.g.? and then use code like in notif_enable(client_t * p_client).

But how can I call/use this in main.c? (So I can work with it inside my key_evt handler)

Parents
  • What u need to do is create the function that you need to call to disable the notification in the c file and then put the function prototype into the h file. The main.c should already have the h file included already. In your function u would just call it with a void argument since u are going to go through all the connected device. U might want to have it return the status. Your function will be in the client_handle.c and it should use m_client variable. Since the p_client you are trying to use is not in the scope of your new function you would have to declare it in the new function. It would be something like.

    uint32_t client_handle_set_all_notification(bool arg)
    {
    Uint32_t err_code = NRF _SUCCESS;
      client_t *p_client;
      for(int i =0; i<m_client_count; i++)
      {
    If(arg = 0) 
    { 
    err_code = notif_disabled(p_client);
    }
    else
    {
    err_code = notif_enable(p_client);
    }
    If(err_code != NRF_Success) break;
    }
    return err_code;
    }
    

    You will have to do your own disabling function. Should be the same as notif_enable but different parameter. You would be the function prototype in the h file and call the function with argument 0 to turn off notification and other to enable all notification. I am not at a computer and typing this from my phone. So might not be correct. But it should be something along this line. Hope it help.

Reply
  • What u need to do is create the function that you need to call to disable the notification in the c file and then put the function prototype into the h file. The main.c should already have the h file included already. In your function u would just call it with a void argument since u are going to go through all the connected device. U might want to have it return the status. Your function will be in the client_handle.c and it should use m_client variable. Since the p_client you are trying to use is not in the scope of your new function you would have to declare it in the new function. It would be something like.

    uint32_t client_handle_set_all_notification(bool arg)
    {
    Uint32_t err_code = NRF _SUCCESS;
      client_t *p_client;
      for(int i =0; i<m_client_count; i++)
      {
    If(arg = 0) 
    { 
    err_code = notif_disabled(p_client);
    }
    else
    {
    err_code = notif_enable(p_client);
    }
    If(err_code != NRF_Success) break;
    }
    return err_code;
    }
    

    You will have to do your own disabling function. Should be the same as notif_enable but different parameter. You would be the function prototype in the h file and call the function with argument 0 to turn off notification and other to enable all notification. I am not at a computer and typing this from my phone. So might not be correct. But it should be something along this line. Hope it help.

Children
  • Thank you very much! This was way more than needed but it will help me a lot! I don't have my DK at home at the moment so testing will take till Monday. But I am 100% sure that it won't be a big problem any longer!

    Something like this should do I guess:

    void toggle_notif(bool toggle_bool)
    {
    		client_t *p_client;
    		ble_gattc_write_params_t write_params;
        uint8_t                  buf[2];
    
    	for(int i=0; i<m_client_count; i++)
    	{
    		p_client = &m_client[i];
        p_client->state = STATE_NOTIF_ENABLE;
    
        buf[0] = toggle_bool;
        buf[1] = 0;
    
        write_params.write_op = BLE_GATT_OP_WRITE_REQ;
        write_params.handle   = p_client->srv_db.services[0].charateristics[p_client->char_index].cccd_handle;
        write_params.offset   = 0;
        write_params.len      = sizeof(buf);
        write_params.p_value  = buf;
    
        sd_ble_gattc_write(p_client->srv_db.conn_handle, &write_params);
    	}
    }
    

    I am only wondering about what to do with the clients state..

  • Looks good. I am not sure about the message that has to be sent out. check if the data in the buffer to send out is correct.

  • It basically works fine as far as I have just tried with one peripheral. But I would like to add a "Notif. enabled/disabled" printf command.. The problem is, that after the printf(Notf disabled) always one more measurement comes in. So I guess I would need to send that message after the notification disabled ack came in.

Related