From 91c3b6cee6ebc0ce2e51dba00eeeba3c10799e78 Mon Sep 17 00:00:00 2001
From: Michal Pawluk
+Once the application is initialized successfully, the devices discovery procedure starts on "Scan" button press which results in +__scan_start_request_cb() callback function invocation. Firstly, the data model is prepared to store incoming data by cleaning +relevant data structures (model_free()). Then, the Bluetooth LE network scanning is initiated with the +bt_le_start_scan() function. The assigned __bt_le_scan_cb() +callback function is invoked each time an advertisement or a scan response is received from a peer device. + +
+static bool __scan_start_request_cb(void) +{ + model_free(); + + if (!bt_le_start_scan(__bt_le_scan_cb)) + return false; + + dlog_print(DLOG_INFO, LOG_TAG, "LE scanning: started."); + __set_state(VS_SCANNING); + + return true; +} ++ +
+There is only ony type of callback function defined for received messages so one has to check which type of message was received. This check can be performed by + +info structure inspection against relevant data existence: +
+static void __bt_le_scan_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data) +{ + /* Advertisement handling. */ + if (info->adv_data && info->adv_data_len > 0) + __bt_le_adv_data_process(info); + + /* Scan response handling. */ + if (info->scan_data && info->scan_data_len > 0) + __bt_le_scan_data_process(info); +} ++ +
+Depending on inspection result, the __bt_le_adv_data_process() and/or +__bt_le_scan_data_process() functions are invoked. +
+ ++static void __bt_le_adv_data_process(bt_adapter_le_device_scan_result_info_s *info) +{ + char *name = NULL; + bool dev_added; + bt_le_device_info_s *dev_info = NULL; + + if (!bt_le_get_adv_device_name(info, &name)) + return; + + dev_added = __add_found_le_device((const char *)name, &dev_info); + free(name); + + if (!dev_added) + return; + + __adv_data_process(info, &dev_info); +} ++ +
+For peer device identification purpose its remote name is used, so the bt_le_get_adv_device_name() function is called in order to extract +it from the advertisement message. The obtained name is then stored (__add_found_le_device()) and further used to identify the remote device. +Finally, the entire advertisement message is processed for any additional information acquisition (__adv_data_process()). +
+ ++static void __adv_data_process(bt_adapter_le_device_scan_result_info_s *info, bt_le_device_info_s **dev_info) +{ + int i; + int manufacturer_id = -1; + int tx_power_level = 0; + int appearance = 0; + int services_count = 0; + int services_solicitation_count = 0; + char **services = NULL; + char **services_solicitation = NULL; + + bt_le_get_adv_manufacturer_data(info, &manufacturer_id); + bt_le_get_adv_tx_power_level(info, &tx_power_level); + bt_le_get_adv_appearance(info, &appearance); + bt_le_get_adv_service_uuids(info, &services, &services_count); + bt_le_get_adv_service_solicitation_uuids(info, &services_solicitation, &services_solicitation_count); + + model_set_device_info(dev_info, + tx_power_level, + appearance, + manufacturer_id, + services, + services_count, + services_solicitation, + services_solicitation_count); + + for (i = 0; i < services_count; i++) + free(services[i]); + + for (i = 0; i < services_solicitation_count; i++) + free(services_solicitation[i]); + + free(services); + free(services_solicitation); + + view_main_update_peer_info(*dev_info); +} ++ +
+The bt_le_get_adv_{adv_info_name}() functions are invoked to acquire the following information from the advertisement message: +