<?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>How to send a byte array by battery level notification</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/34567/how-to-send-a-byte-array-by-battery-level-notification</link><description>Dear Sir/Madam, 
 Normally battery level notification is only one byte. But I need send byte array of 58 bytes through battery level notifications in my project. I have to do this because one obsolete product in my company does this and I have to keep</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 11 Jun 2018 05:58:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/34567/how-to-send-a-byte-array-by-battery-level-notification" /><item><title>RE: How to send a byte array by battery level notification</title><link>https://devzone.nordicsemi.com/thread/135469?ContentTypeID=1</link><pubDate>Mon, 11 Jun 2018 05:58:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b6a0c31d-63cf-469d-85ad-55dc7e261220</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/jgongbitswave"&gt;jacky&lt;/a&gt;: Have you set the size of the characteristic to 20 bytes? Please post the code where you add the battery_level characteristic.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to send a byte array by battery level notification</title><link>https://devzone.nordicsemi.com/thread/135442?ContentTypeID=1</link><pubDate>Sat, 09 Jun 2018 23:41:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9de9d5c-598e-429e-b52c-7c3567ea3fb0</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;&amp;quot;&lt;span&gt;simply failed to work&amp;quot; - not very much information - failed to work how, what error message, what happened?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;quot;So I guess the Nordic layerstack might have certain internal restrict .. &amp;quot; - wrong guess, the stack doesn&amp;#39;t care one jot about the data you&amp;#39;re sending out and has no knowledge of well-known services.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Bjorn gave you code, just make a 20 byte buffer, put data in it, pass it to the sending routine along with the length and it will be sent. If that doesn&amp;#39;t happen then post clearly what doesn&amp;#39;t work and what you&amp;#39;re seeing.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to send a byte array by battery level notification</title><link>https://devzone.nordicsemi.com/thread/135440?ContentTypeID=1</link><pubDate>Sat, 09 Jun 2018 22:32:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f887243-99e1-46f7-8511-ef4de5b18377</guid><dc:creator>jacky</dc:creator><description>&lt;p&gt;Hi Bjorn,&lt;/p&gt;
&lt;p&gt;I have tried a lot times, and the code simply failed to work when I tried to send 20-byte packet through the notification of battery-level characteristic. So I guess the Nordic layerstack might have certain internal restrict to this Well-known service and characteristics. &amp;nbsp; Could you verify it? &amp;nbsp;And If possible, could you send me a sample code that successfully sending out 20 byte packets through Battery_Level characteristic?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Jacky&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to send a byte array by battery level notification</title><link>https://devzone.nordicsemi.com/thread/132975?ContentTypeID=1</link><pubDate>Wed, 23 May 2018 11:53:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e4cfe0e8-01c0-43ff-8c8f-f30f64462b29</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hi Jacky,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Yes, you will need to split it into 20-byte chuncks.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;BLE characteristic only deals with octets or arrays of octets, i.e. byte-streams. If you&amp;#39;re sending something bigger than a byte, then you need to format the data so that it can be placed in a byte array.&lt;/p&gt;
&lt;p&gt;So if you&amp;nbsp; for instance want to sent a 32-bit unsigned integer, then you will have to set the size of the characteristic to 4*sizeof(uint8_t), i.e.&amp;nbsp;attr_char_value.init_len/max_len =&amp;nbsp; &lt;span&gt;4*sizeof(uint8_t) in the function that adds the characteristic&lt;/span&gt;&amp;nbsp;and then divide the 32-bit integer into four 8-bit usigned integers, e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t large_number = 64000;
uint8_t ble_data[4] = {0};&lt;br /&gt;uint16_t ble_data_len = sizeof(&lt;/code&gt;ble_data);&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;for(int i = 0; i &amp;lt; sizeof(ble_data);i++) { ble_data[i] = large_number &amp;gt;&amp;gt; (i*8); } &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Then in your function that calls&amp;nbsp;sd_ble_gatts_hvx you simply point to the byte array and specify the length of the array.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;        ble_gatts_hvx_params_t hvx_params;

        memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_cus-&amp;gt;custom_value_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = 0;
        hvx_params.p_len  = &amp;amp;ble_data_len;
        hvx_params.p_data = ble_data

        err_code = sd_ble_gatts_hvx(p_cus-&amp;gt;conn_handle, &amp;amp;hvx_params);
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>