@@ -410,6 +410,36 @@ void ScanCallbacks::onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
410410 }
411411 SS2K_LOG (BLE_CLIENT_LOG_TAG, " Found device %s with %s" , aDevName.c_str (), servicesStr.c_str ());
412412 if (serviceInfo) {
413+ // Add device to foundDevices list
414+ JsonDocument devices;
415+ const char * foundDevicesJson = userConfig->getFoundDevices ();
416+ if (foundDevicesJson[0 ] != ' \0 ' ) {
417+ deserializeJson (devices, foundDevicesJson);
418+ }
419+
420+ bool isDuplicateLocal = false ;
421+ for (JsonPair kv : devices.as <JsonObject>()) {
422+ JsonObject obj = kv.value ().as <JsonObject>();
423+ if (obj[" name" ] && obj[" name" ] == aDevName) {
424+ isDuplicateLocal = true ;
425+ break ;
426+ }
427+ }
428+
429+ if (!isDuplicateLocal) {
430+ String deviceKey = " device " + String (devices.size ());
431+ devices[deviceKey][" name" ] = aDevName;
432+ // Workaround for IC4 not advertising FTMS as the first service.
433+ if (advertisedDevice->isAdvertisingService (FITNESSMACHINESERVICE_UUID)) {
434+ devices[deviceKey][" UUID" ] = FITNESSMACHINESERVICE_UUID.toString ();
435+ } else {
436+ devices[deviceKey][" UUID" ] = serviceInfo->serviceUUID .toString ();
437+ }
438+ String output;
439+ serializeJson (devices, output);
440+ userConfig->setFoundDevices (output);
441+ }
442+
413443 SS2K_LOG (BLE_CLIENT_LOG_TAG, " Supported Device: %s with service %s" , aDevName.c_str (), serviceInfo->name .c_str ());
414444 const NimBLEUUID& primaryServiceUUID = serviceInfo->serviceUUID ;
415445 // check to see if we're already connected to this device
@@ -528,48 +558,7 @@ void SpinBLEClient::scanProcess(int duration) {
528558}
529559
530560void ScanCallbacks::onScanEnd (const NimBLEScanResults& results, int reason) {
531- int count = results.getCount ();
532- JsonDocument devices;
533-
534- // Check if 'devices' JSON document already exists and has content; if so, deserialize it.
535- const char * foundDevicesJson = userConfig->getFoundDevices ();
536- if (foundDevicesJson[0 ] != ' \0 ' ) {
537- deserializeJson (devices, userConfig->getFoundDevices ());
538- }
539-
540- for (int i = 0 ; i < count; i++) {
541- const NimBLEAdvertisedDevice* d = results.getDevice (i);
542-
543- // Check for duplicates by name or address before adding
544- bool isDuplicate = false ;
545- for (JsonPair kv : devices.as <JsonObject>()) {
546- JsonObject obj = kv.value ().as <JsonObject>();
547- if (obj[" name" ] && obj[" name" ] == spinBLEClient.adevName2UniqueName (d)) {
548- isDuplicate = true ;
549- break ;
550- }
551- }
552-
553- if (!isDuplicate && d->haveServiceUUID () && isDeviceSupported (d, spinBLEClient.adevName2UniqueName (d).c_str ())) {
554- String device = " device " + String (devices.size ()); // Use the current size to index the new device
555-
556- devices[device][" name" ] = spinBLEClient.adevName2UniqueName (d);
557-
558- // Workaround for IC4 not advertising FTMS as the first service.
559- // Potentially others may need to be added in the future.
560- // The symptom was the bike name not showing up in the HTML.
561- if (d->haveServiceUUID () && d->isAdvertisingService (FITNESSMACHINESERVICE_UUID)) {
562- devices[device][" UUID" ] = FITNESSMACHINESERVICE_UUID.toString ();
563- } else {
564- devices[device][" UUID" ] = d->getServiceUUID ().toString ();
565- }
566- }
567- }
568-
569- String output;
570- serializeJson (devices, output);
571- SS2K_LOG (BLE_CLIENT_LOG_TAG, " Found Devices: %s" , output.c_str ());
572- userConfig->setFoundDevices (output); // Save the updated JSON document
561+ SS2K_LOG (BLE_CLIENT_LOG_TAG, " Scan Ended" );
573562}
574563
575564// remove the last connected BLE Power Meter
0 commit comments