1. 概述
Long Range 是BLE 5.0中新增加的特性。Long Range特性对蓝牙的通信距离有很大提高,空旷地带,可达上千米。蓝牙通信距离的增加,也拓宽了其应用场景。
Nordic公司的nrf52840
能够支持Long Range特性。下面就从芯片代码方面,介绍Long Range与普通蓝牙的区别。
2. 代码
下面以代码为例,来说明如何把普通蓝牙增加Long Range特性。
因为普通BLE 与 Long Range不兼容,所以测试时主从机代码都需要修改。测试例程选择为SDK\examples\ble_peripheral\ble_app_blinky
和 SDK\examples\ble_central\ble_app_multilink_central
。
2.1 BLE Peripheral
1. 修改初始化定义参数
宏定义BLE_LONG_RANGE
附近为修改点。
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
#ifdef BLE_LONG_RANGE
#else
static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded scan data. */
#endif
/**@brief Struct that contains pointers to the encoded advertising data. */
static ble_gap_adv_data_t m_adv_data =
{
.adv_data =
{
.p_data = m_enc_advdata,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
},
#ifdef BLE_LONG_RANGE
#else
.scan_rsp_data =
{
.p_data = m_enc_scan_response_data,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX
}
#endif
};
2. advertising_ini() 函数修改
#ifdef BLE_LONG_RANGE
adv_params.interval = APP_ADV_INTERVAL;
adv_params.duration = APP_ADV_DURATION;
adv_params.p_peer_addr = NULL;
adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
adv_params.properties.type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_UNDIRECTED; // extend bit set 1
adv_params.primary_phy = BLE_GAP_PHY_CODED; // PHY_CODED
adv_params.secondary_phy = BLE_GAP_PHY_CODED; // PHY_CODED
#else
adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
adv_params.duration = APP_ADV_DURATION;
adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
adv_params.p_peer_addr = NULL;
adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
adv_params.interval = APP_ADV_INTERVAL;
#endif
3. 设置广播功率
#define RADIO_TX_POWER 8 /**< Radio TX power in dBm. Default to 8dBm, see product spec for valid range */
/**@brief Function for starting advertising.
*/
static void advertising_start(void)
{
ret_code_t err_code;
#ifdef BLE_LONG_RANGE
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, m_adv_handle, RADIO_TX_POWER);
APP_ERROR_CHECK(err_code);
#endif
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
2.2 BLE Central
1. scan_init() 函数修改
init_scan.connect_if_match = mode;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
#ifdef BLE_LONG_RANGE
static ble_gap_scan_params_t m_scan_param =
{
.active = 0x01,
.extended = 0x01,
.interval = 160,
.window = 160,
.timeout = 0x0000, // No timeout.
.scan_phys = BLE_GAP_PHY_CODED,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
};
init_scan.p_scan_param = &m_scan_param;
#endif
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
2. sdk_config.h
NRF_BLE_SCAN_BUFFER
宏定义的值由原来的31,修改为255.
#ifndef NRF_BLE_SCAN_BUFFER
#define NRF_BLE_SCAN_BUFFER 255
#endif
3. 设置扫描功率
#define RADIO_TX_POWER 8 /**< Radio TX power in dBm. Default to 8dBm, see product spec for valid range */
/**@brief Function for starting scanning. */
static void scan_start(void)
{
ret_code_t ret;
NRF_LOG_INFO("Start scanning for device name %s.", (uint32_t)m_target_periph_name);
#ifdef BLE_LONG_RANGE
ret = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_SCAN_INIT, BLE_CONN_HANDLE_INVALID, RADIO_TX_POWER);
APP_ERROR_CHECK(ret);
#endif
ret = nrf_ble_scan_start(&m_scan);
APP_ERROR_CHECK(ret);
}
3. 测试记录
测试硬件使用两块官方 nRF52840-DK(PCA10056) 开发板。
可接收广播距离为900+m,通信距离为600+m。
接全向2dBm增益天线后,通信距离达到1km以上。