配套设备配对

在搭载 Android 8.0(API 级别 26)及更高版本的设备上,您可以自定义在尝试通过蓝牙、BLE 和 WLAN 与配套设备进行配对时显示的配对请求对话框。

在您的应用中,您可以指定用户看到的是可能的配套设备列表,还是仅看到针对配套设备的一条建议。您还可以过配对请求对话框中显示的项进行过滤,例如按类型(蓝牙、BLE 和 WLAN)或设备名称进行过滤。

以下代码段演示了如何显示一个对话框,询问用户是否希望将手持设备与名为“我的设备”的单个蓝牙连接配套设备配对:

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.
                    }
                }
            }
        }
    }
    

Java

    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.
            }
        }
    }