Hub Activation API מאפשר לכם לגלות ולהפעיל באופן פרוגרמטי רכזת Google Home. האפשרות הזו שימושית במיוחד כשאין למשתמש דרך אחרת להפעיל את הרכזת, כמו במקרה של רכזת ללא מסך.
באמצעות Hub Activation API, אפשר ליצור אפליקציה שיכולה לגלות ולהפעיל רכזות. לשם כך:
מקבלים הפניה אל
HubManagementTraitבמבנה:val hubManagementTrait = hubManagementTraitFlow.firstOrNull { it.metadata.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE } if (hubManagementTrait == null) { errorsEmitter.emit(HomeException.notFound("HubManagement trait isn't online")) }זיהוי מכשירים עם יכולת של רכזת ברשת 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(), )הפעלת מכשיר עם אפשרות לשימוש כרכזת:
try { val unused = hubManagementTrait.activateHub(hub) } catch (e: Exception) { Log.d("Hub Activation", "Error activating hub $e") }