@@ -53,6 +53,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
5353 m_pSvcDef = nullptr ;
5454 m_removed = 0 ;
5555 m_secondary = false ;
56+ m_pSecSvcDef = nullptr ;
5657
5758} // NimBLEService
5859
@@ -71,6 +72,13 @@ NimBLEService::~NimBLEService() {
7172 delete (m_pSvcDef);
7273 }
7374
75+ if (m_pSecSvcDef != nullptr ) {
76+ for (auto &it : m_secSvcVec) {
77+ delete it;
78+ }
79+ delete m_pSecSvcDef;
80+ }
81+
7482 for (auto &it : m_chrVec) {
7583 delete it;
7684 }
@@ -223,6 +231,19 @@ bool NimBLEService::start() {
223231 // end of services must indicate to api with type = 0
224232 svc[1 ].type = 0 ;
225233 m_pSvcDef = svc;
234+
235+ if (m_secSvcVec.size () > 0 ){
236+ size_t numSecSvcs = m_secSvcVec.size ();
237+ ble_gatt_svc_def** m_pSecSvcDef = new ble_gatt_svc_def*[numSecSvcs + 1 ];
238+ int i = 0 ;
239+ for (auto & it : m_secSvcVec) {
240+ it->start ();
241+ m_pSecSvcDef[i] = it->m_pSvcDef ;
242+ ++i;
243+ }
244+ m_pSecSvcDef[numSecSvcs] = nullptr ;
245+ m_pSvcDef->includes = (const ble_gatt_svc_def**)m_pSecSvcDef;
246+ }
226247 }
227248
228249 int rc = ble_gatts_count_cfg ((const ble_gatt_svc_def*)m_pSvcDef);
@@ -235,13 +256,6 @@ bool NimBLEService::start() {
235256 if (rc != 0 ) {
236257 NIMBLE_LOGE (LOG_TAG, " ble_gatts_add_svcs, rc= %d, %s" , rc, NimBLEUtils::returnCodeToString (rc));
237258 return false ;
238-
239- }
240-
241- if (m_secSvcVec.size () > 0 ){
242- for (auto & it : m_secSvcVec) {
243- it->start ();
244- }
245259 }
246260
247261 NIMBLE_LOGD (LOG_TAG, " << start()" );
0 commit comments