[ NXP BLE KW38 ] 動態修改 BLE 連接間隔

一、簡介

BLE 產品開發過程中, 在已連線階段 ( Connected ) 經常需要因應不同的情境,在 BLE 不斷線的情況下進行動態修改連接間隔 ( Connection Interval ) ,本文將使用 KW38 開發板 ( FRDM-KW38 ) 建立一個 BLE 動態修改連接間隔的範例做個分享.

接下來會使用到兩片 KW38 開發板, 利用 USB 與電腦相互連接,透過 wireless_uart_bm 原廠範例增加動態修改 BLE 連接間隔 ( Connection Interval ) 的功能.

操作方面也十分簡單, 當兩塊開發板上電後, 分別選擇 Advertiser 與 Scanner ,之後按下Advertiser 開發板上的硬體按鈕 ( 編號為 SW2 ) , 就能傳送需要更新 BLE 動態間隔的請求,待 Scanner 接收並審核通過後就能使用新的 BLE 連接間隔來做接下來雙方的資料傳輸.

關於 BLE 連接間隔數值方面, 在原廠 wireless_uart_bm 範例當中, 連接間隔的預設值為 16 ( 連接間隔為7.5ms * 16 ) ,會透過動態更新修改連接間隔增加 500 變為 516 ( 連接間隔為 7.5ms * 516 ).

二、事前準備

1. IDE & SDK Download

Software Name

Software Version

MCUXpresso IDE

v11.3.1 [Build 5262] [2021-04-02]

MCUXpresso SDK

v2.6.15 (575 2022-04-22)

2. Example code :


3. Hardware

開發板 : FRDM-KW38 * 2


三、動態修改 BLE 連接間隔 ( Connection Interval )

程式碼僅修改 source 資料夾下的 wireless_uart.c 即可 , 請見以下 :

1. 增加一個 Flag 判斷目前連接間隔數值

int BLE_conninterval_long = 0;   //wpi 1: ble connection interval > 500 .
// 0: ble connection interval < 500 .​

2. 在 BleApp_HandleKeys 當中將開發板按鈕SW2 短按原有的功能內容修改為動態修改 BLE 連接間隔,透過 BLE_conninterval_long flag 判斷是否接下來要更新的連接間隔數值是預設值 16 還是 +500 後的數值516.

