허브 활성화 API를 사용하면 Google Home 허브를 프로그래매틱 방식으로 검색하고 활성화할 수 있습니다. 화면이 없는 허브와 같이 사용자가 허브를 활성화할 다른 방법이 없는 경우에 특히 유용합니다.
허브 활성화 API 사용
허브 활성화 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") }