From 91c3b6cee6ebc0ce2e51dba00eeeba3c10799e78 Mon Sep 17 00:00:00 2001 From: Michal Pawluk Date: Wed, 30 Dec 2015 13:48:01 +0100 Subject: [PATCH] [SAMPLE APP][Bluetooth LE Collector] Devices discovery section added Change-Id: Ib0aec3f38864d86a148d90c9dfb63a6fbb44362d Signed-off-by: Michal Pawluk --- .../html/mobile_n/bluetooth_le_collector_sd_mn.htm | 145 +++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/org.tizen.sampledescriptions/html/mobile_n/bluetooth_le_collector_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/bluetooth_le_collector_sd_mn.htm index d8ef4c7..1321d69 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/bluetooth_le_collector_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/bluetooth_le_collector_sd_mn.htm @@ -180,6 +180,141 @@ and view destroying (vi For undescribed function's reference, see the Reference section.

+

Devices discovery

+ +

+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: +

    +
  • if the adv_data pointer is not NULL + and the adv_data_len value is greater than 0 then an advertisement message was received,
  • +
  • if the scan_data pointer is not NULL + and the scan_data_len value is greater than 0 then a scan response message was received.
  • +
+

+ +
+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: +

    +
  • manufacturer's data - device's vendor identifier (see the company identifiers for details);
  • +
  • transmition power level - the transmition power expressed in dBm (decibel-milliwatts);
  • +
  • appearance code - a value that describes general device usage (see the + external device's appearance + for details);
  • +
  • services UUIDs - a list of handled services identifiers;
  • +
  • solicitation services UUIDs - a list of handled solicitation services identifiers.
  • +
+All the acquired information is binded to the remote device name previously obtained and stored internally for further use. +
+Finally, the user interface is updated with view_main_update_peer_info() function to reflect received data. +

-- 2.7.4