From a4fe6467953bdb173fdf96a604f6527ed88f81c3 Mon Sep 17 00:00:00 2001
From: Matthias Ringwald <matthias@ringwald.ch>
Date: Fri, 24 Aug 2018 10:03:14 +0200
Subject: [PATCH] example: register for events after setting up the stack,
 register for hci events after sm_init()

---
 example/a2dp_sink_demo.c         |  8 +++----
 example/a2dp_source_demo.c       |  7 +++---
 example/ancs_client_demo.c       | 16 +++++++++-----
 example/ant_test.c               | 21 +++++++++---------
 example/avrcp_browsing_client.c  | 18 +++++++++-------
 example/dut_mode_classic.c       |  6 +++---
 example/gap_inquiry.c            |  6 +++---
 example/gap_le_advertisements.c  |  5 +++--
 example/gatt_battery_query.c     |  8 +++----
 example/gatt_browser.c           | 14 ++++++------
 example/gatt_heart_rate_client.c |  7 +++---
 example/hfp_ag_demo.c            | 15 +++++++------
 example/hfp_hf_demo.c            | 17 ++++++++-------
 example/hid_host_demo.c          |  6 +++---
 example/hid_keyboard_demo.c      | 12 +++++------
 example/hid_mouse_demo.c         | 11 +++++-----
 example/hog_keyboard_demo.c      | 18 ++++++++++------
 example/hog_mouse_demo.c         | 15 +++++++------
 example/hsp_ag_demo.c            |  2 ++
 example/hsp_hs_demo.c            | 12 ++++++-----
 example/le_counter.c             | 12 ++++++-----
 example/le_data_channel_client.c |  6 +++---
 example/le_data_channel_server.c | 11 +++++-----
 example/le_streamer.c            | 12 ++++++-----
 example/le_streamer.gatt         |  2 +-
 example/le_streamer_client.c     |  7 +++---
 example/panu_demo.c              |  8 +++----
 example/pbap_client_demo.c       | 12 +++++------
 example/sdp_bnep_query.c         |  5 ++---
 example/sdp_general_query.c      |  5 ++---
 example/sdp_rfcomm_query.c       |  9 +++-----
 example/sm_pairing_central.c     | 17 +++++++++------
 example/sm_pairing_peripheral.c  | 37 ++++++++++++++++++++++----------
 example/spp_and_le_counter.c     | 21 +++++++++---------
 example/spp_and_le_streamer.c    | 11 +++++-----
 example/spp_streamer.c           |  8 +++----
 example/spp_streamer_client.c    |  8 +++----
 37 files changed, 228 insertions(+), 187 deletions(-)

