메시지의 송수신


당신은 MessageApi를 이용하여 메시지를 전송하고 메시지에 다음 아이템들을 첨부할 수 있다:

  • 임의의 페이로드 (선택사항)
  • 메시지의 액션을 판별하는 고유한 경로

데이터 아이템과는 달리, 휴대용과 웨어러블 앱 간의 동기화는 없다. 메시지는 액티비티를 시작하기 위해 웨어러블에 메시지를 보내는 것과 같은 fire-and-forget(보내고 잊어버리는) 작업 의미를 갖는 단방향 통신 메카니즘이다. 또한 당신은 접속의 한쪽에서 메시지를 전송하고 메시지를 회신하는 몇몇 작업들을 수행하는 요청/응답 모델의 메시지를 사용할 수 있다.

메시지 송신


다음 예는 액티비티를 시작하기 위해 다른쪽 연결에 지시하는 메시지를 전송하는 방법을 보여준다. 이 호출은 메시지가 수신 될 때까지 블로킹하거나 요청 시간이 초과 될 때 동기적으로 만들어진다:

메모: Google Play Services와 통신에서 각각 사용 할 때 Google Play services에 비동기 및 동기 호출에 관하여 읽어라.

Node node; // the connected device to send the message to
GoogleApiClient mGoogleApiClient;
public static final START_ACTIVITY_PATH = "/start/MainActivity";
...

   
SendMessageResult result = Wearable.MessageApi.sendMessage(
            mGoogleApiClient
, node, START_ACTIVITY_PATH, null).await();
   
if (!result.getStatus().isSuccess()) {
       
Log.e(TAG, "ERROR: failed to send Message: " + result.getStatus());
   
}

여기 당신이 잠재적으로 메시지를 보낼 수 있는 연결된 노드들의 리스트를 얻을 수 있는 방법이 간단히 나와있다:

private Collection<String> getNodes() {
   
HashSet <String>results= new HashSet<String>();
   
NodeApi.GetConnectedNodesResult nodes =
           
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
   
for (Node node : nodes.getNodes()) {
        results
.add(node.getId());
   
}
   
return results;
}

메시지 수신


수신된 메시지를 통지 받기 위해 당신은 메시지 이벤트를 위한 리스너를 구현해야 한다. 이 예제는 앞의 예제에서 메시지를 전송하는데 사용한 START_ACTIVITY_PATH를 확인하여 이 작업을 수행하는 방법을 보여준다. 만약 이 조건이 true라면, 특정 액티비티가 시작된다.

@Override
public void onMessageReceived(MessageEvent messageEvent) {
   
if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {
       
Intent startIntent = new Intent(this, MainActivity.class);
        startIntent
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity
(startIntent);
   
}
}

이것은 단지 더 많은 구현 세부사항을 필요로 하는 정보일 뿐이다. 데이터 계층 이벤트들의 수신에서 전체 리스너 서비스나 액티비티의 구현 방법에 대해 배워본다.

'Android - Building Apps for Wearables > Sending and Syncing Data' 카테고리의 다른 글

Handling Data Layer Events  (0) 2014.09.10
Transferring Assets  (0) 2014.09.10
Syncing Data Items  (0) 2014.09.10
Accessing the Wearable Data Layer  (0) 2014.09.10
Sending and Syncing Data  (0) 2014.09.10
Posted by 레미파
,