How shall i define multiple services in GATT Server database, so that only 1 service is seen by GATT Client at a time ?

I am developing GATT Server Reader Firmware usng nRF SDK COnnect using NRF52840

I have implemented two services in reader FW in two files say: service_a.c and service_b.c

Both files have their respective GATT attribute table.

Both have their advertisement packet defined but only one is advertised based on user input.

Example scenario : Reader is advertising service a, when user inputs 1 to reader.

Reader changes its advertisement to service b, when user inputs 2 to reader.

Now, when user enters 1, mobile sees service a advertisement and it connects with reader.

Problem is .. when Mobile connects with Reader .. it sees service a as well as service b both !! 

How do i only expose service a when connected via service a advertisement and not the other ?

Example declaration of my both services in seperate files 

  • Hi Deep Patel, 

    It's actually possible to do so. You can have a look at gatt pool library here. I haven't used it myself but as far as I can see you can find en example of using it inside \nrf\subsys\bluetooth\services\hids.c 

    Note that changing the attribute table dynamically requires you to send a service change indication to tell the peer device (the phone) to do a service discovery to update the table. 

    Normally in many application we use fixed attribute table. Meaning if the service is not needed for the operation you simply keep it there and remove any function on the service. This normally doesn't affect the end user. In most case you have your own app on the phone that can hide the service that is not needed for certain mode of operation. Most of the time the end user wouldn't be able to see the service/characteristic in raw. 

  • Hello Hung Bai,

    Probably the end user using the mobile phone application (GATT Client), will only see the service which it is looking for.

    But. for testing purposes i am using nRF Connect  Mobile App as a sniffer (scanner), and that sees all services and their characteristics (not related to the advertiser) defined in the GATT Server database, which i don't want to be exposed. Let me look into your example if that helps me.

    Thanks.

  • I would suggest to also look at this library: \zephyr\subsys\bluetooth\mesh\pb_gatt_srv.c

    In the library we use CONFIG_BT_GATT_DYNAMIC_DB and use bt_gatt_service_register() and bt_gatt_service_unregister() to update the ATT table. 


    But as I mentioned, this will cause more complex situation when the phone may confuse with the current ATT table if it's not properly handled. So to make it more simple and robust I would suggest to keep the ATT table static. 

  • Hello Hung .

    What do you mean by your statement : 

    "Meaning if the service is not needed for the operation you simply keep it there and remove any function on the service"

    Please explain in detail.

    I dont mind having a static ATT table (with two or more service definitions .. example Service A, Service B, etc)

    Just want to know how to keep things in code,

    such that if USER A Mobile Client.. when connected for Service A, if by accidentally it tries to access (read / write) Service B, what to do ?

  • Hi Deep Patel, 

    What I meant was that you keep the declaration of different services in your device. 

    Then you can define different "states" of operation where a service would operate or not. 

    So in the code of a service you can have an "if" to check the state, if the state is "A" for example then you will return the correct value in a read call back, or do notification etc. If the state is "B" then you will not return correct value and only return 0. And you don't do any notification, don't react to a write command etc. 

    On the opposite, with another service, the behavior can be different, you react correctly if the state is "B". 

    You can use event module to notify to the service about different state changes. 

Related