<?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>BLE app Blinky - How do I assign the initial LED characteristic value as it is not initially defined (default is 0 / LED disabled)</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/83752/ble-app-blinky---how-do-i-assign-the-initial-led-characteristic-value-as-it-is-not-initially-defined-default-is-0-led-disabled</link><description>I have modified the BLE app Blinky example so that the LED is enabled (state 1) on power up. However, this is not being reflected on the BLE side because when I connect with the device and read the LED characteristic this still indicates that the LED</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 18 Jan 2022 17:57:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/83752/ble-app-blinky---how-do-i-assign-the-initial-led-characteristic-value-as-it-is-not-initially-defined-default-is-0-led-disabled" /><item><title>RE: BLE app Blinky - How do I assign the initial LED characteristic value as it is not initially defined (default is 0 / LED disabled)</title><link>https://devzone.nordicsemi.com/thread/348374?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 17:57:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65c3e393-d838-486c-a812-dff9ef118fa3</guid><dc:creator>Gerrikoio</dc:creator><description>[quote userid="26071" url="~/f/nordic-q-a/83752/ble-app-blinky---how-do-i-assign-the-initial-led-characteristic-value-as-it-is-not-initially-defined-default-is-0-led-disabled/348307#348307"]My guess is that you only set the LED, but try to add:[/quote]
&lt;p&gt;Yes your guess is correct. I indeed wanted to change the code as you&amp;#39;ve suggested but as the example was developed by someone else, I wanted to understand how best to implement before I created a mess hacking.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="26071" url="~/f/nordic-q-a/83752/ble-app-blinky---how-do-i-assign-the-initial-led-characteristic-value-as-it-is-not-initially-defined-default-is-0-led-disabled/348307#348307"]Another thing that comes&amp;nbsp;is that when you connect, do you actively read the LED status?[/quote]
&lt;p&gt;Yes I actively read the status when I need to. The important part I need is determining the initial LED status. If you do first write to the LED characteristic it will always be undefined or 0.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Anyhow, it is amazing how the brain works. Start by taking time to explain the problem (as I did above), then sleep on it, and the next day the problem becomes so much clearer... and a solution appears.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So I have just come up with an implementation&lt;/strong&gt;, which seems clean enough but it does involve modifying 3 files.&lt;/p&gt;
&lt;p&gt;1. I modifed the typedef struct &amp;quot;ble_lbs_init_t&amp;quot; as found in ble_lbs.h&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/** @brief LED Button Service init structure. This structure contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
    ble_lbs_led_write_handler_t led_write_handler; /**&amp;lt; Event handler to be called when the LED Characteristic is written. */
    uint8_t initial_LED_state[1];
} ble_lbs_init_t;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;2. Then I added an initial LED state within main.c via the services_init function&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Function for initializing services that will be used by the application.
 */
static void services_init(void)
{
    ret_code_t         err_code;
    ble_lbs_init_t     init     = {0};
    nrf_ble_qwr_init_t qwr_init = {0};

    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&amp;amp;m_qwr, &amp;amp;qwr_init);
    APP_ERROR_CHECK(err_code);

    // Initialize LBS.
    init.led_write_handler = led_write_handler;
    init.initial_LED_state[0] = 1;        // Set to match the LED state set on board power up

    err_code = ble_lbs_init(&amp;amp;m_lbs, &amp;amp;init);
    APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;3. Finally I added the additional &amp;quot;add_char_params.p_init_value&amp;quot; within ble_lbs_init function found in ble_lbs.c&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
{

.....

    // Add LED characteristic.
    memset(&amp;amp;add_char_params, 0, sizeof(add_char_params));

    add_char_params.uuid             = LBS_UUID_LED_CHAR;
    add_char_params.uuid_type        = p_lbs-&amp;gt;uuid_type;
    add_char_params.init_len         = sizeof(uint8_t);
    add_char_params.max_len          = sizeof(uint8_t);
    add_char_params.p_init_value     = p_lbs_init-&amp;gt;initial_LED_state;
    add_char_params.char_props.read  = 1;
    add_char_params.char_props.write = 1;
    
.....
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE app Blinky - How do I assign the initial LED characteristic value as it is not initially defined (default is 0 / LED disabled)</title><link>https://devzone.nordicsemi.com/thread/348307?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 14:00:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00d6eb95-4c8a-4988-b842-af41f22b885d</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user=""]I have modified the &lt;strong&gt;BLE app Blinky example&lt;/strong&gt; so that the LED is enabled (state 1) on power up.[/quote]
&lt;p&gt;What did you do here? Did you just toggle the LED, or did you set the actual state of the characteristic?&lt;/p&gt;
&lt;p&gt;My guess is that you only set the LED, but try to add:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;add_char_params.p_init_value = &amp;amp;my_value;
add_char_params.init_len     = my_len;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;inside&amp;nbsp;ble_lbs_init().&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Another thing that comes&amp;nbsp;is that when you connect, do you actively read the LED status?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>