hci: Close the HAL before cleaning up pending command queue and timers

This prevents errors in case of event callbacks being received or timers
being fired after the pending command queue has been deleted.

Bug: 36026072
Test: Turn Bluetooth off/on while scanning and file transfer
Change-Id: I7bb699e929a8b40e42a3543b5445885ae6b1b975
(cherry picked from commit d696e66cadd6592f32bc2efb74e3687554ba669c)
diff --git a/hci/src/hci_layer.cc b/hci/src/hci_layer.cc
index 18d57bb..6c9d79c 100644
--- a/hci/src/hci_layer.cc
+++ b/hci/src/hci_layer.cc
@@ -220,6 +220,14 @@
 static future_t* hci_module_shut_down() {
   LOG_INFO(LOG_TAG, "%s", __func__);
 
+  // Free the timers
+  alarm_free(command_response_timer);
+  command_response_timer = NULL;
+  alarm_free(startup_timer);
+  startup_timer = NULL;
+
+  hci_close();
+
   if (thread) {
     thread_stop(thread);
     thread_join(thread);
@@ -234,14 +242,6 @@
 
   packet_fragmenter->cleanup();
 
-  // Free the timers
-  alarm_free(command_response_timer);
-  command_response_timer = NULL;
-  alarm_free(startup_timer);
-  startup_timer = NULL;
-
-  hci_close();
-
   thread_free(thread);
   thread = NULL;