diff --git a/include/bluetooth/gatt.h b/include/bluetooth/gatt.h index 13cf4baa610bb..1ad9ea79d72da 100644 --- a/include/bluetooth/gatt.h +++ b/include/bluetooth/gatt.h @@ -1278,6 +1278,20 @@ enum { */ BT_GATT_SUBSCRIBE_FLAG_VOLATILE, + /** No resubscribe flag + * + * By default when BT_GATT_SUBSCRIBE_FLAG_VOLATILE is unset, the + * subscription will be automatically renewed when the client + * reconnects, as a workaround for GATT servers that do not persist + * subscriptions. + * + * This flag will disable the automatic resubscription. It is useful + * if the application layer knows that the GATT server remembers + * subscriptions from previous connections and wants to avoid renewing + * the subscriptions. + */ + BT_GATT_SUBSCRIBE_FLAG_NO_RESUB, + /** Write pending flag * * If set, indicates write operation is pending waiting remote end to diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index 3f4959c0e15dd..bd3e6ea395084 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -3579,11 +3579,15 @@ static void add_subscriptions(struct bt_conn *conn) continue; } - /* Force write to CCC to workaround devices that don't track - * it properly. - */ - gatt_write_ccc(conn, params->ccc_handle, params->value, - gatt_write_ccc_rsp, params); + if (bt_addr_le_is_bonded(conn->id, &conn->le.dst) && + !atomic_test_bit(params->flags, + BT_GATT_SUBSCRIBE_FLAG_NO_RESUB)) { + /* Force write to CCC to workaround devices that don't + * track it properly. + */ + gatt_write_ccc(conn, params->ccc_handle, params->value, + gatt_write_ccc_rsp, params); + } } }