<?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 enable after 5 sec long press button</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/21823/ble-enable-after-5-sec-long-press-button</link><description>i am very new for nordic development. i need to design bluetooth advertising enable after long press of button. can anyone help me please. 
 thanks in advance.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 06 Mar 2023 09:37:12 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/21823/ble-enable-after-5-sec-long-press-button" /><item><title>RE: ble enable after 5 sec long press button</title><link>https://devzone.nordicsemi.com/thread/413460?ContentTypeID=1</link><pubDate>Mon, 06 Mar 2023 09:37:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d07b432-0d6a-4764-929b-1b5a0eb3a76b</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi M.Praveen,&lt;/p&gt;
&lt;p&gt;This case is ~6 years old. Please open a new case if you are having issues.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ble enable after 5 sec long press button</title><link>https://devzone.nordicsemi.com/thread/413353?ContentTypeID=1</link><pubDate>Sat, 04 Mar 2023 04:19:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9bf90b60-6ca0-426c-a376-57c36a50971a</guid><dc:creator>M.Praveen</dc:creator><description>&lt;p&gt;How to ble advertising the two times continuously button pressed ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ble enable after 5 sec long press button</title><link>https://devzone.nordicsemi.com/thread/85720?ContentTypeID=1</link><pubDate>Fri, 05 May 2017 09:24:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5fd0e45b-ae84-45ff-9ef1-107388d1d562</guid><dc:creator>J&amp;#248;rn</dc:creator><description>&lt;p&gt;Hello satheesh&lt;/p&gt;
&lt;p&gt;When using one of the ble_peripheral examples the following changes can be made to make it start advertising on a long button press.&lt;/p&gt;
&lt;p&gt;NOTE:This is a very simple example, and pressing the button while it is already advertising will cause a fatal error and lead to a system reset. You could add a variable to keep track of whether your device is advertising or not, and keep that updated. You can then do a check on whether you are advertising or not before deciding if you should call the advertising_start function.&lt;/p&gt;
&lt;p&gt;First define the button you want to use, 0 is button 1, and 3 is button 4. In this case I have used button 3.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define MYADVBUTTON 2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the buttons_leds_init function add the following lines. They set up button 3 to generate the BSP_EVENT_ADVERTISING_START event when a long push is detected.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;err_code = bsp_event_to_button_action_assign(MYADVBUTTON, BSP_BUTTON_ACTION_LONG_PUSH, BSP_EVENT_ADVERTISING_START);
APP_ERROR_CHECK(err_code);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the bsp_event_handler add the following case&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;case BSP_EVENT_ADVERTISING_START:

	advertising_start(false);
						
	break;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have here chosen to use the advertising_start function with no bonding erase. Change this according to your needs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;EDIT:&lt;/strong&gt; Due to me misreading the question, the above activates the advertising after 1 second and not 5 seconds.&lt;/p&gt;
&lt;p&gt;There are two ways of achieving what you want. You could either change the long press timeout defined for the bsp module by changing the value of BSP_LONG_PUSH_TIMEOUT_MS in bsp_config.h. You can then use the same method I showed above.
Do note that this will change the timing of ALL long button presses for ALL projects using this file in the SDK.&lt;/p&gt;
&lt;p&gt;Alternatively you can set up a routine yourself.&lt;/p&gt;
&lt;p&gt;The following code will configure button 3 on the devkit to start advertisement after 5 seconds. It does this by activating a timer on button press, if the button is still pressed after 5 seconds it calls the advertising_start function. It also toggles a couple of leds for debugging reasons.&lt;/p&gt;
&lt;p&gt;I have also configured it so the release of the button calls the BSP_EVENT_KEY_4, which is normally not in use on the devkits, which stops the timer, and performs the task a normal button press should perform. Do note it will only perform this task on release of the button, when the 5 second timer has not yet timed out. The &amp;quot;five_sec_timer_enabled&amp;quot; flag is used to keep track of whether or not the 5 second timer is currently running.&lt;/p&gt;
&lt;p&gt;NOTE: As in the previous part of the answer, if advertise start is called while it is already advertising you will get a fatal error and the device will reset. I cannot guarantee if this method will work well with the long press functionality already present in the bsp module, for the same button. I think it will work, but no guarantees.&lt;/p&gt;
&lt;p&gt;At the top of main add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define MYADVBUTTON 2 //button used to start advertise

static bool five_sec_timer_enabled = false; //flag for monitoring timer

APP_TIMER_DEF(five_sec_timer_id); //define 5 second timer id
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the timers_init function add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;err_code = app_timer_create(&amp;amp;five_sec_timer_id, APP_TIMER_MODE_SINGLE_SHOT, five_sec_timeout_handler); 
APP_ERROR_CHECK(err_code);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Above timers_init add the handler&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void five_sec_timeout_handler(void * p_context)
{
	
	UNUSED_PARAMETER(p_context);
	
	if(nrf_gpio_pin_read(BSP_BUTTON_2)==0) //if button is still pressed
	{
		nrf_gpio_pin_toggle(BSP_LED_2);
		advertising_start(false);
	}
	five_sec_timer_enabled=false;
	
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In buttons_leds_init function add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;err_code = bsp_event_to_button_action_assign(MYADVBUTTON, BSP_BUTTON_ACTION_RELEASE, BSP_EVENT_KEY_4); //add release action trigger on the button. call BSP_EVENT_KEY_4 event
APP_ERROR_CHECK(err_code);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In bsp_event_handler add:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;case BSP_EVENT_KEY_2:
err_code = app_timer_start(five_sec_timer_id, APP_TIMER_TICKS(5000), NULL); //start timer, 5 second timeout
APP_ERROR_CHECK(err_code);
five_sec_timer_enabled=true;

break;
case BSP_EVENT_KEY_4:
if(five_sec_timer_enabled) //if timer is running, stop timer, perform wanted action for &amp;quot;normal&amp;quot; press. In this case toggle led.
{
app_timer_stop(five_sec_timer_id);
nrf_gpio_pin_toggle(BSP_LED_3);
five_sec_timer_enabled=false;
}

break;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Best regards&lt;/p&gt;
&lt;p&gt;Jørn Frøysa&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>