באמצעות Hub Activation API אפשר לגלות ולהפעיל באופן פרוגרמטי את Google Home Hub. היא שימושית במיוחד כשאין למשתמש דרך אחרת להפעיל את הרכזת, למשל אם אין לה מסך.
באמצעות 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") }