How to show different services in 2 different advertising?

Hello DevZone,

I'm developing a HomeKit project for the customer. They require 2 BLE advertising in one device. One advertising is for HomeKit over BLE, and the other one is for their custom BLE service.

Environment: NCS v2.5.3 with HomeKit repo.

Hardware: nRF52840

What I have done:

I modified the HomeKit library. I changed the code in `homekit/adk/PAL/nRFConnect/HAPPlatformBLE.c` and `homekit/adk/PAL/nRFConnect/HAPPlatformBLEPeripheralManager.c`

Basically, I changed from `bt_le_adv_start()` to `bt_le_ext_adv_start()`.

Then I start my own custom BLE advertising by `bt_le_ext_adv_start()`, too.

I also modified all relative code to make these 2 advertising to be able to work together. They have different `.connected()` callback. So, when a connection is established, I will know which advertising is this connection from.

Issue:

When I connect to any of the advertising by using nRF connect APP, I can see all the BLE services on nRF Connect APP. including all Homekit services.

I don't want to see the HomeKit services when I connect to my custom BLE advertising. On the other hand, I also don't want to see my custom services when I connect to the HomeKit advertising.

I tried method like below:

ssize_t my_bt_gatt_attr_read_chrc(struct bt_conn *conn,
                const struct bt_gatt_attr *attr,
                void *buf, uint16_t len, uint16_t offset)
{
    if (conn != blePeripheralManagerRef->conn) {
        LOG_WRN("%s Not the current connection", __func__);
        return BT_GATT_ERR(BT_ATT_ERR_INVALID_HANDLE);
    }

    return bt_gatt_attr_read_chrc(conn, attr, buf, len, offset);
}

This can prevent the non-HomeKit connection to read/write the HomeKit attribytes. But I'm not sure if it is a good way.

Best regards,

Jayant

  • Jayant, 

    that is exactly what you should do if you do not want some other services to be read by peers. During the discovery procedure, the peer tries to read everything in the attribute table by default. The additional check you added in the read_chrc callback is a good way to check that you only allow the peer to read attributes relevant to that connection only. 

  • Hello Susheel,

    Thank you for your reply. Now I'm using this method, and I modified the error return value:

    return BT_GATT_ERR(BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
     

    If I don't use this return value, the nRF Connect APP on Android Phone will be not able to read all services.

    And I finally find a better way, it's to dynamically register and unregister my services. Using `bt_gatt_service_register` and `bt_gatt_service_unregister` in my connected and disconnected callbacks.

    Best regards,

    Jayant

Related