This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to change from BLE central to peripheral mode by buttons

Hi, I was wondering how to change from central mode of the NRF52 board to peripheral mode and from peripheral mode to central mode by pressing the input buttons on the board. For example, button 1 would enable the central mode which is when the NRF52 board is acting as the client and wants to send data and then button 2 would enable the peripheral mode when the NRF52 board is advertising to be connected to another BLE device, but then disable the central mode which is button 1.

Are there any examples related to this? Any help would be appreciated.

Parents
  • Hi,

    The S130 (nRF51) and S132 (nRF52) softdevices are capable of running concurrently in peripheral and central mode with up to 8 individual links (up to 20 in latest S132 v4.0.0 alpha release). You are therefore not required to switch from central to peripheral mode and back, but you can of course do this if you want to. It is also possible to configure button presses to start advertising/scanning for a predefined period of time if this is what you want.

    I recommand that you take a look at the BLE Relay example and the BLE LE Secure Connections multirole example, which both use concurrent paripheral and central mode.

    Best regards,

    Jørgen

  • It should be possible, but we don't have any examples showing this. You will have to initialize buttons in main, and do all initialization of the program you want to run, and shutdown of the currently running program, in the button event handler (you can also use a flag and do initialization in main based on the set flag). I attached pseudocode to show principle:

    enum OP_MODE {INIT,CENTRAL,PERIPHERAL);
    OP_MODE mode = INIT;
    
    button_event_handler()
    {
    	switch (button_evt)
    	case BUTTON_0: //Switch to Central mode
    		if(mode == PERIPHERAL)
    		{
    			advertising_stop();
    		}
    		init_central();
    		start_scan();
    		mode = CENTRAL;
    	break;
    	
    	case BUTTON_1: //Switch to Peripheral mode
    		if(mode == CENTAL)
    		{
    			scan_stop();
    		}
    		init_peripheral();
    		advertising_start();
    		mode = PERIPHERAL;
    	break;
    	
    	case BUTTON_2: //Turn off both modes
    		if(mode == CENTAL)
    		{
    			advertising_stop();
    		}
    		else if(mode == PERIPHERAL)
    		{
    			scan_stop();
    		}
    		mode = INIT;
    	break;
    	
    	default:
    	break;
    }
    
    main()
    {
    	buttons_init();
    	ble_stack_init();
    	while(1)
    	{
    		sd_app_evt_wait();
    	}
    }
    
Reply
  • It should be possible, but we don't have any examples showing this. You will have to initialize buttons in main, and do all initialization of the program you want to run, and shutdown of the currently running program, in the button event handler (you can also use a flag and do initialization in main based on the set flag). I attached pseudocode to show principle:

    enum OP_MODE {INIT,CENTRAL,PERIPHERAL);
    OP_MODE mode = INIT;
    
    button_event_handler()
    {
    	switch (button_evt)
    	case BUTTON_0: //Switch to Central mode
    		if(mode == PERIPHERAL)
    		{
    			advertising_stop();
    		}
    		init_central();
    		start_scan();
    		mode = CENTRAL;
    	break;
    	
    	case BUTTON_1: //Switch to Peripheral mode
    		if(mode == CENTAL)
    		{
    			scan_stop();
    		}
    		init_peripheral();
    		advertising_start();
    		mode = PERIPHERAL;
    	break;
    	
    	case BUTTON_2: //Turn off both modes
    		if(mode == CENTAL)
    		{
    			advertising_stop();
    		}
    		else if(mode == PERIPHERAL)
    		{
    			scan_stop();
    		}
    		mode = INIT;
    	break;
    	
    	default:
    	break;
    }
    
    main()
    {
    	buttons_init();
    	ble_stack_init();
    	while(1)
    	{
    		sd_app_evt_wait();
    	}
    }
    
Children
No Data
Related