웨어러블 데이터 영역에 액세스

Data Layer API를 호출하려면 Wearable 클래스를 사용하여 DataClient MessageClient와 같은 다양한 클라이언트 클래스의 인스턴스를 가져옵니다.

다음 관련 리소스를 참고하세요.

참고: Data Layer API는 Android 기기 또는 Wear OS 시계에서만 메시지를 전송하고 데이터를 동기화할 수 있습니다. 즉, Wear OS 기기가 iOS 기기와 페어링된 경우 Data Layer API는 작동하지 않습니다.

따라서 네트워크와 통신하는 기본 방법으로 Data Layer API를 사용하면 안 됩니다. 대신 모바일 앱과 동일한 패턴(약간의 차이는 있음)을 따르세요.

최소 클라이언트 사용

다음 예와 같은 최소 클라이언트로 시작하면 됩니다. 자세한 내용은 Google API 액세스를 참고하세요.

Kotlin

val dataClient: DataClient = Wearable.getDataClient(context)

Java

DataClient dataClient = Wearable.getDataClient(context);

유효한 Android 컨텍스트라면 무엇이든 괜찮습니다. Activity의 범위 내에서 API를 사용하고 있다면 Wearable 클래스의 getDataClient(activity) 메서드를 사용하세요. 이렇게 하면 사용자에게 Google Play 서비스 버전을 업데이트하라는 메시지가 표시될 때와 같은 특정 상호작용이 알림이 아닌 대화상자로 표시됩니다.

기본적으로 리스너 콜백은 앱의 기본 UI 스레드에서 이루어집니다. 다른 스레드에서 콜백을 실행하려면 WearableOptions 객체를 사용하여 맞춤 Looper를 지정합니다.

Kotlin

val dataClient: DataClient =
        Wearable.WearableOptions.Builder().setLooper(myLooper).build().let { options ->
            Wearable.getDataClient(context, options)
        }

Java

WearableOptions options = new WearableOptions.Builder().setLooper(myLooper).build();
DataClient dataClient = Wearable.getDataClient(context, options);

자세한 내용은 WearableOptions.Builder 참조를 확인하세요.

DataClient, MessageClient와 같은 Wearable API 클라이언트는 저렴한 비용으로 만들 수 있습니다. API 클라이언트를 필요한 횟수만큼 만들 수 있습니다. 앱에 적합한 스타일을 사용하세요. 등록된 리스너 집합과 같은 클라이언트 상태는 모든 클라이언트에서 공유되며 앱 실행 중에 Google Play 서비스가 업데이트되는 경우에도 유지됩니다.