@@ -55,6 +55,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
5555 m_numHandles = numHandles;
5656 m_pSvcDef = nullptr ;
5757 m_removed = 0 ;
58+ m_secondary = false ;
5859
5960} // NimBLEService
6061
@@ -133,7 +134,7 @@ bool NimBLEService::start() {
133134 ble_gatt_chr_def* pChr_a = nullptr ;
134135 ble_gatt_dsc_def* pDsc_a = nullptr ;
135136
136- svc[0 ].type = BLE_GATT_SVC_TYPE_PRIMARY;
137+ svc[0 ].type = m_secondary ? BLE_GATT_SVC_TYPE_SECONDARY : BLE_GATT_SVC_TYPE_PRIMARY;
137138 svc[0 ].uuid = &m_uuid.getNative ()->u ;
138139 svc[0 ].includes = NULL ;
139140
@@ -240,6 +241,12 @@ bool NimBLEService::start() {
240241
241242 }
242243
244+ if (m_secSvcVec.size () > 0 ){
245+ for (auto & it : m_secSvcVec) {
246+ it->start ();
247+ }
248+ }
249+
243250 NIMBLE_LOGD (LOG_TAG, " << start()" );
244251 return true ;
245252} // start
@@ -254,6 +261,44 @@ uint16_t NimBLEService::getHandle() {
254261} // getHandle
255262
256263
264+ /* *
265+ * @brief Creates a BLE service as a secondary service to the service this was called from.
266+ * @param [in] uuid The UUID of the secondary service.
267+ * @return A reference to the new secondary service object.
268+ */
269+ NimBLEService* NimBLEService::createService (const NimBLEUUID &uuid) {
270+ NIMBLE_LOGD (LOG_TAG, " >> createService - %s" , uuid.toString ().c_str ());
271+
272+ NimBLEServer* pServer = getServer ();
273+ NimBLEService* pService = new NimBLEService (uuid, 0 , pServer);
274+ m_secSvcVec.push_back (pService);
275+ pService->m_secondary = true ;
276+ pServer->serviceChanged ();
277+
278+ NIMBLE_LOGD (LOG_TAG, " << createService" );
279+ return pService;
280+ }
281+
282+
283+ /* *
284+ * @brief Adds a secondary service to this service which was either already created but removed from availability,\n
285+ * or created and later added.
286+ * @param [in] service The secondary service object to add.
287+ */
288+ void NimBLEService::addService (NimBLEService* service) {
289+ // If adding a service that was not removed add it and return.
290+ // Else reset GATT and send service changed notification.
291+ if (service->m_removed == 0 ) {
292+ m_secSvcVec.push_back (service);
293+ return ;
294+ }
295+
296+ service->m_secondary = true ;
297+ service->m_removed = 0 ;
298+ getServer ()->serviceChanged ();
299+ }
300+
301+
257302/* *
258303 * @brief Create a new BLE Characteristic associated with this service.
259304 * @param [in] uuid - The UUID of the characteristic.
0 commit comments