天线|RF射频|微波|雷达技术
直播中

mameng

13年用户 377经验值
擅长:嵌入式技术
私信 关注
[经验]

【益登科技 Silicon Labs SLWSTK6021A开发板试用体验】温度ClientBLE 主从测试

` 888.png
SoC-温度计客户端
此示例应用程序演示了在多从BLE拓扑中客户端设备的操作。Silicon Labs蓝牙协议栈支持同时连接最多8个从设备。这个示例应用程序演示了如何同时连接到四个温度计外围设备。
11.png
需要一个运行SoC温度计客户端示例应用程序的EFR32/BGM设备,以及至少一个(但不超过四个)运行SoC温度计应用程序的外围设备。下图显示了测试此示例应用程序的拓扑设置。
网络拓扑
当客户端启动时,它开始搜索/发现广告设备。它启动与那些在其广告包中包含健康温度计服务的设备的连接。
建立连接后,客户机从远程GATT数据库中发现健康温度计服务。服务发现完成后,客户机发现温度计特性并启用从远程GATT服务器发送的指示。
对设备进行编程后,打开终端仿真器并通过其串行端口连接到客户端设备。将波特率设置为115200。打开从属温度计设备的电源,观察终端上的输出读数。
四个外围设备的温度读数
22.png

  1. void sl_bt_on_event(sl_bt_msg_t* evt)
  2. {
  3.   sl_status_t sc;
  4.   uint8_t *char_value;
  5.   uint16_t addr_value;
  6.   uint8_t table_index;

  7.   // Handle stack events
  8.   switch (SL_BT_MSG_ID(evt->header)) {
  9.     // -------------------------------
  10.     // This event indicates the device has started and the radio is ready.
  11.     // Do not call any stack command before receiving this boot event!
  12.     case sl_bt_evt_system_boot_id:
  13.       // Print boot message.
  14.       app_log_info("Bluetooth stack booted: v%d.%d.%d-b%d
  15. ",
  16.                    evt->data.evt_system_boot.major,
  17.                    evt->data.evt_system_boot.minor,
  18.                    evt->data.evt_system_boot.patch,
  19.                    evt->data.evt_system_boot.build);
  20.       // Print bluetooth address.
  21.       print_bluetooth_address();
  22.       // Set passive scanning on 1Mb PHY
  23.       sc = sl_bt_scanner_set_mode(sl_bt_gap_1m_phy, SCAN_PASSIVE);
  24.       app_assert_status(sc);
  25.       // Set scan interval and scan window
  26.       sc = sl_bt_scanner_set_timing(sl_bt_gap_1m_phy, SCAN_INTERVAL, SCAN_WINDOW);
  27.       app_assert_status(sc);
  28.       // Set the default connection parameters for subsequent connections
  29.       sc = sl_bt_connection_set_default_parameters(CONN_INTERVAL_MIN,
  30.                                                    CONN_INTERVAL_MAX,
  31.                                                    CONN_RESPONDER_LATENCY,
  32.                                                    CONN_TIMEOUT,
  33.                                                    CONN_MIN_CE_LENGTH,
  34.                                                    CONN_MAX_CE_LENGTH);
  35.       app_assert_status(sc);
  36.       // Start scanning - looking for thermometer devices
  37.       sc = sl_bt_scanner_start(sl_bt_gap_1m_phy, sl_bt_scanner_discover_generic);
  38.       app_assert_status_f(sc,
  39.                           "Failed to start discovery #1
  40. ");
  41.       conn_state = scanning;
  42.       break;

  43.     // -------------------------------
  44.     // This event is generated when an advertisement packet or a scan response
  45.     // is received from a responder
  46.     case sl_bt_evt_scanner_scan_report_id:
  47.       // Parse advertisement packets
  48.       if (evt->data.evt_scanner_scan_report.packet_type == 0) {
  49.         // If a thermometer advertisement is found...
  50.         if (find_service_in_advertisement(&(evt->data.evt_scanner_scan_report.data.data[0]),
  51.                                           evt->data.evt_scanner_scan_report.data.len) != 0) {
  52.           // then stop scanning for a while
  53.           sc = sl_bt_scanner_stop();
  54.           app_assert_status(sc);
  55.           // and connect to that device
  56.           if (active_connections_num < SL_BT_CONFIG_MAX_CONNECTIONS) {
  57.             sc = sl_bt_connection_open(evt->data.evt_scanner_scan_report.address,
  58.                                        evt->data.evt_scanner_scan_report.address_type,
  59.                                        sl_bt_gap_1m_phy,
  60.                                        NULL);
  61.             app_assert_status(sc);
  62.             conn_state = opening;
  63.           }
  64.         }
  65.       }
  66.       break;

  67.     // -------------------------------
  68.     // This event is generated when a new connection is established
  69.     case sl_bt_evt_connection_opened_id:
  70.       // Get last two bytes of sender address
  71.       addr_value = (uint16_t)(evt->data.evt_connection_opened.address.addr[1] << 8) + evt->data.evt_connection_opened.address.addr[0];
  72.       // Add connection to the connection_properties array
  73.       add_connection(evt->data.evt_connection_opened.connection, addr_value);
  74.       // Discover Health Thermometer service on the responder device
  75.       sc = sl_bt_gatt_discover_primary_services_by_uuid(evt->data.evt_connection_opened.connection,
  76.                                                         sizeof(thermo_service),
  77.                                                         (const uint8_t*)thermo_service);
  78.       app_assert_status(sc);

  79.       // Set remote connection power reporting - needed for Power Control
  80.       sc = sl_bt_connection_set_remote_power_reporting(
  81.         evt->data.evt_connection_opened.connection,
  82.         sl_bt_connection_power_reporting_enable);
  83.       app_assert_status(sc);

  84.       conn_state = discover_services;
  85.       break;

  86.     // -------------------------------
  87.     // This event is generated when a new service is discovered
  88.     case sl_bt_evt_gatt_service_id:
  89.       table_index = find_index_by_connection_handle(evt->data.evt_gatt_service.connection);
  90.       if (table_index != TABLE_INDEX_INVALID) {
  91.         // Save service handle for future reference
  92.         conn_properties[table_index].thermometer_service_handle = evt->data.evt_gatt_service.service;
  93.       }
  94.       break;

  95.     // -------------------------------
  96.     // This event is generated when a new characteristic is discovered
  97.     case sl_bt_evt_gatt_characteristic_id:
  98.       table_index = find_index_by_connection_handle(evt->data.evt_gatt_characteristic.connection);
  99.       if (table_index != TABLE_INDEX_INVALID) {
  100.         // Save characteristic handle for future reference
  101.         conn_properties[table_index].thermometer_characteristic_handle = evt->data.evt_gatt_characteristic.characteristic;
  102.       }
  103.       break;

  104.     // -------------------------------
  105.     // This event is generated for various procedure completions, e.g. when a
  106.     // write procedure is completed, or service discovery is completed
  107.     case sl_bt_evt_gatt_procedure_completed_id:
  108.       table_index = find_index_by_connection_handle(evt->data.evt_gatt_procedure_completed.connection);
  109.       if (table_index == TABLE_INDEX_INVALID) {
  110.         break;
  111.       }
  112.       // If service discovery finished
  113.       if (conn_state == discover_services && conn_properties[table_index].thermometer_service_handle != SERVICE_HANDLE_INVALID) {
  114.         // Discover thermometer characteristic on the responder device
  115.         sc = sl_bt_gatt_discover_characteristics_by_uuid(evt->data.evt_gatt_procedure_completed.connection,
  116.                                                          conn_properties[table_index].thermometer_service_handle,
  117.                                                          sizeof(thermo_char),
  118.                                                          (const uint8_t*)thermo_char);
  119.         app_assert_status(sc);
  120.         conn_state = discover_characteristics;
  121.         break;
  122.       }
  123.       // If characteristic discovery finished
  124.       if (conn_state == discover_characteristics && conn_properties[table_index].thermometer_characteristic_handle != CHARACTERISTIC_HANDLE_INVALID) {
  125.         // stop discovering
  126.         sl_bt_scanner_stop();
  127.         // enable indications
  128.         sc = sl_bt_gatt_set_characteristic_notification(evt->data.evt_gatt_procedure_completed.connection,
  129.                                                         conn_properties[table_index].thermometer_characteristic_handle,
  130.                                                         sl_bt_gatt_indication);
  131.         app_assert_status(sc);
  132.         conn_state = enable_indication;
  133.         break;
  134.       }
  135.       // If indication enable process finished
  136.       if (conn_state == enable_indication) {
  137.         // and we can connect to more devices
  138.         if (active_connections_num < SL_BT_CONFIG_MAX_CONNECTIONS) {
  139.           // start scanning again to find new devices
  140.           sc = sl_bt_scanner_start(sl_bt_gap_1m_phy, sl_bt_scanner_discover_generic);
  141.           app_assert_status_f(sc,
  142.                               "Failed to start discovery #2
  143. ");
  144.           conn_state = scanning;
  145.         } else {
  146.           conn_state = running;
  147.         }
  148.         break;
  149.       }
  150.       break;

  151.     // -------------------------------
  152.     // This event is generated when a connection is dropped
  153.     case sl_bt_evt_connection_closed_id:
  154.       // remove connection from active connections
  155.       remove_connection(evt->data.evt_connection_closed.connection);
  156.       if (conn_state != scanning) {
  157.         // start scanning again to find new devices
  158.         sc = sl_bt_scanner_start(sl_bt_gap_1m_phy, sl_bt_scanner_discover_generic);
  159.         app_assert_status_f(sc,
  160.                             "Failed to start discovery #3
  161. ");
  162.         conn_state = scanning;
  163.       }
  164.       break;

  165.     // -------------------------------
  166.     // This event is generated when a characteristic value was received e.g. an indication
  167.     case sl_bt_evt_gatt_characteristic_value_id:
  168.       if (evt->data.evt_gatt_characteristic_value.value.len >= 5) {
  169.         char_value = &(evt->data.evt_gatt_characteristic_value.value.data[0]);
  170.         table_index = find_index_by_connection_handle(evt->data.evt_gatt_characteristic_value.connection);
  171.         if (table_index != TABLE_INDEX_INVALID) {
  172.           conn_properties[table_index].temperature = translate_IEEE_11073_temperature_to_float((IEEE_11073_float *)(char_value + 1));
  173.           conn_properties[table_index].unit = translate_flags_to_temperature_unit(char_value[0]);
  174.         }
  175.       } else {
  176.         app_log_warning("Characteristic value too short: %d
  177. ",
  178.                         evt->data.evt_gatt_characteristic_value.value.len);
  179.       }
  180.       // Send confirmation for the indication
  181.       sc = sl_bt_gatt_send_characteristic_confirmation(evt->data.evt_gatt_characteristic_value.connection);
  182.       app_assert_status(sc);
  183.       // Trigger RSSI measurement on the connection
  184.       sc = sl_bt_connection_get_rssi(evt->data.evt_gatt_characteristic_value.connection);
  185.       app_assert_status(sc);
  186.       break;

  187.     // -------------------------------
  188.     // This event is generated when RSSI value was measured
  189.     case sl_bt_evt_connection_rssi_id:
  190.       table_index = find_index_by_connection_handle(evt->data.evt_connection_rssi.connection);
  191.       if (table_index != TABLE_INDEX_INVALID) {
  192.         conn_properties[table_index].rssi = evt->data.evt_connection_rssi.rssi;
  193.       }

  194.       print_values();
  195.       break;

  196.     // -------------------------------
  197.     // TX Power is updated
  198.     case sl_bt_evt_connection_tx_power_id:

  199.       table_index = find_index_by_connection_handle(
  200.         evt->data.evt_connection_tx_power.connection);

  201.       if (table_index != TABLE_INDEX_INVALID) {
  202.         conn_properties[table_index].tx_power =
  203.           evt->data.evt_connection_tx_power.power_level;
  204.       }

  205.       // TX Power reporting is enabled on the other side.
  206.       conn_properties[table_index].power_control_active =
  207.         TX_POWER_CONTROL_ACTIVE;
  208.       break;

  209.     // -------------------------------
  210.     // Remote TX Power is updated
  211.     case sl_bt_evt_connection_remote_tx_power_id:
  212.       table_index = find_index_by_connection_handle(
  213.         evt->data.evt_connection_remote_tx_power.connection);

  214.       if (table_index != TABLE_INDEX_INVALID) {
  215.         conn_properties[table_index].remote_tx_power =
  216.           evt->data.evt_connection_remote_tx_power.power_level;
  217.       }
  218.       break;

  219.     default:
  220.       break;
  221.   }
  222. }





` 999.png

更多回帖

发帖
×
20
完善资料,
赚取积分