1. 设置广播参数为间隔1000ms,不停止
2. 添加广播消息,含01、03、09、FF TYPE
3. 设置蓝牙通信间隔参数为320ms、400ms、2、4000ms超时
3. 配置发射功率为较低
4. 配置GATT所有数据与原Nordic 配置一致
为了解决以上疑问,需要多看一些示例代码。目前还没有看到有价值的例程。但是可以查看代码中的API接口说明,多试几次,可以找到。
- // 自定义的广播数据
- uint8_t advData[] = {0x02, 0x01, 0x06, 0x03, 0x03, 0x30, 0xFF, 0x03, 0xFF, 0xAA, 0x55, 0x0B, 0x09, 0x53, 0x55, 0x4E, 0x4C, 0x41, 0x4E, 0x54, 0x30, 0x30, 0x31};//0x02 01 06 03 03 30 FF 03 FF AA 55 0B 09 53 55 4E 4C 41 4E 54 30 30 31
- /**************************************************************************//**
- * Bluetooth stack event handler.
- * This overrides the dummy weak implementation.
- *
- * @param[in] evt Event coming from the Bluetooth stack.
- *****************************************************************************/
- void sl_bt_on_event(sl_bt_msg_t *evt)
- {
- sl_status_t sc;
- static int cntt = 0;
- bd_addr address;
- uint8_t address_type;
-
- switch (SL_BT_MSG_ID(evt->header)) {
- // -------------------------------
- // This event indicates the device has started and the radio is ready.
- // Do not call any stack command before receiving this boot event!
- case sl_bt_evt_system_boot_id:
-
- // Extract unique ID from BT Address.
- sc = sl_bt_system_get_identity_address(&address, &address_type);
- app_assert_status(sc);
-
- app_log_info("Bluetooth %s address: %02X:%02X:%02X:%02X:%02X:%02X\n",
- address_type ? "static random" : "public device",
- address.addr[5],
- address.addr[4],
- address.addr[3],
- address.addr[2],
- address.addr[1],
- address.addr[0]);
-
- // Create an advertising set.
- sc = sl_bt_advertiser_create_set(&advertising_set_handle);
- app_assert_status(sc);
-
- // 查找API,可以发现多个函数可以被使用
- //sl_bt_advertiser_set_data(advertising_set_handle, 0, sizeof(advData), advData);
- //sl_bt_advertiser_set_tx_power
- //sl_bt_legacy_advertiser_set_data
-
- // Generate data for advertising
- sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle,
- sl_bt_advertiser_general_discoverable);
- app_assert_status(sc);
-
- // Set advertising interval to 100ms.
- // 设置广播间隔为1000ms,一直广播不停止
- sc = sl_bt_advertiser_set_timing(
- advertising_set_handle,
- 1600, // min. adv. interval (milliseconds * 1.6) 320ms
- 1600, // max. adv. interval (milliseconds * 1.6) 400ms
- 0, // adv. duration
- 0); // max. num. adv. events
- app_assert_status(sc);
-
- // 设置广播消息,放在此处有效而不能紧靠放在sl_bt_advertiser_create_set之后
- sc = sl_bt_legacy_advertiser_set_data(advertising_set_handle, sl_bt_advertiser_advertising_data_packet, sizeof(advData), advData);
- app_assert_status(sc);
-
- // Start advertising and enable connections.
- sc = sl_bt_legacy_advertiser_start(advertising_set_handle,
- sl_bt_advertiser_connectable_scannable);
- app_assert_status(sc);
注意,广播消息的设置,要有正确的顺序才行;
由于完整有用的例程不多,所以只能阅读【sl_bt_api.h】文件,除了函数,也需要看看结构体定义,多试是能将接口用起来。
ID必须要有,不然event中找不到此信号
代码中对属性数据的设置:
在这里,似乎只能不使用Generic Attribute服务,而不能使其为空,这是UI配置工具的局限吧,具体如何设置为空暂不考虑。
蓝牙参数的初始化配置,顺便地,,看下其他所有可配置的组件:
广播参数在上边代码中已经说明。那连接参数呢?
经过测试,需要放到连接打开时间中,调用“sl_bt_connection_set_parameters”
- // -------------------------------
- // This event indicates that a new connection was opened.
- case sl_bt_evt_connection_opened_id:
- app_log_info("Connection opened.\n");
- // evt_connection_opened发生之后
- sl_bt_connection_set_parameters(evt->data.evt_connection_opened.connection, 256, 320, 0, 400, 0, 0x7FFF); //320ms-400ms的通信间隔
- //sl_bt_connection_set_parameters(evt->data.evt_connection_opened.connection, 10, 20, 0, 400, 0, 0x7FFF); //12.5ms-25ms的通信间隔
-
- break;
如何验证这个链接参数设置成功呢?
借助ssv5集成的功率监控:
放大电流曲线图,可以清晰识别出通信阶段的蓝牙连接间隔。
注意,连接间隔受APP侧的控制,调试app可以调整此参数。此处设置相当于设置了一个默认值。具体的内容还有待了解。
(补充):此处遇到了手机不兼容的情况,刷机的红米,连接之后自动将间隔设置为最短!可能那些能刷机的手机在蓝牙稳定性上有更多不确定,需要注意。
添加timer模块:
参考吞吐量例程,可知其用法:
- #include "app_timer.h"
-
- static app_timer_t refresh_timer;
-
- static void refresh_timer_callback(app_timer_t *timer,
- void *data)
- {
- (void)timer;
- (void)data;
- timer_on_refresh_rssi();
- }
-
- /**************************************************************************//**
- * Start RSSI refresh timer
- *****************************************************************************/
- void timer_refresh_rssi_start(void)
- {
- // Start refresh timer
- sl_status_t sc;
- sc = app_timer_start(&refresh_timer,
- THROUGHPUT_CENTRAL_REFRESH_TIMER_PERIOD,
- refresh_timer_callback,
- NULL,
- true);
- app_assert_status(sc);
- }
-
- /**************************************************************************//**
- * Stop RSSI refresh timer
- *****************************************************************************/
- void timer_refresh_rssi_stop(void)
- {
- // Stop refresh timer
- sl_status_t sc;
- sc = app_timer_stop(&refresh_timer);
- app_assert_status(sc);
- }
-
- /**************************************************************************//**
- * Event handler for timer
- *****************************************************************************/
- void timer_on_refresh_rssi(void)
- {
- sl_status_t sc;
- if (connection_handle_central != SL_BT_INVALID_CONNECTION_HANDLE && central_state.state != THROUGHPUT_STATE_TEST) {
- sc = sl_bt_connection_get_rssi(connection_handle_central);
- app_assert_status(sc);
- }
- }