void BleApp_HandleKeys(key_event_t events)
{
uint8_t mPeerId = 0;

switch (events)
{
case gKBD_EventPressPB1_c: //wpi SW2 short press
{
if(BLE_conninterval_long == 1)
{
Gap_UpdateConnectionParameters(maPeerInformation[mPeerId].deviceId,
gConnReqParams.connIntervalMin,
gConnReqParams.connIntervalMax,
gConnReqParams.connLatency,
gConnReqParams.supervisionTimeout,
gConnReqParams.connEventLengthMin,
gConnReqParams.connEventLengthMax
); //wpi change the connection interval Min and Max
BLE_conninterval_long = 0;
}else
{
Gap_UpdateConnectionParameters(maPeerInformation[mPeerId].deviceId,
gConnReqParams.connIntervalMin + 500,
gConnReqParams.connIntervalMax + 500,
gConnReqParams.connLatency,
gConnReqParams.supervisionTimeout,
gConnReqParams.connEventLengthMin,
gConnReqParams.connEventLengthMax
); //wpi change the connection interval Min and Max
BLE_conninterval_long = 1;
}
break;
}

3. 增加開發板按鈕 SW3 短按時觸發的功能, 將開發板按鈕SW2 短按原有的功能移植, 如下註解位置 ( //wpi add ) 
    ( 執行 LED 閃爍與BleApp_Start()功能 )

#if (gWuart_CentralRole_c == 1) && (gWuart_PeripheralRole_c == 1)
case gKBD_EventPressPB2_c: //wpi SW3 short press
{
/* Switch current role */
if (mGapRole == gGapCentral_c)
{
(void)Serial_Print(gAppSerMgrIf, "\n\rSwitched role to GAP Peripheral.\n\r", gAllowToBlock_d);
mAppUartNewLine = TRUE;
mGapRole = gGapPeripheral_c;

LED_StopFlashingAllLeds(); //wpi add
Led1Flashing(); //wpi add

BleApp_Start(mGapRole); //wpi add
}
else
{
(void)Serial_Print(gAppSerMgrIf, "\n\rSwitched role to GAP Central.\n\r", gAllowToBlock_d);
mAppUartNewLine = TRUE;
mGapRole = gGapCentral_c;

LED_StopFlashingAllLeds(); //wpi add
Led1Flashing(); //wpi add
BleApp_Start(mGapRole); //wpi add
}

break;
}
#endif

4. 將更新後結果透過 UART interface 列印在畫面中

在 BleApp_ConnectionCallback 當中將 gConnEvtParameterUpdateComplete_c 事件加入, 當 BLE 已更新好新的連接間隔數值, 就會執行此事件, 以下 case gConnEvtParameterUpdateComplete_c 全部內容為新增的列印結果程式碼 :
static void BleApp_ConnectionCallback(deviceId_t peerDeviceId, gapConnectionEvent_t *pConnectionEvent)
{
switch (pConnectionEvent->eventType)
{
case gConnEvtParameterUpdateComplete_c: //wpi if BLE update a new connection parameter are finish, then do the below
{
if (pConnectionEvent->eventData.connectionUpdateComplete.status == gBleSuccess_c)
{
(void)Serial_Print(gAppSerMgrIf, "\n\r Connection parameters updated!!! \n\r", gAllowToBlock_d);

//connInterval
(void)Serial_Print(gAppSerMgrIf, "\n\r connInterval: ", gAllowToBlock_d);
(void)Serial_PrintDec(gAppSerMgrIf, pConnectionEvent->eventData.connectionUpdateComplete.connInterval );
(void)Serial_Print(gAppSerMgrIf, "\n\r", gAllowToBlock_d);

//connLatency
(void)Serial_Print(gAppSerMgrIf, "\n\r connLatency: ", gAllowToBlock_d);
(void)Serial_PrintDec(gAppSerMgrIf, pConnectionEvent->eventData.connectionUpdateComplete.connLatency);
(void)Serial_Print(gAppSerMgrIf, "\n\r", gAllowToBlock_d);

//supervisionTimeout
(void)Serial_Print(gAppSerMgrIf, "\n\r supervisionTimeout: ", gAllowToBlock_d);
(void)Serial_PrintDec(gAppSerMgrIf, pConnectionEvent->eventData.connectionUpdateComplete.supervisionTimeout);
(void)Serial_Print(gAppSerMgrIf, "\n\r", gAllowToBlock_d);
}
}
break;

四、功能測試

將兩片 KW38 開發板皆燒錄同樣修改後的韌體程式碼後, 透過 USB 連接到電腦依以下步驟操作 :


1. 開啟兩個 Tera Term , 並連接兩片 KW38 開發板


2. 其中一片 KW38 開發板按下 SW3 按鈕一次, 設定為 Advertiser並開始 Advertising

   另外一片 KW38 開發板按下 SW3 按鈕兩次, 設定為 Scanner 並開始 Scanning

操作完畢應看見雙方 BLE 已正確連線到彼此的畫面結果,如下圖 

(下圖 COM8 為 Scanner , COM11 為 Advertiser )


3. 操作 COM11 Advertiser 那片 KW38 開發板, 按下開發板按鈕 SW2, 此時會將更新 BLE 連接間隔預設值 16 的請求發送到 Scanner 端,結果如以下畫面顯示 : ( connInterval 連接間隔數值修改為 16 )



4. 同樣再操作 COM11 Advertiser 那片 KW38 開發板, 再按下一次開發板按鈕 SW2, 此時會將更新 BLE 連接間隔數值 516 的請求發送到 Scanner 端,結果如以下畫面顯示 : ( connInterval 連接間隔數值修改為 516 )



五、參考來源

1. NXP 技術文件《Bluetooth® Low Energy Host Stack API Reference Manual》
2. NXP 論壇 https://community.nxp.com/t5/Wireless-Connectivity/KW36-Changing-connection-parameters-by-central/m-p/940013

★博文內容均由個人提供,與平台無關,如有違法或侵權,請與網站管理員聯繫。

★文明上網,請理性發言。內容一周內被舉報5次,發文人進小黑屋喔~

評論