updateAdvertising function

Future<void> updateAdvertising({
  1. NodeRole? role,
  2. List<String>? groupIds,
})

Sends a role/group update command. Updates the singletons in the main isolate and restarts advertising.

Implementation

Future<void> updateAdvertising({NodeRole? role, List<String>? groupIds}) async {
  _log(
    'updateAdvertising requested: role=${role?.name}, groupIds=${groupIds?.join(",")}',
  );

  // Persist role change so it survives app restarts
  if (role != null) {
    await updateLocalNodeRole(role);
  }

  try {
    final manager = getIt<NearbyConnectionManager>();

    // Stop advertising to clear old endpoint name from the air
    await manager.stopAdvertising();

    // Update identity
    if (role != null) {
      manager.localRole = role;
      manager.syncEngine.localRole = role;
    }
    if (groupIds != null) {
      manager.localGroupIds = groupIds;
      manager.syncEngine.localGroupIds = groupIds;
    }

    // Restart advertising with the new endpoint name
    await manager.startAdvertising();

    // Reset backoff — the user wants to be found immediately
    _currentScanInterval = kInitialScanIntervalMs;

    _log('Identity updated — re-advertising');
  } catch (e) {
    _log('Failed to update advertising: $e');
  }
}