@@ -55,6 +55,7 @@ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLE
55
55
m_numHandles = numHandles;
56
56
m_pSvcDef = nullptr ;
57
57
m_removed = 0 ;
58
+ m_secondary = false ;
58
59
59
60
} // NimBLEService
60
61
@@ -133,7 +134,7 @@ bool NimBLEService::start() {
133
134
ble_gatt_chr_def* pChr_a = nullptr ;
134
135
ble_gatt_dsc_def* pDsc_a = nullptr ;
135
136
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;
137
138
svc[0 ].uuid = &m_uuid.getNative ()->u ;
138
139
svc[0 ].includes = NULL ;
139
140
@@ -240,6 +241,12 @@ bool NimBLEService::start() {
240
241
241
242
}
242
243
244
+ if (m_secSvcVec.size () > 0 ){
245
+ for (auto & it : m_secSvcVec) {
246
+ it->start ();
247
+ }
248
+ }
249
+
243
250
NIMBLE_LOGD (LOG_TAG, " << start()" );
244
251
return true ;
245
252
} // start
@@ -254,6 +261,44 @@ uint16_t NimBLEService::getHandle() {
254
261
} // getHandle
255
262
256
263
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
+
257
302
/* *
258
303
* @brief Create a new BLE Characteristic associated with this service.
259
304
* @param [in] uuid - The UUID of the characteristic.
0 commit comments