Hub Activation API を使用すると、Google Home Hub をプログラムで検出して有効にできます。これは、画面のないハブなど、ユーザーがハブを有効にする他の方法がない場合に特に便利です。
Hub Activation API を使用する
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") }