ประสบการณ์การใช้งานหลายอุปกรณ์เกือบทุกครั้งจะเริ่มต้นด้วยการค้นหาอุปกรณ์ที่ใช้ได้ ถึง ลดความซับซ้อนของงานทั่วไปนี้ เราจึงมี Device Discovery API

เปิดกล่องโต้ตอบการเลือกอุปกรณ์
การค้นหาอุปกรณ์ใช้กล่องโต้ตอบของระบบเพื่อให้ผู้ใช้เลือกอุปกรณ์เป้าหมายได้ ถึง
เริ่มกล่องโต้ตอบการเลือกอุปกรณ์ คุณต้องมีการค้นพบอุปกรณ์ก่อน
และลงทะเบียนผู้รับผลลัพธ์ โปรดทราบว่าเช่นเดียวกับ
registerForActivityResult
ผู้รับรายนี้จะต้องลงทะเบียนอย่างไม่มีเงื่อนไขเป็น
ของกิจกรรมหรือเส้นทางการเริ่มต้น Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices)
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
devicePickerLauncher = Discovery.create(this).registerForResult(this, handleDevices);
}
ในข้อมูลโค้ดด้านบน เรามีออบเจ็กต์ handleDevices
ที่ไม่ได้กำหนดไว้ หลัง
ผู้ใช้เลือกอุปกรณ์ที่จะเชื่อมต่อ และเมื่อ SDK สำเร็จแล้ว
เชื่อมต่อกับอุปกรณ์อื่นๆ โดย Callback นี้จะรับรายการ Participants
ไว้
handleDevices = OnDevicePickerResultListener { participants -> participants.forEach {
// Use participant info
}
}
handleDevices = participants -> {
for (Participant participant : participants) {
// Use participant info
}
}
หลังจากลงทะเบียนเครื่องมือเลือกอุปกรณ์แล้ว ให้เปิดใช้งานโดยใช้ devicePickerLauncher
อินสแตนซ์ DevicePickerLauncher.launchDevicePicker
ใส่พารามิเตอร์ 2 ตัว ได้แก่
รายการตัวกรองอุปกรณ์ (ดูส่วนด้านล่าง) และ startComponentRequest
startComponentRequest
ใช้เพื่อระบุว่าควรเริ่มกิจกรรมใด
อุปกรณ์ที่เป็นผู้รับ และเหตุผลสำหรับคำขอที่แสดงต่อผู้ใช้
devicePickerLauncher.launchDevicePicker(
listOf(),
startComponentRequest {
action = "com.example.crossdevice.MAIN"
reason = "I want to say hello to you"
},
)
devicePickerLauncher.launchDevicePickerFuture(
Collections.emptyList(),
new StartComponentRequest.Builder()
.setAction("com.example.crossdevice.MAIN")
.setReason("I want to say hello to you")
.build());
ยอมรับคำขอเชื่อมต่อ
เมื่อผู้ใช้เลือกอุปกรณ์ในเครื่องมือเลือกอุปกรณ์ กล่องโต้ตอบจะปรากฏขึ้นบน
อุปกรณ์ที่เป็นผู้รับเพื่อขอให้ผู้ใช้ยอมรับการเชื่อมต่อ เมื่อยอมรับแล้ว ฟิลด์
เริ่มกิจกรรมเป้าหมายแล้ว ซึ่งจัดการได้ใน onCreate
และ
onNewIntent
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)
}
@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)
}
ตัวกรองอุปกรณ์
เมื่อค้นพบอุปกรณ์ คุณมักจะต้องกรองอุปกรณ์เหล่านี้ให้แสดงเฉพาะ แสดงตัวเลือกที่เกี่ยวข้องกับกรณีการใช้งานที่มีอยู่ เช่น
- การกรองไปยังอุปกรณ์ที่มีกล้องเท่านั้นเพื่อช่วยสแกนคิวอาร์โค้ด
- การกรองไปยังทีวีเท่านั้นเพื่อประสบการณ์การรับชมบนหน้าจอขนาดใหญ่
สำหรับเวอร์ชันตัวอย่างสำหรับนักพัฒนาซอฟต์แวร์นี้ เราจะเริ่มด้วยความสามารถในการกรองเป็น อุปกรณ์ที่เป็นของผู้ใช้คนเดียวกัน
คุณจะระบุตัวกรองอุปกรณ์ได้โดยใช้คลาส DeviceFilter
ดังนี้
val deviceFilters = listOf(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY))
List<DeviceFilter> deviceFilters =
Arrays.asList(DeviceFilter.trustRelationshipFilter(MY_DEVICES_ONLY));
เมื่อกำหนดตัวกรองอุปกรณ์แล้ว คุณจะเริ่มการค้นหาอุปกรณ์ได้
devicePickerLauncher.launchDevicePicker(deviceFilters, startComponentRequest)
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);
โปรดสังเกตว่า launchDevicePicker
เป็นฟังก์ชันอะซิงโครนัสที่ใช้ฟังก์ชัน
คีย์เวิร์ด suspend
รายการ