@@ -52,6 +52,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
52
52
m_numHandles = numHandles;
53
53
m_pSvcDef = nullptr ;
54
54
m_removed = 0 ;
55
+ m_secondary = false ;
55
56
56
57
} // NimBLEService
57
58
@@ -130,7 +131,7 @@ bool NimBLEService::start() {
130
131
ble_gatt_chr_def* pChr_a = nullptr ;
131
132
ble_gatt_dsc_def* pDsc_a = nullptr ;
132
133
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;
134
135
svc[0 ].uuid = &m_uuid.getNative ()->u ;
135
136
svc[0 ].includes = NULL ;
136
137
@@ -237,6 +238,12 @@ bool NimBLEService::start() {
237
238
238
239
}
239
240
241
+ if (m_secSvcVec.size () > 0 ){
242
+ for (auto & it : m_secSvcVec) {
243
+ it->start ();
244
+ }
245
+ }
246
+
240
247
NIMBLE_LOGD (LOG_TAG, " << start()" );
241
248
return true ;
242
249
} // start
@@ -251,6 +258,44 @@ uint16_t NimBLEService::getHandle() {
251
258
} // getHandle
252
259
253
260
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
+
254
299
/* *
255
300
* @brief Create a new BLE Characteristic associated with this service.
256
301
* @param [in] uuid - The UUID of the characteristic.
0 commit comments