<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/123692/gatt-characteristic-value-not-maintained-if-initial-value-is-0</link><description>I am writing a custom BLE profile library and when I add new characteristics that are read write parameters and initialize the value with 0 the code seems to lose context and whenever there is a BLE update the value goes to 0xff. When i initialize it</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 14 Aug 2025 13:50:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/123692/gatt-characteristic-value-not-maintained-if-initial-value-is-0" /><item><title>RE: GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/thread/545683?ContentTypeID=1</link><pubDate>Thu, 14 Aug 2025 13:50:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aaf48400-b433-4c69-8440-db5920cb7fb8</guid><dc:creator>edmcnicholas</dc:creator><description>&lt;p&gt;Read Before Indicate&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1755179359476v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Read After&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1755179382381v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If&amp;nbsp;gSenseVal is 0 in the definition this happens if it is anything else the value is maintained i.e.&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;static&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;uint8_t&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;gSenseVal&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;0xff&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/thread/545681?ContentTypeID=1</link><pubDate>Thu, 14 Aug 2025 13:48:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8fe4135-780e-4adf-a01a-18042f8ccd90</guid><dc:creator>edmcnicholas</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 *   Copyright (c) 2024 Rogue Solutions LLC
 *   All rights reserved.
 */

#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;zephyr/init.h&amp;gt;
#include &amp;lt;zephyr/sys/__assert.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;zephyr/types.h&amp;gt;

#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;

#include &amp;quot;bleLTECwldm.h&amp;quot;
#include &amp;quot;btLTEC.h&amp;quot;

#include &amp;lt;zephyr/logging/log.h&amp;gt;
LOG_MODULE_REGISTER(bt_wldm, LOG_LEVEL_INF);

/*******************************************************************************
* Defines 
*******************************************************************************/



/*******************************************************************************
* Private Declerations 
*******************************************************************************/
static int wldm_init(void);

static void hit_ccc_cfg_changed(const struct bt_gatt_attr* attr, uint16_t value);

static void indicate_cb(struct bt_conn* conn, struct bt_gatt_indicate_params* params, unsigned char err);

static ssize_t read_hit(struct bt_conn* conn, const struct bt_gatt_attr* attr, void* buf, uint16_t len, uint16_t offset);

static ssize_t read_sense(struct bt_conn* conn, const struct bt_gatt_attr* attr, void* buf, uint16_t len, uint16_t offset);

static ssize_t write_sense(struct bt_conn* conn, const struct bt_gatt_attr* attr, const void* buf, uint16_t len, uint16_t offset,
                           uint8_t flags);

/*******************************************************************************
* Private Variables 
*******************************************************************************/

/* Custom Service Variables */
static struct bt_uuid_128 BT_UUID_MILES       = LTEC_UUID_INIT(MILES_UUID_SERVICE);
static struct bt_uuid_128 BT_UUID_MILES_HIT   = LTEC_UUID_INIT(MILES_UUID_HIT_CHAR);
static struct bt_uuid_128 BT_UUID_MILES_SENSE   = LTEC_UUID_INIT(MILES_UUID_SENSITIVITY_CHAR);

/* Indication parameters for MILES HIT char */
static struct bt_gatt_indicate_params g_hit_char_ind_params = {0};
static volatile bool g_b_is_hit_ind_enabled                 = false;

static struct ble_wldm_event g_miles_event = {
    .u16Pid         = 0,
    .u8BasicCode    = 0,
    .u8AmmoCode     = 0,
    .u8NumWords     = 0,
    .u8DetectorType = 0,
};

static struct wldmSenseWork gSenseWork = {0};

static uint8_t gSenseVal = 0; // Default to 127, which is the max sensitivity.
/***************************************************************************************************************************************
Profile Definition
***************************************************************************************************************************************/

