| **Warning:** The [`HubManagementTrait`](https://developers.home.google.com/reference/kotlin/com/google/home/google/HubManagementTrait) is restricted and isn't available for general use.

Using the Hub Activation API, you can build an app that can discover and
activate hubs. To do this:

1. Get a reference to the
   [`HubManagementTrait`](https://developers.home.google.com/reference/kotlin/com/google/home/google/HubManagementTrait) in
   the structure:

       val hubManagementTrait =
         hubManagementTraitFlow.firstOrNull {
           it.metadata.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE
         }
       if (hubManagementTrait == null) {
         errorsEmitter.emit(HomeException.notFound("HubManagement trait isn't online"))
       }

2. Identify any hub-capable devices on the Wi-Fi network:

       try {
         val unused = hubManagementTrait.discoverAvailableHubs()
       } catch (e: Exception) {
         Log.d(TAG_HUB_DISCOVERY, "Error discovering hubs $e")
         errorsEmitter.emit(e)
       }

       val hubManagementTraitFlow = structureFlow.flatMapLatest { it.trait(HubManagement) }

       val discoveredHubs =
         hubManagementTraitFlow
           .map { it.discoveredHubsList }
           .handleErrors()
           .flowOn(ioDispatcher)
           .stateIn(
             scope = CoroutineScope(viewModelScope.coroutineContext + ioDispatcher),
             started = SharingStarted.WhileSubscribed(),
             listOf(),
           )

3. Activate a hub-capable device:

       try {
         val unused = hubManagementTrait.activateHub(hub)
       } catch (e: Exception) {
         Log.d("Hub Activation", "Error activating hub $e")
       }

   | **Note** : Once a hub-capable device becomes an active hub, it is no longer returned by the `discoverAvailableHubs()` method.