본문으로 바로가기

[NRF52] ble_app_blinky 프로젝트에 multi link 추가하기

category Nordic_nRF52 2023. 6. 27. 10:34

 : ble_app_blinky 프로젝트는 한개의 Central 에 대한 LED on/off 및 Button 상태를 읽는 프로젝트인데

   2개의 Central 에 연결시키고 LED on/off 제어및 버튼 notification 이 동작하게 만들어 보겠습니다.

 

< 준비사항 >

 → 스마트폰 2대에 nRF Connect 앱 설치하기 

 

 

▶ sdk_config.h 파일의 LINK_COUNT 관련 2가지 값을 변경합니다.

+#define NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 2
~~ 중략 ~~
+#define NRF_SDH_BLE_TOTAL_LINK_COUNT 2

 

connection 이벤트 발생시 peripheral link 의 갯수를 읽어서 advertising을 시작해 줍니다.

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
        {
         	uint32_t cnt = ble_conn_state_peripheral_conn_count(); // Number of peripheral links.
			~~ 중략 ~~
			if(cnt == NRF_SDH_BLE_PERIPHERAL_LINK_COUNT){
				 NRF_LOG_INFO("max connection.");
			}else	
				advertising_start();
            break;
        }

 

==> 위 2가지만 해도 LED on/off 제어는 동작합니다.

 

Button notification 추가하기

   : Button1 을 누를경우 2개의 연결된 Central 장치에 버튼상태 전송하기

     버튼 누를때 이벤트 전송은 button_event_handler() 함수를 사용합니다.      


#include "ble_conn_state.h"
~~ 중략 ~~
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    switch (pin_no)
    {
        case LEDBUTTON_BUTTON:
            ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_periph_handles();
            for (uint8_t i = 0; i < conn_handles.len; i++){
                err_code = ble_lbs_on_button_change(conn_handles.conn_handles[i], &m_lbs, button_action);
            }
            break;
        ~~ 중략 ~~
    }
}

 

< 테스트 >

   nRF Connect 앱을 사용해 2개의 연결 링크를 만듭니다.

 

   아래 그림의 Button 관련 notification을 Enable 해주고

       LED 특성은 write를 통해서 ON/OFF 테스트해 보면 됩니다.

 

   정상 작동시 화면

 

 

참고용 자료 입니다.

그럼 수고하세요.

 

 

 

 

반응형