BT_GATT_SERVICE_DEFINE(
    wldm, BT_GATT_PRIMARY_SERVICE(&amp;amp;BT_UUID_MILES.uuid),
    BT_GATT_CHARACTERISTIC(&amp;amp;BT_UUID_MILES_HIT.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_INDICATE,
                           BT_GATT_PERM_READ, read_hit, NULL, &amp;amp;g_miles_event), 
    BT_GATT_CCC(hit_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
    BT_GATT_CHARACTERISTIC(&amp;amp;BT_UUID_MILES_SENSE.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE ,
                                              BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_sense, write_sense, NULL));

/*******************************************************************************
* Public Functions 
*******************************************************************************/

int bt_wldm_report_hit(uint16_t uPid, uint8_t u8BasicCode, uint8_t u8AmmoCode, uint8_t u8NumWords, uint8_t u8DetectorType)
{
    int rc;
    g_miles_event.u16Pid         = uPid;
    g_miles_event.u8BasicCode    = u8BasicCode;
    g_miles_event.u8AmmoCode     = u8AmmoCode;
    g_miles_event.u8NumWords     = u8NumWords;
    g_miles_event.u8DetectorType = u8DetectorType;


    // Don&amp;#39;t try to indicate if indications haven&amp;#39;t been enabled.
    if (!g_b_is_hit_ind_enabled)
    {
        LOG_DBG(&amp;quot;Not indicating because indications are not enabled&amp;quot;);
        return 0;
    }

    // ind_params.attr MUST be set to the GATT service attribute if the characteristic attribute
    // being indicated has a shared UUID (e.g. LTEC_PID_CHAR_UUID). This tells the BT stack where to
    // start looking for the characteristic attribute with the uuid ind_params.uuid value.
    g_hit_char_ind_params.attr = attr_wldm;
    g_hit_char_ind_params.uuid = &amp;amp;BT_UUID_MILES_HIT;
    g_hit_char_ind_params.func = indicate_cb;
    g_hit_char_ind_params.data = &amp;amp;g_miles_event;
    g_hit_char_ind_params.len  = sizeof(g_miles_event);

    
    rc = bt_gatt_indicate(NULL, &amp;amp;g_hit_char_ind_params);
    if (rc)
    {
        LOG_WRN(&amp;quot;Hit Indicate failed: %d&amp;quot;, rc);
    }

    return rc == -ENOTCONN ? 0 : rc;

	// return rc == -ENOTCONN ? 0 : rc;
}

int bt_wldm_SetSensitivtyHandler(k_work_handler_t handler)
{
    k_work_init(&amp;amp;gSenseWork.work, handler);
}


int bt_wldm_SetSensitivty(uint8_t uLvl)
{
    gSenseVal = uLvl;
}

/*******************************************************************************
* Private Functions  
*******************************************************************************/

static int wldm_init(void)
{
    LOG_DBG(&amp;quot;BT WLDM Init&amp;quot;);
    gSenseVal =0;
	return 0;
}

static void hit_ccc_cfg_changed(const struct bt_gatt_attr* attr, uint16_t value)
{
    ARG_UNUSED(attr);

    bool ind_enabled = (value == BT_GATT_CCC_INDICATE);

    g_b_is_hit_ind_enabled = ind_enabled;

    LOG_INF(&amp;quot;MILES Hit Indication %s&amp;quot;, ind_enabled ? &amp;quot;enabled&amp;quot; : &amp;quot;disabled&amp;quot;);
}

static void indicate_cb(struct bt_conn* conn, struct bt_gatt_indicate_params* params, unsigned char err)
{
    LOG_INF(&amp;quot;MILES Hit Indication %s&amp;quot;, err != 0U ? &amp;quot;fail&amp;quot; : &amp;quot;success&amp;quot;);
    // todo handle
}

/* BLE Read Functions */

static ssize_t read_hit(struct bt_conn* conn, const struct bt_gatt_attr* attr, void* buf, uint16_t len, uint16_t offset)
{
    return bt_gatt_attr_read(conn, attr, buf, len, offset, &amp;amp;g_miles_event, sizeof(g_miles_event));
}

static ssize_t read_sense(struct bt_conn* conn, const struct bt_gatt_attr* attr, void* buf, uint16_t len, uint16_t offset)
{
    // uint8_t status = gSenseVal;

    return bt_gatt_attr_read(conn, attr, buf, len, offset, &amp;amp;gSenseVal, sizeof(gSenseVal));
}

/* BLE Write Functions */

static ssize_t write_sense(struct bt_conn* conn, const struct bt_gatt_attr* attr, const void* buf, uint16_t len, uint16_t offset,
                           uint8_t flags)
{
    struct net_buf_simple data;

    if (flags &amp;amp; BT_GATT_WRITE_FLAG_PREPARE)
    {
        return 0;
    }

    if (offset + len &amp;gt; sizeof(gSenseVal))
    {
        return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
    }

    net_buf_simple_init_with_data(&amp;amp;data, (void *)buf, len);
	gSenseVal = net_buf_simple_pull_u8(&amp;amp;data);

    LOG_INF(&amp;quot;Sense Write %x&amp;quot;, gSenseVal);
    /* Submit Reset Work */
    if(gSenseWork.work.handler != 0)
    {
        gSenseWork.uLevel = gSenseVal;
        k_work_submit(&amp;amp;gSenseWork);
    }
    return len;
}


SYS_INIT(wldm_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/thread/545666?ContentTypeID=1</link><pubDate>Thu, 14 Aug 2025 12:38:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42bf3e6b-6510-42b1-a664-2c1a935fc08d</guid><dc:creator>edmcnicholas</dc:creator><description>&lt;p&gt;I have another characteristic that I notify in.&amp;nbsp; This happens to a second profile whenever I notify.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/thread/545665?ContentTypeID=1</link><pubDate>Thu, 14 Aug 2025 12:35:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff777119-ce59-467f-9cce-34e28a0b86e7</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
[quote user=""]whenever there is a BLE update the value goes to 0xff.&amp;nbsp;[/quote]
&lt;p&gt;What do you mean by BLE update?&lt;/p&gt;
&lt;p&gt;Also, could you post some more code that shows&amp;nbsp;what you are doing with this&amp;nbsp;&lt;span&gt;variable ?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: GATT characteristic value not maintained if initial value is 0</title><link>https://devzone.nordicsemi.com/thread/545594?ContentTypeID=1</link><pubDate>Thu, 14 Aug 2025 00:46:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f50bf474-d6af-4efc-a3ce-f13f0ebdf286</guid><dc:creator>edmcnicholas</dc:creator><description>&lt;p&gt;New fun fact it only happens to&amp;nbsp;8 bit variables or structures&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>