diff --git a/example/a2dp_sink_demo.c b/example/a2dp_sink_demo.c
index 725dccfb0..09399f79d 100644
--- a/example/a2dp_sink_demo.c
+++ b/example/a2dp_sink_demo.c
@@ -211,10 +211,6 @@ static void handle_l2cap_media_data_packet(uint8_t seid, uint8_t *packet, uint16
 
 static int a2dp_and_avrcp_setup(void){
 
-    /* Register for HCI events */
-    hci_event_callback_registration.callback = &hci_packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
     // Initialize AVDTP Sink
     a2dp_sink_init();
@@ -246,6 +242,10 @@ static int a2dp_and_avrcp_setup(void){
     gap_discoverable_control(1);
     gap_set_class_of_device(0x200408);
 
+    /* Register for HCI events */
+    hci_event_callback_registration.callback = &hci_packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     return 0;
 }
 
diff --git a/example/a2dp_source_demo.c b/example/a2dp_source_demo.c
index b201427b1..9c0c27a47 100644
--- a/example/a2dp_source_demo.c
+++ b/example/a2dp_source_demo.c
@@ -235,9 +235,6 @@ static void stdin_process(char cmd);
 #endif
 
 static int a2dp_source_and_avrcp_services_init(void){
-    // Register for HCI events.
-    hci_event_callback_registration.callback = &a2dp_source_packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
 
     l2cap_init();
     // Initialize  A2DP Source.
@@ -276,6 +273,10 @@ static int a2dp_source_and_avrcp_services_init(void){
     gap_discoverable_control(1);
     gap_set_class_of_device(0x200408);
     
+    // Register for HCI events.
+    hci_event_callback_registration.callback = &a2dp_source_packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     hxcmod_initialized = hxcmod_init(&mod_context);
     if (hxcmod_initialized){
         hxcmod_setcfg(&mod_context, A2DP_SAMPLE_RATE, 16, 1, 1, 1);
diff --git a/example/ancs_client_demo.c b/example/ancs_client_demo.c
index eae2d1c34..256f9c23e 100644
--- a/example/ancs_client_demo.c
+++ b/example/ancs_client_demo.c
@@ -136,9 +136,6 @@ int btstack_main(int argc, const char * argv[]){
 
     printf("BTstack ANCS Client starting up...\n");
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &app_packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
 
     // set up l2cap_le
     l2cap_init();
@@ -151,19 +148,28 @@ int btstack_main(int argc, const char * argv[]){
     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
     sm_set_authentication_requirements( SM_AUTHREQ_BONDING ); 
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &app_packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // register for SM events
     sm_event_callback_registration.callback = &app_packet_handler;
     sm_add_event_handler(&sm_event_callback_registration);
 
     // setup ATT server
     att_server_init(profile_data, NULL, NULL);    
+
+    // setup ANCS Client
+    ancs_client_init();
+
+
+    // register for ATT Serer events
     att_server_register_packet_handler(app_packet_handler);
 
     // setup GATT client
     gatt_client_init();
 
-    // setup ANCS Client
-    ancs_client_init();
+    // register for ancs events
     ancs_client_register_callback(&ancs_callback);
 
     // setup advertisements
diff --git a/example/ant_test.c b/example/ant_test.c
index 429a0240a..9652d3b62 100644
--- a/example/ant_test.c
+++ b/example/ant_test.c
@@ -231,10 +231,6 @@ static void  heartbeat_handler(struct btstack_timer_source *ts){
 int btstack_main(int argc, const char * argv[]);
 int btstack_main(int argc, const char * argv[]){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // init L2CAP
     l2cap_init();
     
@@ -247,17 +243,20 @@ int btstack_main(int argc, const char * argv[]){
     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
     spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
     sdp_register_service(spp_service_buffer);
-    printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
-    
+        
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // set local name
+    gap_set_local_name("ANT Demo");
+    // make discoverable
+    gap_discoverable_control(1);
+
     // set one-shot timer
     heartbeat.process = &heartbeat_handler;
     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
     btstack_run_loop_add_timer(&heartbeat);
-    
-    // set local name
-    gap_set_local_name("BlueMSP-Demo");
-    // make discoverable
-    gap_discoverable_control(1);
 
 	printf("Run...\n\r");
  	// turn on!
diff --git a/example/avrcp_browsing_client.c b/example/avrcp_browsing_client.c
index e53ab06a3..0f4280735 100644
--- a/example/avrcp_browsing_client.c
+++ b/example/avrcp_browsing_client.c
@@ -168,19 +168,13 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 
-    // Register for HCI events.
-    hci_event_callback_registration.callback = &avrcp_browsing_controller_packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // Initialize L2CAP.
     l2cap_init();
     
     // Initialize AVRCP Controller.
     avrcp_controller_init();
-    // Register AVRCP for HCI events.
-    avrcp_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
-    
-    // Initialize AVRCP Browsing Controller, HCI events will be sent to the AVRCP Controller callback. 
+
+        // Initialize AVRCP Browsing Controller, HCI events will be sent to the AVRCP Controller callback. 
     avrcp_browsing_controller_init();
     // // Register AVRCP for HCI events.
     // avrcp_browsing_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
@@ -200,6 +194,14 @@ int btstack_main(int argc, const char * argv[]){
     gap_discoverable_control(1);
     gap_set_class_of_device(0x200408);
     
+    // Register for HCI events.
+    hci_event_callback_registration.callback = &avrcp_browsing_controller_packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // Register for AVRCP events.
+    avrcp_controller_register_packet_handler(&avrcp_browsing_controller_packet_handler);
+
+
 #ifdef HAVE_BTSTACK_STDIN
     // Parse human readable Bluetooth address.
     sscanf_bd_addr(device_addr_string, device_addr);
diff --git a/example/dut_mode_classic.c b/example/dut_mode_classic.c
index 86d261b19..e38d3d04e 100644
--- a/example/dut_mode_classic.c
+++ b/example/dut_mode_classic.c
@@ -110,9 +110,6 @@ int btstack_main(int argc, const char * argv[]) {
     (void)argc;
     (void)argv;
 
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // make device connectable
     // @note: gap_connectable_control will be enabled when an L2CAP service 
     // (e.g. RFCOMM) is initialized). Therefore, it's not needed in regular applications
@@ -121,6 +118,9 @@ int btstack_main(int argc, const char * argv[]) {
     // make device discoverable
     gap_discoverable_control(1);
 
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
         
diff --git a/example/gap_inquiry.c b/example/gap_inquiry.c
index 9931f977e..e1567bb69 100644
--- a/example/gap_inquiry.c
+++ b/example/gap_inquiry.c
@@ -249,12 +249,12 @@ int btstack_main(int argc, const char * argv[]) {
     (void)argc;
     (void)argv;
 
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // enabled EIR
     hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
 
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
         
diff --git a/example/gap_le_advertisements.c b/example/gap_le_advertisements.c
index 3d062921f..5cc43b40d 100644
--- a/example/gap_le_advertisements.c
+++ b/example/gap_le_advertisements.c
@@ -68,11 +68,12 @@ static btstack_packet_callback_registration_t hci_event_callback_registration;
 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
 
 static void gap_le_advertisements_setup(void){
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
     // Active scanning, 100% (scan interval = scan window)
     gap_set_scan_parameters(1,48,48);
     gap_start_scan(); 
+
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
 }
 
 /* LISTING_END */
diff --git a/example/gatt_battery_query.c b/example/gatt_battery_query.c
index e9fbb1e51..5bf439a5b 100644
--- a/example/gatt_battery_query.c
+++ b/example/gatt_battery_query.c
@@ -334,10 +334,6 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 #endif
-
-    hci_event_callback_registration.callback = &hci_event_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones
@@ -349,6 +345,10 @@ int btstack_main(int argc, const char * argv[]){
     sm_init();
     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
 
+    hci_event_callback_registration.callback = &hci_event_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
 
diff --git a/example/gatt_browser.c b/example/gatt_browser.c
index f2007f69d..a807d9dc9 100644
--- a/example/gatt_browser.c
+++ b/example/gatt_browser.c
@@ -102,10 +102,6 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
 
 static void gatt_client_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &handle_hci_event;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // Initialize L2CAP and register HCI event handler
     l2cap_init();
 
@@ -115,6 +111,10 @@ static void gatt_client_setup(void){
     // Optinoally, Setup security manager
     sm_init();
     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &handle_hci_event;
+    hci_add_event_handler(&hci_event_callback_registration);
 }
 /* LISTING_END */
 
@@ -297,12 +297,12 @@ int btstack_main(int argc, const char * argv[]){
     (void)argv;
 #endif
 
-    // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones
-    att_server_init(profile_data, NULL, NULL);    
-
     // setup GATT client
     gatt_client_setup();
 
+    // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android and iOS
+    att_server_init(profile_data, NULL, NULL);    
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
     
diff --git a/example/gatt_heart_rate_client.c b/example/gatt_heart_rate_client.c
index 5ee26f1ca..0d22cddd5 100644
--- a/example/gatt_heart_rate_client.c
+++ b/example/gatt_heart_rate_client.c
@@ -364,10 +364,6 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 #endif
-
-    hci_event_callback_registration.callback = &hci_event_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     gatt_client_init();
@@ -375,6 +371,9 @@ int btstack_main(int argc, const char * argv[]){
     sm_init();
     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
 
+    hci_event_callback_registration.callback = &hci_event_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
 
diff --git a/example/hfp_ag_demo.c b/example/hfp_ag_demo.c
index 9b28dc233..ad113e022 100644
--- a/example/hfp_ag_demo.c
+++ b/example/hfp_ag_demo.c
@@ -563,11 +563,6 @@ int btstack_main(int argc, const char * argv[]){
 
     sco_demo_init();
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-    hci_register_sco_packet_handler(&packet_handler);
-
     gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
     gap_discoverable_control(1);
 
@@ -596,8 +591,6 @@ int btstack_main(int argc, const char * argv[]){
     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators); 
     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
-    hfp_ag_register_packet_handler(&packet_handler);
-    hci_register_sco_packet_handler(&packet_handler);
 
     // SDP Server
     sdp_init();
@@ -606,6 +599,14 @@ int btstack_main(int argc, const char * argv[]){
     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
     sdp_register_service(hfp_service_buffer);
     
+    // register for HCI events and SCO packets
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+    hci_register_sco_packet_handler(&packet_handler);
+
+    // register for HFP events
+    hfp_ag_register_packet_handler(&packet_handler);
+
     // parse humand readable Bluetooth address
     sscanf_bd_addr(device_addr_string, device_addr);
 
diff --git a/example/hfp_hf_demo.c b/example/hfp_hf_demo.c
index 748674582..71a192dd1 100644
--- a/example/hfp_hf_demo.c
+++ b/example/hfp_hf_demo.c
@@ -564,11 +564,6 @@ int btstack_main(int argc, const char * argv[]){
 
     sco_demo_init();
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-    hci_register_sco_packet_handler(&packet_handler);
-
     gap_discoverable_control(1);
     gap_set_class_of_device(0x200408);   
     gap_set_local_name("HFP HF Demo 00:00:00:00:00:00");
@@ -591,15 +586,21 @@ int btstack_main(int argc, const char * argv[]){
     hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
     hfp_hf_init_codecs(sizeof(codecs), codecs);
     
-    hfp_hf_register_packet_handler(packet_handler);
-    hci_register_sco_packet_handler(&packet_handler);
-
     sdp_init();    
     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
     hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
     printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
     sdp_register_service(hfp_service_buffer);
 
+    // register for HCI events and SCO packets
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+    hci_register_sco_packet_handler(&packet_handler);
+    hci_register_sco_packet_handler(&packet_handler);
+
+    // register for HFP events
+    hfp_hf_register_packet_handler(packet_handler);
+
 #ifdef HAVE_BTSTACK_STDIN
     // parse human readable Bluetooth address
     sscanf_bd_addr(device_addr_string, device_addr);
diff --git a/example/hid_host_demo.c b/example/hid_host_demo.c
index bfc81b193..e17224178 100644
--- a/example/hid_host_demo.c
+++ b/example/hid_host_demo.c
@@ -138,13 +138,13 @@ static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel
 
 static void hid_host_setup(void){
 
+    // Initialize L2CAP 
+    l2cap_init();
+
     // register for HCI events
     hci_event_callback_registration.callback = &packet_handler;
     hci_add_event_handler(&hci_event_callback_registration);
 
-    // Initialize L2CAP 
-    l2cap_init();
-
     // Disable stdout buffering
     setbuf(stdout, NULL);
 }
diff --git a/example/hid_keyboard_demo.c b/example/hid_keyboard_demo.c
index 9b0af0a21..443d8174e 100644
--- a/example/hid_keyboard_demo.c
+++ b/example/hid_keyboard_demo.c
@@ -382,11 +382,6 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-    hci_register_sco_packet_handler(&packet_handler);
-
     gap_discoverable_control(1);
     gap_set_class_of_device(0x2540);
     gap_set_local_name("HID Keyboard Demo 00:00:00:00:00:00");
@@ -408,9 +403,14 @@ int btstack_main(int argc, const char * argv[]){
     printf("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer));
     sdp_register_service(device_id_sdp_service_buffer);
 
-    
     // HID Device
     hid_device_init();
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for HID events
     hid_device_register_packet_handler(&packet_handler);
 
 #ifdef HAVE_BTSTACK_STDIN
diff --git a/example/hid_mouse_demo.c b/example/hid_mouse_demo.c
index d3f5bd39a..40eb4b044 100644
--- a/example/hid_mouse_demo.c
+++ b/example/hid_mouse_demo.c
@@ -284,11 +284,6 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-    hci_register_sco_packet_handler(&packet_handler);
-
     gap_discoverable_control(1);
     gap_set_class_of_device(0x2540);
     gap_set_local_name("HID Mouse Demo 00:00:00:00:00:00");
@@ -306,6 +301,12 @@ int btstack_main(int argc, const char * argv[]){
 
     // HID Device
     hid_device_init();
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for HID
     hid_device_register_packet_handler(&packet_handler);
 
 #ifdef HAVE_BTSTACK_STDIN
diff --git a/example/hog_keyboard_demo.c b/example/hog_keyboard_demo.c
index 4e33bc927..bddd62e02 100644
--- a/example/hog_keyboard_demo.c
+++ b/example/hog_keyboard_demo.c
@@ -179,10 +179,6 @@ const uint8_t adv_data_len = sizeof(adv_data);
 
 static void le_keyboard_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup le device db
@@ -190,8 +186,6 @@ static void le_keyboard_setup(void){
 
     // setup SM: Display only
     sm_init();
-    sm_event_callback_registration.callback = &packet_handler;
-    sm_add_event_handler(&sm_event_callback_registration);
     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
     sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
 
@@ -206,7 +200,6 @@ static void le_keyboard_setup(void){
 
     // setup HID Device service
     hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));
-    hids_device_register_packet_handler(packet_handler);
 
     // setup advertisements
     uint16_t adv_int_min = 0x0030;
@@ -217,6 +210,17 @@ static void le_keyboard_setup(void){
     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
     gap_advertisements_enable(1);
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for SM events
+    sm_event_callback_registration.callback = &packet_handler;
+    sm_add_event_handler(&sm_event_callback_registration);
+
+    // register for HIDS
+    hids_device_register_packet_handler(packet_handler);
 }
 
 // HID Keyboard lookup
diff --git a/example/hog_mouse_demo.c b/example/hog_mouse_demo.c
index 2c4774c06..3023a8dff 100644
--- a/example/hog_mouse_demo.c
+++ b/example/hog_mouse_demo.c
@@ -116,9 +116,6 @@ const uint8_t adv_data_len = sizeof(adv_data);
 
 static void hog_mouse_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
 
     // setup l2cap and register for connection parameter updates
     l2cap_init();
@@ -129,8 +126,6 @@ static void hog_mouse_setup(void){
 
     // setup SM: Display only
     sm_init();
-    sm_event_callback_registration.callback = &packet_handler;
-    sm_add_event_handler(&sm_event_callback_registration);
     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);
     sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
@@ -146,7 +141,6 @@ static void hog_mouse_setup(void){
 
     // setup HID Device service
     hids_device_init(0, hid_descriptor_mouse_boot_mode, sizeof(hid_descriptor_mouse_boot_mode));
-    hids_device_register_packet_handler(packet_handler);
 
     // setup advertisements
     uint16_t adv_int_min = 0x0030;
@@ -157,6 +151,15 @@ static void hog_mouse_setup(void){
     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
     gap_advertisements_enable(1);
+
+    // register for events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    sm_event_callback_registration.callback = &packet_handler;
+    sm_add_event_handler(&sm_event_callback_registration);
+
+    hids_device_register_packet_handler(packet_handler);
 }
 
 // HID Report sending
diff --git a/example/hsp_ag_demo.c b/example/hsp_ag_demo.c
index 6bc6802ed..2503b4547 100644
--- a/example/hsp_ag_demo.c
+++ b/example/hsp_ag_demo.c
@@ -296,6 +296,8 @@ int btstack_main(int argc, const char * argv[]){
 
     hsp_ag_init(rfcomm_channel_nr);
     hsp_ag_register_packet_handler(&packet_handler);
+
+    // register for SCO packets
     hci_register_sco_packet_handler(&packet_handler);
 
     // parse human readable Bluetooth address
diff --git a/example/hsp_hs_demo.c b/example/hsp_hs_demo.c
index 523b004ed..a80de98c4 100644
--- a/example/hsp_hs_demo.c
+++ b/example/hsp_hs_demo.c
@@ -280,11 +280,6 @@ int btstack_main(int argc, const char * argv[]){
 
     sco_demo_init();
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-    hci_register_sco_packet_handler(&packet_handler);
-
     l2cap_init();
 
     sdp_init();
@@ -295,6 +290,13 @@ int btstack_main(int argc, const char * argv[]){
     rfcomm_init();
 
     hsp_hs_init(rfcomm_channel_nr);
+
+    // register for HCI events and SCO packets
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+    hci_register_sco_packet_handler(&packet_handler);
+
+    // register for HSP events
     hsp_hs_register_packet_handler(packet_handler);
 
 #ifdef HAVE_BTSTACK_STDIN
diff --git a/example/le_counter.c b/example/le_counter.c
index 336010fa1..c763a4e27 100644
--- a/example/le_counter.c
+++ b/example/le_counter.c
@@ -95,10 +95,6 @@ const uint8_t adv_data_len = sizeof(adv_data);
 
 static void le_counter_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup le device db
@@ -109,7 +105,6 @@ static void le_counter_setup(void){
 
     // setup ATT server
     att_server_init(profile_data, att_read_callback, att_write_callback);    
-    att_server_register_packet_handler(packet_handler);
 
     // setup battery service
     battery_service_server_init(battery);
@@ -124,6 +119,13 @@ static void le_counter_setup(void){
     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
     gap_advertisements_enable(1);
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for ATT event
+    att_server_register_packet_handler(packet_handler);
+
     // set one-shot timer
     heartbeat.process = &heartbeat_handler;
     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
diff --git a/example/le_data_channel_client.c b/example/le_data_channel_client.c
index a3269f125..438f91d28 100644
--- a/example/le_data_channel_client.c
+++ b/example/le_data_channel_client.c
@@ -339,14 +339,14 @@ int btstack_main(int argc, const char * argv[]){
     (void)argv;
 #endif
 
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     sm_init();
     sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
 
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // turn on!
     hci_power_control(HCI_POWER_ON);
 
diff --git a/example/le_data_channel_server.c b/example/le_data_channel_server.c
index 6461016f4..46fa89216 100644
--- a/example/le_data_channel_server.c
+++ b/example/le_data_channel_server.c
@@ -103,12 +103,7 @@ static uint8_t data_channel_buffer[TEST_PACKET_SIZE];
 
 static void le_data_channel_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
-    l2cap_register_packet_handler(&packet_handler);
 
     // setup le device db
     le_device_db_init();
@@ -119,6 +114,12 @@ static void le_data_channel_setup(void){
     // setup ATT server: iOS disconnects if ATT MTU Exchange fails
     att_server_init(profile_data, NULL, NULL);    
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    l2cap_register_packet_handler(&packet_handler);
+
     // le data channel setup
     l2cap_le_register_service(&packet_handler, TSPX_le_psm, LEVEL_0);
 
diff --git a/example/le_streamer.c b/example/le_streamer.c
index 3dd7540bc..5e002f1ac 100644
--- a/example/le_streamer.c
+++ b/example/le_streamer.c
@@ -136,10 +136,6 @@ static void next_connection_index(void){
 
 static void le_streamer_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &hci_packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup le device db
@@ -150,8 +146,14 @@ static void le_streamer_setup(void){
 
     // setup ATT server
     att_server_init(profile_data, NULL, att_write_callback);    
-    att_server_register_packet_handler(att_packet_handler);
     
+    // register for HCI events
+    hci_event_callback_registration.callback = &hci_packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for ATT events
+    att_server_register_packet_handler(att_packet_handler);
+
     // setup advertisements
     uint16_t adv_int_min = 0x0030;
     uint16_t adv_int_max = 0x0030;
diff --git a/example/le_streamer.gatt b/example/le_streamer.gatt
index 5fe3dfef3..cd4b5dd72 100644
--- a/example/le_streamer.gatt
+++ b/example/le_streamer.gatt
@@ -7,7 +7,7 @@ CHARACTERISTIC, GATT_SERVICE_CHANGED, READ,
 // Test Service
 PRIMARY_SERVICE, 0000FF10-0000-1000-8000-00805F9B34FB
 // Test Characteristic A, write_without_response as well as notify
-CHARACTERISTIC,  0000FF11-0000-1000-8000-00805F9B34FB, WRITE_WITHOUT_RESPONSE | NOTIFY | DYNAMIC,
+CHARACTERISTIC,  0000FF11-0000-1000-8000-00805F9B34FB, ENCRYPTION_KEY_SIZE_16 | WRITE_WITHOUT_RESPONSE | NOTIFY | DYNAMIC,
 // Test Characteristic B, write_without_response as well as notify
 CHARACTERISTIC,  0000FF12-0000-1000-8000-00805F9B34FB, WRITE_WITHOUT_RESPONSE | NOTIFY | DYNAMIC,
 
diff --git a/example/le_streamer_client.c b/example/le_streamer_client.c
index 012fb49ad..57d0ba8b3 100644
--- a/example/le_streamer_client.c
+++ b/example/le_streamer_client.c
@@ -437,10 +437,6 @@ int btstack_main(int argc, const char * argv[]){
     (void)argc;
     (void)argv;
 #endif
-
-    hci_event_callback_registration.callback = &hci_event_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     sm_init();
@@ -449,6 +445,9 @@ int btstack_main(int argc, const char * argv[]){
     // sm_init needed before gatt_client_init
     gatt_client_init();
 
+    hci_event_callback_registration.callback = &hci_event_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // use different connection parameters: conn interval min/max (* 1.25 ms), slave latency, supervision timeout, CE len min/max (* 0.6125 ms) 
     // gap_set_connection_parameters(0x06, 0x06, 4, 1000, 0x01, 0x06 * 2);
 
diff --git a/example/panu_demo.c b/example/panu_demo.c
index aef21445c..03e0bac1b 100644
--- a/example/panu_demo.c
+++ b/example/panu_demo.c
@@ -95,10 +95,6 @@ static void network_send_packet_callback(const uint8_t * packet, uint16_t size);
 
 static void panu_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // Initialize L2CAP 
     l2cap_init();
 
@@ -110,6 +106,10 @@ static void panu_setup(void){
 
     // Initialize network interface
     btstack_network_init(&network_send_packet_callback);
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
 }
 /* LISTING_END */
 
diff --git a/example/pbap_client_demo.c b/example/pbap_client_demo.c
index 1e706e367..279fc4c0c 100644
--- a/example/pbap_client_demo.c
+++ b/example/pbap_client_demo.c
@@ -229,13 +229,7 @@ int btstack_main(int argc, const char * argv[]){
 
     (void)argc;
     (void)argv;
-
-    sscanf_bd_addr(remote_addr_string, remote_addr);
         
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     // init L2CAP
     l2cap_init();
 
@@ -248,6 +242,12 @@ int btstack_main(int argc, const char * argv[]){
     // init PBAP Client
     pbap_client_init();
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    sscanf_bd_addr(remote_addr_string, remote_addr);
+
 #ifdef HAVE_BTSTACK_STDIN
     btstack_stdin_setup(stdin_process);
 #endif    
diff --git a/example/sdp_bnep_query.c b/example/sdp_bnep_query.c
index 6c5fa6af7..d2747f3ad 100644
--- a/example/sdp_bnep_query.c
+++ b/example/sdp_bnep_query.c
@@ -84,13 +84,12 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
 
 static void sdp_client_init(void){
+    // init L2CAP
+    l2cap_init();
 
     // register for HCI events
     hci_event_callback_registration.callback = &packet_handler;
     hci_add_event_handler(&hci_event_callback_registration);
-
-    // init L2CAP
-    l2cap_init();
 }
 /* LISTING_END */
 
diff --git a/example/sdp_general_query.c b/example/sdp_general_query.c
index d53af70d6..64d6e6d0e 100644
--- a/example/sdp_general_query.c
+++ b/example/sdp_general_query.c
@@ -75,13 +75,12 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
 
 static void sdp_client_init(void){
+    // init L2CAP
+    l2cap_init();
 
     // register for HCI events
     hci_event_callback_registration.callback = &packet_handler;
     hci_add_event_handler(&hci_event_callback_registration);
-
-    // init L2CAP
-    l2cap_init();
 }
 /* LISTING_END */
 
diff --git a/example/sdp_rfcomm_query.c b/example/sdp_rfcomm_query.c
index 9dfc06110..5532a1db9 100644
--- a/example/sdp_rfcomm_query.c
+++ b/example/sdp_rfcomm_query.c
@@ -132,19 +132,16 @@ static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uin
 
 int btstack_main(int argc, const char * argv[]);
 int btstack_main(int argc, const char * argv[]){
-
     (void)argc;
     (void)argv;
 
-    printf("Client HCI init done\r\n");
-        
+    // init L2CAP
+    l2cap_init();
+
     // register for HCI events
     hci_event_callback_registration.callback = &packet_handler;
     hci_add_event_handler(&hci_event_callback_registration);
 
-    // init L2CAP
-    l2cap_init();
-
     // turn on!
     hci_power_control(HCI_POWER_ON);
 
diff --git a/example/sm_pairing_central.c b/example/sm_pairing_central.c
index f19c3710f..9e64489a8 100644
--- a/example/sm_pairing_central.c
+++ b/example/sm_pairing_central.c
@@ -79,9 +79,6 @@ static btstack_packet_callback_registration_t sm_event_callback_registration;
 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
 
 static void sm_pairing_central_setup(void){
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup le device db
@@ -89,17 +86,23 @@ static void sm_pairing_central_setup(void){
 
     // setup SM: Display only
     sm_init();
-    sm_event_callback_registration.callback = &packet_handler;
-    sm_add_event_handler(&sm_event_callback_registration);
 
     // setup ATT server
-    att_server_init(profile_data, NULL, NULL);    
-    att_server_register_packet_handler(packet_handler);
+    att_server_init(profile_data, NULL, NULL);
 
     /**
      * Choose ONE of the following configurations
      */
 
+    // register handler
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    sm_event_callback_registration.callback = &packet_handler;
+    sm_add_event_handler(&sm_event_callback_registration);
+
+    att_server_register_packet_handler(packet_handler);
+
     // LE Legacy Pairing, Just Works
     sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
     sm_set_authentication_requirements(SM_AUTHREQ_NO_BONDING);
diff --git a/example/sm_pairing_peripheral.c b/example/sm_pairing_peripheral.c
index 049394ca0..0be46d1e1 100644
--- a/example/sm_pairing_peripheral.c
+++ b/example/sm_pairing_peripheral.c
@@ -83,10 +83,6 @@ const uint8_t adv_data_len = sizeof(adv_data);
 
 static void sm_peripheral_setup(void){
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     // setup le device db
@@ -94,8 +90,6 @@ static void sm_peripheral_setup(void){
 
     // setup SM: Display only
     sm_init();
-    sm_event_callback_registration.callback = &packet_handler;
-    sm_add_event_handler(&sm_event_callback_registration);
 
     /**
      * Choose ONE of the following configurations
@@ -103,7 +97,7 @@ static void sm_peripheral_setup(void){
 
     // LE Legacy Pairing, Just Works
     // sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
-    // sm_set_authentication_requirements(0);
+    // sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
 
     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
@@ -116,8 +110,8 @@ static void sm_peripheral_setup(void){
     // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION);
 
     // LE Secure Connections, Numeric Comparison
-    // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_YES_NO);
-    // sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
+    sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
+    sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION|SM_AUTHREQ_MITM_PROTECTION);
 
     // LE Legacy Pairing, Passkey entry initiator enter, responder (us) displays
     // sm_set_io_capabilities(IO_CAPABILITY_DISPLAY_ONLY);
@@ -127,7 +121,6 @@ static void sm_peripheral_setup(void){
 
     // setup ATT server
     att_server_init(profile_data, NULL, NULL);    
-    att_server_register_packet_handler(packet_handler);
 
     // setup advertisements
     uint16_t adv_int_min = 0x0030;
@@ -138,6 +131,17 @@ static void sm_peripheral_setup(void){
     gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
     gap_advertisements_enable(1);
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for SM events
+    sm_event_callback_registration.callback = &packet_handler;
+    sm_add_event_handler(&sm_event_callback_registration);
+
+    // register for ATT 
+    att_server_register_packet_handler(packet_handler);
 }
 
 /* LISTING_END */
@@ -154,11 +158,22 @@ static void sm_peripheral_setup(void){
 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     UNUSED(channel);
     UNUSED(size);
-
+    hci_con_handle_t con_handle;
     bd_addr_t addr;
     switch (packet_type) {
         case HCI_EVENT_PACKET:
             switch (hci_event_packet_get_type(packet)) {
+                case HCI_EVENT_LE_META:
+                    switch (hci_event_le_meta_get_subevent_code(packet)) {
+                        case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
+                            // setup new 
+                            con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet);
+                            sm_send_security_request(con_handle);
+                            break;
+                        default:
+                            break;
+                    }
+                    break;
                 case SM_EVENT_JUST_WORKS_REQUEST:
                     printf("Just Works requested\n");
                     sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
diff --git a/example/spp_and_le_counter.c b/example/spp_and_le_counter.c
index 62ade54c6..1a1a7f132 100644
--- a/example/spp_and_le_counter.c
+++ b/example/spp_and_le_counter.c
@@ -261,11 +261,6 @@ static void heartbeat_handler(struct btstack_timer_source *ts){
 int btstack_main(void);
 int btstack_main(void)
 {
-
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     rfcomm_init();
@@ -290,12 +285,13 @@ int btstack_main(void)
 
     // setup ATT server
     att_server_init(profile_data, att_read_callback, att_write_callback);    
-    att_server_register_packet_handler(packet_handler);
 
-    // set one-shot timer
-    heartbeat.process = &heartbeat_handler;
-    btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
-    btstack_run_loop_add_timer(&heartbeat);
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for ATT events
+    att_server_register_packet_handler(packet_handler);
 
     // setup advertisements
     uint16_t adv_int_min = 0x0030;
@@ -307,6 +303,11 @@ int btstack_main(void)
     gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
     gap_advertisements_enable(1);
 
+    // set one-shot timer
+    heartbeat.process = &heartbeat_handler;
+    btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
+    btstack_run_loop_add_timer(&heartbeat);
+
     // beat once
     beat();
 
diff --git a/example/spp_and_le_streamer.c b/example/spp_and_le_streamer.c
index 96f53f83f..f17211c6e 100644
--- a/example/spp_and_le_streamer.c
+++ b/example/spp_and_le_streamer.c
@@ -370,11 +370,6 @@ int btstack_main(int argc, const char * argv[])
     UNUSED(argc);
     (void)argv;
 
-
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     rfcomm_init();
@@ -403,6 +398,12 @@ int btstack_main(int argc, const char * argv[])
 
     // setup ATT server
     att_server_init(profile_data, att_read_callback, att_write_callback);    
+
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
+    // register for ATT events
     att_server_register_packet_handler(packet_handler);
 
     // setup advertisements
diff --git a/example/spp_streamer.c b/example/spp_streamer.c
index 9d456e315..5b1cdfaca 100644
--- a/example/spp_streamer.c
+++ b/example/spp_streamer.c
@@ -251,10 +251,6 @@ int btstack_main(int argc, const char * argv[])
     (void)argc;
     (void)argv;
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     rfcomm_init();
@@ -267,6 +263,10 @@ int btstack_main(int argc, const char * argv[])
     sdp_register_service(spp_service_buffer);
     // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // short-cut to find other SPP Streamer
     gap_set_class_of_device(TEST_COD);
 
diff --git a/example/spp_streamer_client.c b/example/spp_streamer_client.c
index b9abfe467..50820b6d8 100644
--- a/example/spp_streamer_client.c
+++ b/example/spp_streamer_client.c
@@ -266,15 +266,15 @@ int btstack_main(int argc, const char * argv[]){
     UNUSED(argc);
     (void)argv;
 
-    // register for HCI events
-    hci_event_callback_registration.callback = &packet_handler;
-    hci_add_event_handler(&hci_event_callback_registration);
-
     l2cap_init();
 
     rfcomm_init();
     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
 
+    // register for HCI events
+    hci_event_callback_registration.callback = &packet_handler;
+    hci_add_event_handler(&hci_event_callback_registration);
+
     // init SDP
     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);