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.
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.
Hello satheesh
When using one of the ble_peripheral examples the following changes can be made to make it start advertising on a long button press.
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.
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.
#define MYADVBUTTON 2
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.
err_code = bsp_event_to_button_action_assign(MYADVBUTTON, BSP_BUTTON_ACTION_LONG_PUSH, BSP_EVENT_ADVERTISING_START);
APP_ERROR_CHECK(err_code);
In the bsp_event_handler add the following case
case BSP_EVENT_ADVERTISING_START:
advertising_start(false);
break;
I have here chosen to use the advertising_start function with no bonding erase. Change this according to your needs.
EDIT: Due to me misreading the question, the above activates the advertising after 1 second and not 5 seconds.
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.
Alternatively you can set up a routine yourself.
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.
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 "five_sec_timer_enabled" flag is used to keep track of whether or not the 5 second timer is currently running.
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.
At the top of main add:
#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
In the timers_init function add:
err_code = app_timer_create(&five_sec_timer_id, APP_TIMER_MODE_SINGLE_SHOT, five_sec_timeout_handler);
APP_ERROR_CHECK(err_code);
Above timers_init add the handler
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;
}
In buttons_leds_init function add:
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);
In bsp_event_handler add:
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 "normal" 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;
Best regards
Jørn Frøysa
How to ble advertising the two times continuously button pressed ?
Hi M.Praveen,
This case is ~6 years old. Please open a new case if you are having issues.