This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

disconnect if wrong characteristic should be written

Hi guys,

i have a service with more characteristics and more states. Not every characteristic should be available in each state. Thus there are services which are just allowed in one specific state.

If a device wants to write the wrong characteristic in a state, i just want to disconnect the connection (during the write operation). Is that allowed or is there a good way to disconnect during the write operation?

Like:

if ((p_evt_write->handle == p_ps->char_handle.value_handle) &&
    (p_evt_write->len < MAX_DATA_LENGTH) &&
    (p_ps->handler_char != NULL))
{
     if(incorrectState) { sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); }

}

Thanks in advance! greetings

  • You should force the central to do a service change operation to ensure that the services/characteristics are updates. It's possible to do so and that way make sure that the cached information about the peripheral is deleted and a new service discovery is done. Also, since you are moving through different states I would recommend that you disable the services you don't need. Worst case you can disconnect as you suggest.

  • Hi Asbjørn,

    thanks for our answer. So you would group all characteristics to services, and just enable thous which are allowed to write in the current state? If a service is not broadcasted, is it still possible to write it? (never tested it) :)

    The thing is, if i disconnect as i wrote in my question post, sometimes the connection hangs, and the peripheral do not disconnect. And i don't know where the problem is..

    Greetings

  • If you have states you rotate through you should disable the services that are not available in those states so that they are not accessible. I don't know if you're able to have such a strict distinction between your states. So every time you switch between states you force an update of which services are available and make sure that the others are not access from either side. If the service isn't broadcasted, it shouldn't be possible to write to it either. But if you have a set of services running all the time, but only make one of them available at any given time, you could potentially run into a situation where they are trying to access a service that isn't available, but it's still there out of lack of refreshing cache. That's why it would be important to update the GATT table with which services that are available.

    The disconnect option should be the last option as you could see the behavior you describe and this could go on until the connection times out.I don't know what kind of FW you are running right now, but you could try to reduce the connection time out.

    There might be other ways to implement this, but I don't know what you are trying to do more than the short description initially.

Related