<h2>Implementation</h2>
+<h3>Service initialization and termination</h3>
+<h4>Initialization</h4>
+<p>
+Before the service starts its operation, the availability of relevant Bluetooth interfaces is checked by calling the following functions:
+ <ul>
+ <li><span style="font-family: Courier New,Courier,monospace">bt_common_is_bluetooth_supported()</span> - General Bluetooth adapter;</li>
+ <li><span style="font-family: Courier New,Courier,monospace">bt_common_is_bluetooth_le_supported()</span> - Low Energy Bluetooth adapter.</li>
+ </ul>
+If both of them are available, the Heart Rate sensor listener is created with <span style="font-family: Courier New,Courier,monospace">sensor_listener_create()</span> function. Finally, the Bluetooth
+interface is initialized with the <span style="font-family: Courier New,Courier,monospace">__bt_init()</span> function.
+</p>
+
+<pre class="prettyprint">
+static bool __bt_init(void)
+{
+ bool is_enabled = false;
+
+ /* Access to the H/W Bluetooth adapter is initialized. */
+ if (bt_common_init()) {
+ /* The __bt_device_state_changed_cb() callback function is registered to be invoked on Bluetooth adapter state change. */
+ bt_common_set_state_change_callback(__bt_device_state_changed_cb);
+
+ /* The current state of the Bluetooth adapter is obtained.
+ If it is enabled the the __bt_device_state_changed_cb() callback function is artificially invoked. */
+ bt_common_get_state(&is_enabled);
+ if (is_enabled)
+ __bt_device_state_changed_cb(BT_ERROR_NONE, BT_ADAPTER_ENABLED, NULL);
+ }
+
+ return is_enabled;
+}
+</pre>
+
+<p>
+See the <a href="#ref">References</a> section for details of all undescribed functions.
+</p>
+
+<h4>Termination</h4>
+
+<p>
+In the service termination procedure, the Heart Rate sensor listener and data advertizer are destroyed, the assigned callback function is unset and, finally, the access to the Bluetooth adapter is deinitialized.
+</p>
+
+<pre class="prettyprint">
+void controller_terminate(void)
+{
+ sensor_listener_delete();
+
+ bt_advertiser_delete(&__ctrldata.adv_hrm_h);
+ dlog_print(DLOG_WARN, LOG_TAG, "Heart Rate Measurement advertiser stopped.");
+
+ bt_common_unset_state_change_callback();
+ bt_common_deinit();
+}
+</pre>
+
+<p>
+See the <a href="#ref">References</a> section for the details of all the undescribed functions.
+</p>
+
+<h3 id="ref">References</h3>
<script type="text/javascript" src="../scripts/jquery.zclip.min.js"></script>
<script type="text/javascript" src="../scripts/showhide.js"></script>