@@ -52,6 +52,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
5252 m_numHandles = numHandles;
5353 m_pSvcDef = nullptr ;
5454 m_removed = 0 ;
55+ m_secondary = false ;
5556
5657} // NimBLEService
5758
@@ -130,7 +131,7 @@ bool NimBLEService::start() {
130131 ble_gatt_chr_def* pChr_a = nullptr ;
131132 ble_gatt_dsc_def* pDsc_a = nullptr ;
132133
133- svc[0 ].type = BLE_GATT_SVC_TYPE_PRIMARY;
134+ svc[0 ].type = m_secondary ? BLE_GATT_SVC_TYPE_SECONDARY : BLE_GATT_SVC_TYPE_PRIMARY;
134135 svc[0 ].uuid = &m_uuid.getNative ()->u ;
135136 svc[0 ].includes = NULL ;
136137
@@ -237,6 +238,12 @@ bool NimBLEService::start() {
237238
238239 }
239240
241+ if (m_secSvcVec.size () > 0 ){
242+ for (auto & it : m_secSvcVec) {
243+ it->start ();
244+ }
245+ }
246+
240247 NIMBLE_LOGD (LOG_TAG, " << start()" );
241248 return true ;
242249} // start
@@ -251,6 +258,44 @@ uint16_t NimBLEService::getHandle() {
251258} // getHandle
252259
253260
261+ /* *
262+ * @brief Creates a BLE service as a secondary service to the service this was called from.
263+ * @param [in] uuid The UUID of the secondary service.
264+ * @return A reference to the new secondary service object.
265+ */
266+ NimBLEService* NimBLEService::createService (const NimBLEUUID &uuid) {
267+ NIMBLE_LOGD (LOG_TAG, " >> createService - %s" , uuid.toString ().c_str ());
268+
269+ NimBLEServer* pServer = getServer ();
270+ NimBLEService* pService = new NimBLEService (uuid, 0 , pServer);
271+ m_secSvcVec.push_back (pService);
272+ pService->m_secondary = true ;
273+ pServer->serviceChanged ();
274+
275+ NIMBLE_LOGD (LOG_TAG, " << createService" );
276+ return pService;
277+ }
278+
279+
280+ /* *
281+ * @brief Adds a secondary service to this service which was either already created but removed from availability,\n
282+ * or created and later added.
283+ * @param [in] service The secondary service object to add.
284+ */
285+ void NimBLEService::addService (NimBLEService* service) {
286+ // If adding a service that was not removed add it and return.
287+ // Else reset GATT and send service changed notification.
288+ if (service->m_removed == 0 ) {
289+ m_secSvcVec.push_back (service);
290+ return ;
291+ }
292+
293+ service->m_secondary = true ;
294+ service->m_removed = 0 ;
295+ getServer ()->serviceChanged ();
296+ }
297+
298+
254299/* *
255300 * @brief Create a new BLE Characteristic associated with this service.
256301 * @param [in] uuid - The UUID of the characteristic.
0 commit comments