Device discovery API

거의 모든 멀티스크린 경험은 사용 가능한 기기를 찾는 것에서 시작됩니다. 이 일반적인 작업을 간소화하기 위해 Device Discovery API를 제공합니다.

주변 사용자와의 공유 옵션이 포함된 대화상자
그림 1: 주변 사용자와 공유

기기 선택 대화상자 실행

기기 검색은 시스템 대화상자를 사용하여 사용자가 대상 기기를 선택할 수 있도록 합니다. 기기 선택 대화상자를 시작하려면 먼저 기기 검색 클라이언트를 가져오고 결과 수신기를 등록해야 합니다. registerForActivityResult와 마찬가지로 이 broadcast receiver는 무조건 활동 또는 프래그먼트 초기화 경로의 일부로 등록되어야 합니다.

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices)
}

Java

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices);
}

위의 코드 스니펫에는 정의되지 않은 handleDevices 객체가 있습니다. 사용자가 연결할 기기를 선택하고 SDK가 다른 기기에 성공적으로 연결되면 이 콜백은 선택된 Participants 목록을 수신합니다.

Kotlin

handleDevices = OnDevicePickerResultListener { participants -> participants.forEach {
    // Use participant info
  }
}

Java

handleDevices = participants -> {
   for (Participant participant : participants) {
      // Use participant info
   }
}

기기 선택 도구가 등록되면 devicePickerLauncher 인스턴스를 사용하여 실행합니다. DevicePickerLauncher.launchDevicePicker는 두 가지 매개변수, 즉 기기 필터 목록 (아래 섹션 참고)과 startComponentRequest를 사용합니다. startComponentRequest는 수신 기기에서 시작해야 하는 활동과 사용자에게 표시되는 요청 이유를 나타내는 데 사용됩니다.

Kotlin

devicePickerLauncher.launchDevicePicker(
  listOf(),
  startComponentRequest {
    action = "com.example.crossdevice.MAIN"
    reason = "I want to say hello to you"
  },
)

Java

devicePickerLauncher.launchDevicePickerFuture(
    Collections.emptyList(),
    new StartComponentRequest.Builder()
        .setAction("com.example.crossdevice.MAIN")
        .setReason("I want to say hello to you")
        .build());

연결 요청 수락

사용자가 기기 선택 도구에서 기기를 선택하면 수신 기기에 사용자에게 연결을 수락하도록 요청하는 대화상자가 표시됩니다. 수락되면 타겟 활동이 실행되며 onCreateonNewIntent에서 처리할 수 있습니다.

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  handleIntent(getIntent())
}

override fun onNewIntent(intent: Intent) {
  super.onNewIntent(intent)
  handleIntent(intent)
}

private fun handleIntent(intent: Intent) {
  val participant = Discovery.create(this).getParticipantFromIntent(intent)
  // Accept connection from participant (see below)
}

Java

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  handleIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  handleIntent(intent);
}

private void handleIntent(Intent intent) {
  Participant participant = Discovery.create(this).getParticipantFromIntent(intent);
  // Accept connection from participant (see below)
}

기기 필터

기기를 검색할 때는 당면한 사용 사례와 관련된 기기만 표시하도록 이러한 기기를 필터링하는 것이 일반적입니다. 예:

  • 카메라가 있는 기기로만 필터링하여 QR 코드 스캔 지원
  • 대형 화면 시청 환경을 위해 TV로만 필터링

이 개발자 프리뷰에서는 동일한 사용자가 소유한 기기를 필터링하는 기능부터 시작합니다.

DeviceFilter 클래스를 사용하여 기기 필터를 지정할 수 있습니다.

Kotlin

val deviceFilters = listOf(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY))

Java

List<DeviceFilter> deviceFilters =
    Arrays.asList(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY));

기기 필터를 정의한 후에는 기기 검색을 시작할 수 있습니다.

Kotlin

devicePickerLauncher.launchDevicePicker(deviceFilters, startComponentRequest)

Java

Futures.addCallback(
    devicePickerLauncher.launchDevicePickerFuture(deviceFilters, startComponentRequest),
    new FutureCallback<Void>() {
      @Override
      public void onSuccess(Void result) {
        // do nothing, result will be returned to handleDevices callback
      }

      @Override
      public void onFailure(Throwable t) {
        // handle error
      }
    },
    mainExecutor);

launchDevicePickersuspend 키워드를 사용하는 비동기 함수입니다.