在Android 8.0(API第26级)和更高版本的设备上,您可以在试图通过蓝牙、BLE和wi - fi对其他设备进行配对时定制配对请求对话框

在运行Android 8.0(API第26级)和更高版本的设备上,您可以在试图通过蓝牙、BLE和wi - fi对其他设备进行配对时定制配对请求对话框。

在您的应用程序中,您可以指定用户是否看到了可能的伙伴设备列表,或者只是对一个配套设备的一个建议。您还可以过滤在配对请求对话框中出现的条目,比如type(蓝牙、BLE和wi - fi)或设备名称。

public class MyDeviceSelectionActivity {
    private CompanionDeviceManager mDeviceManager;
    private AssociationRequest mPairingRequest;
    private BluetoothDeviceFilter mDeviceFilter;

    private static final int SELECT_DEVICE_REQUEST_CODE = 42;

    @override
    public void onCreate() {
        // ...
        mDeviceManager = 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.
        mDeviceFilter = new BluetoothDeviceFilter.Builder()
                .setNamePattern(Pattern.compile("My device"))
                .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.
        mPairingRequest = new AssociationRequest.Builder()
                .addDeviceFilter(mDeviceFilter)
                .setSingleDevice(true)
                .build();

        // When the app tries to pair with the Bluetooth device, show the
        // appropriate pairing request dialog to the user.
        mDeviceManager.associate(mPairingRequest,
                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.
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值