<?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 make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/86418/how-to-make-nrf52832-to-first-advertise-for-30secs-then-scan-for-30secs-and-repeat-this-cycle</link><description>Hello Everyone, 
 For my project, I have to make my BLE device to advertise itself for a period of time and transit to &amp;quot;scan&amp;quot; mode to scan for other device for a period of time. 
 This will be in a repeated cycle until the battery dies. 
 I&amp;#39;ve attached</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 06 Apr 2022 09:39:06 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/86418/how-to-make-nrf52832-to-first-advertise-for-30secs-then-scan-for-30secs-and-repeat-this-cycle" /><item><title>RE: How to make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/thread/362022?ContentTypeID=1</link><pubDate>Wed, 06 Apr 2022 09:39:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a2b92d99-22a3-4571-9b85-e788ab4cac29</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So in your app_timer timeout handler, you will stop the advertisement, but it is still visible in nRF Connect, is that what you are saying?&lt;/p&gt;
&lt;p&gt;As long as you tell it to stop advertising, it will do so, but if nRF Connect has already started scanning and picked up the advertisements then it will not automatically disappear. You should see that if you clear the scan buffer (start scanning again), it will not reappear. Let me know what version (Desktop/mobile) you are using if you can&amp;#39;t see how to clear the buffer.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="Zachary16"]I have also received 3 warnings, 1 stating that the &amp;#39;NRF_BLE_SCAN_SCAN_DURATION has been redefined&amp;#39; and the remaining&amp;nbsp;warning states &amp;#39;passing argument 3 of &amp;#39;app_timer_create&amp;#39; from incompatible pointer type [-Wincompatible-pointer-types]&amp;#39;.&amp;nbsp;[/quote]
&lt;p&gt;Where did you define NRF_BLE_SCAN_SCAN_DURATION? I guess you have one instance in sdk_config.h and one in main.c, is that correct? There should only be one instance, so you should remove one of them. I think the default placement is sdk_config.h for this one, but it is up to you where you define it.&lt;/p&gt;
&lt;p&gt;What is the 3rd parameter you use in app_timer_create?&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><item><title>RE: How to make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/thread/361776?ContentTypeID=1</link><pubDate>Tue, 05 Apr 2022 09:18:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4a139fe-cf65-49d1-b260-1ebcbd4e63d6</guid><dc:creator>Zachary16</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;Thank you for your prompt and kind reply.&amp;nbsp;Apologies for the late reply, I was caught up with other projects.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve tried your recommendation, however, it seems like the app_timer would eventually overwrites the initial timers that were defined for advertising and scanning. (#define&amp;nbsp;&lt;span&gt;APP_ADV_DURATION &amp;amp; #define&amp;nbsp;NRF_BLE_SCAN_SCAN_DURATION) and will continue to toggle between scan and advertising mode (it will turn on scan led but advertising itself on the nRF connect app).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;What I&amp;#39;ve done:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;created APP_TIMER_DEF for both scan and advertise respectively&lt;/li&gt;
&lt;li&gt;implemented app_timer_create in the timer_init() and callback to the event handler respectively&lt;/li&gt;
&lt;li&gt;created application_timer_start for both scan and adv using APP_TIMER_TICKS to set the time respectively&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What I didn&amp;#39;t do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;creating a separate/new timeout handler for the app_timer_create, I called back the old &amp;#39;scan_evt_handler&amp;#39; and &amp;#39;on_adv_evt&amp;#39;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Was I supposed to create a new sets of timeout handler for the timer_init? If so, how do I go about with creating a new set of timeout handler? Will it eventually overwrites the initial handlers (scan_evt_handler &amp;amp; on_adv_evt) or this app_timer will run simultaneously with the initial timer and callback handler?&lt;/p&gt;
&lt;p&gt;I have also received 3 warnings, 1 stating that the &amp;#39;NRF_BLE_SCAN_SCAN_DURATION has been redefined&amp;#39; and the remaining&amp;nbsp;warning states &amp;#39;passing argument 3 of &amp;#39;app_timer_create&amp;#39; from incompatible pointer type [-Wincompatible-pointer-types]&amp;#39;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Zachary&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/thread/361041?ContentTypeID=1</link><pubDate>Thu, 31 Mar 2022 13:00:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:03be7df8-e6de-4371-9858-20a5aca3bbb2</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello Zachary,&lt;/p&gt;
&lt;p&gt;There is no API to do exactly this, no. If you want this, you need to keep track of time yourself from the application. What you can do is e.g. to use the app_timer. I think, since you just want to toggle the switch between advertising and scanning every 30ms, regardless of connections, then you can just start a repeated app_timer that gives a timeout callback every 30 seconds, and start/stop advertising scanning manually from there.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For an example that uses the app_timer, you can look at the SDK\examples\ble_peripheral\ble_app_hrs, which uses an app_timer to update the simulated &amp;quot;remaining battery service&amp;quot; every 2 seconds or so.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Relevant APIs for scanning and advertising:&lt;/p&gt;
&lt;p&gt;sd_ble_gap_adv_stop() for stopping advertisements (you can see how it is used in ble_advertising_restart_without_whitelist() in ble_advertising.c.)&lt;/p&gt;
&lt;p&gt;sd_ble_gap_scan_stop() (or nrf_ble_scan_stop() in nrf_ble_scan.c, which just calls the softdevice call directly)&lt;/p&gt;
&lt;p&gt;Relevant APIs for app_timer:&lt;/p&gt;
&lt;p&gt;APP_TIMER_DEF()&lt;/p&gt;
&lt;p&gt;app_timer_create()&lt;/p&gt;
&lt;p&gt;app_timer_start()&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Then you just start the advertisements and scanning with a higher timeout than 30 seconds, to make sure that they don&amp;#39;t time out before the 30 second mark, so that you have one (two) less timeout callbacks to worry about.&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><item><title>RE: How to make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/thread/360978?ContentTypeID=1</link><pubDate>Thu, 31 Mar 2022 10:26:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9c4f6cfe-e3bc-49bb-8b5c-552c84ad2852</guid><dc:creator>Zachary16</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;Yes, I&amp;#39;m using nRF5 DK.&lt;/p&gt;
&lt;p&gt;Thank you so much for the clear explanation! It really helps me a lot!&lt;/p&gt;
&lt;p&gt;*edit: I have another question:&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve establish a connection between&amp;nbsp;a device and the&amp;nbsp;development kit during its advertising period for x amount of time, and I&amp;#39;ve realised that once I&amp;#39;ve disconnected my device from the development kit, the development kit will &amp;quot;reset&amp;quot; the advertising duration. Is it possible for the development kit to continue the advertising duration right after disconnection?&lt;br /&gt;&lt;br /&gt;E.g.:&lt;br /&gt;Development kit programmed to advertise for 30 sec and scan for 15 sec.&lt;br /&gt;Connect device to development kit&amp;nbsp; at the 5th second of its advertising duration.&lt;br /&gt;Disconnect device from development kit after 15 seconds. (total duration taken 20sec).&lt;br /&gt;Development kit continues to advertise from where it stops/pause (20th sec onwards) for another 10sec (total 30sec) then switch to scan again.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Zachary&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to make nRF52832 to first advertise for 30secs then scan for 30secs and repeat this cycle</title><link>https://devzone.nordicsemi.com/thread/360890?ContentTypeID=1</link><pubDate>Wed, 30 Mar 2022 17:43:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d043bf04-57f5-4632-b8e0-4c9677243981</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Are you using the nRF5 SDK or NCS? Based on your case history, I am going to guess nRF5 SDK.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you look at one of the examples that supports both advertising and scanning, such as the SDK\examples\ble_central_and_peripheral\experimental\ble_app_hrs_rscs_relay, you can see that by default, the advertising_init() function sets the advertising timeout to APP_ADV_DURATION, which is set to 180 seconds by default. You can change this to 30.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When the advertising times out after APP_ADV_DURATION, it will trigger an event in on_adv_evt with the case BLE_ADV_EVT_IDLE. You can use this event to trigger the start of the scanning. Then, if you look in the scan_init() function you can see that it uses the scan parameter m_scan_param, which is defined further up in the main.c file. Here, the timeout is set to NRF_BLE_SCAN_SCAN_DURATION, which is the time that the scanning will go on. This particular parameter is set in sdk_config.h (but you can use a parameter defined in main.c as well, if you prefer that. By default, this is set to 0, which means that it will scan &amp;quot;forever&amp;quot;. But you can set it to any value. This parameter is given in units of 10ms.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When (if) the scanner times out, it will trigger an event in the&amp;nbsp;scan_evt_handler() callback function with the scan_evt_id&amp;nbsp;&amp;nbsp;NRF_BLE_SCAN_EVT_SCAN_TIMEOUT. You can use this to start advertising when you are done scanning.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Then you have the function&amp;nbsp;adv_scan_start() that currently starts both advertising and scanning. Use it to start only one of them, and then use the timeout events to start the other.&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>