L'API Hub Activation vous permet de découvrir et d'activer de manière programmatique un hub Google Home. Cette fonctionnalité est particulièrement utile lorsque l'utilisateur n'a aucun autre moyen d'activer un hub, comme dans le cas d'un hub sans écran.
Utiliser l'API Hub Activation
L'API Hub Activation vous permet de créer une application capable de détecter et d'activer des hubs.
Obtenez une référence à
HubManagementTraitdans la structure :val hubManagementTrait = hubManagementTraitFlow.firstOrNull { it.metadata.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE } if (hubManagementTrait == null) { errorsEmitter.emit(HomeException.notFound("HubManagement trait isn't online")) }Identifiez les appareils compatibles avec un hub sur le réseau Wi-Fi :
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(), )Activez un appareil compatible avec un hub :
try { val unused = hubManagementTrait.activateHub(hub) } catch (e: Exception) { Log.d("Hub Activation", "Error activating hub $e") }