Android 8.0(API 레벨 26) 이상을 실행하는 기기에서는 블루투스, BLE 및 Wi-Fi를 통해 호환 기기와 페어링할 때 표시되는 페어링 요청 대화상자를 맞춤설정할 수 있습니다.
앱에서 사용자에게 가능한 호환 기기 목록을 표시할지 또는 호환 기기 추천을 하나만 표시할지 지정할 수 있습니다. 또한 페어링 요청 대화상자에 표시되는 항목을 유형(블루투스, BLE 및 Wi-Fi)별 또는 기기 이름별로 필터링할 수도 있습니다.
다음 코드 스니펫은 사용자에게 휴대용 기기를 블루투스로 연결된 '내 기기'라는 단일 호환 기기와 페어링할지 여부를 묻는 대화상자를 표시하는 방법을 보여줍니다.
Kotlin
class MyDeviceSelectionActivity : Activity() { private val deviceManager: CompanionDeviceManager by lazy(LazyThreadSafetyMode.NONE) { getSystemService(CompanionDeviceManager::class.java) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... // To skip filtering based on name and supported feature flags (UUIDs), // don't include calls to setNamePattern() and addServiceUuid(), // respectively. This example uses Bluetooth. val deviceFilter: BluetoothDeviceFilter = BluetoothDeviceFilter.Builder() .setNamePattern(Pattern.compile("My device")) .addServiceUuid(ParcelUuid(UUID(0x123abcL, -1L)), null) .build() // The argument provided in setSingleDevice() determines whether a single // device name or a list of device names is presented to the user as // pairing options. val pairingRequest: AssociationRequest = AssociationRequest.Builder() .addDeviceFilter(deviceFilter) .setSingleDevice(true) .build() // When the app tries to pair with the Bluetooth device, show the // appropriate pairing request dialog to the user. deviceManager.associate(pairingRequest, object : CompanionDeviceManager.Callback() { override fun onDeviceFound(chooserLauncher: IntentSender) { startIntentSenderForResult(chooserLauncher, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0) } override fun onFailure(error: CharSequence?) { // Handle failure } }, null) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { when (requestCode) { SELECT_DEVICE_REQUEST_CODE -> when(resultCode) { Activity.RESULT_OK -> { // User has chosen to pair with the Bluetooth device. val deviceToPair: BluetoothDevice = data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE) deviceToPair.createBond() // ... Continue interacting with the paired device. } } } } }
자바
public class MyDeviceSelectionActivity extends Activity { private CompanionDeviceManager deviceManager; private AssociationRequest pairingRequest; private BluetoothDeviceFilter deviceFilter; private static final int SELECT_DEVICE_REQUEST_CODE = 42; @override public void onCreate() { // ... deviceManager = getSystemService(CompanionDeviceManager.class); // To skip filtering based on name and supported feature flags (UUIDs), // don't include calls to setNamePattern() and addServiceUuid(), // respectively. This example uses Bluetooth. deviceFilter = new BluetoothDeviceFilter.Builder() .setNamePattern(Pattern.compile("My device"), null) .addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L))) .build(); // The argument provided in setSingleDevice() determines whether a single // device name or a list of device names is presented to the user as // pairing options. pairingRequest = new AssociationRequest.Builder() .addDeviceFilter(deviceFilter) .setSingleDevice(true) .build(); // When the app tries to pair with the Bluetooth device, show the // appropriate pairing request dialog to the user. deviceManager.associate(pairingRequest, new CompanionDeviceManager.Callback() { @Override public void onDeviceFound(IntentSender chooserLauncher) { startIntentSenderForResult(chooserLauncher, SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0); } }, null); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_DEVICE_REQUEST_CODE && resultCode == Activity.RESULT_OK) { // User has chosen to pair with the Bluetooth device. BluetoothDevice deviceToPair = data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE); deviceToPair.createBond(); // ... Continue interacting with the paired device. } } }