AndroidStudio安卓原生开发_Android扫描附近指定的蓝牙设备_通过设备名称过滤_计算距离_离扫描设备近的显示的时候放在前面---Android原生开发工作笔记128
发布日期:2021-05-28 17:12:56 浏览次数:27 分类:精选文章

本文共 1451 字,大约阅读时间需要 4 分钟。

如何实现蓝牙设备的特定过滤与排序

在这个项目中,我们需要实现通过蓝牙进行设备扫描并进行特定型号设备的过滤。具体来说,我们关注体重秤设备的扫描与过滤。

过滤机制

我们采用蓝牙设备的uuid进行过滤。这是因为不同设备的uuid具有品牌特性,允许我们通过特定的uuid值来标识和筛选体重秤设备。

实现步骤

  • 检测蓝牙支持情况

    首先,我们需要判断该设备是否具备必要的蓝牙功能。这涉及到调用系统的api来检查蓝牙配置和权限。

  • 初始化蓝牙设备列表

    在已经具备蓝牙支持的基础上,我们初始化一个空的设备列表。这个列表将用于存储扫描到的蓝牙设备信息。

  • 实现Uuid过滤

    在扫描完成后,我们根据预期的uuid值进行筛选。这种方法能够确保只有目标型号的设备被显示。

  • 设备排序规则

    为了让用户体验更优,我们采用距离倒序排列。具体来说,扫描到的设备按照与扫描设备的远近顺序进行排序。离得近的设备更容易被用户发现。

    代码示例

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initBluetooth();
    }
    private void initBluetooth() {
    if (!getBluetoothManager().isBluetooth 돌아 обесп Lime android系统
    mBluetoothManager =.getSystemService조나BluetoothManager;
    ///
    mBluetoothManager.set.replaceAllJo bolch
    // 初始化设备列表
    List
    deviceList = new ArrayList<>();
    // 您需要根据实际需求添加Uuid过滤条件
    deviceList.clear();
    deviceList.addAll(mBluetoothManager.getPairedDevices());
    // 您可以根据实际需求添加Uuid过滤条件
    List
    targetUuids = new ArrayList<>(Arrays.asList(QN_produced_uuids));
    deviceList = deviceList.stream()
    . filter(device -> targetUuids.contains(device.getUuid()))
    . sorted((device1, device2) -> {
    int distance1 = calculateDistanceToUser(device1);
    int distance2 = calculateDistanceToUser(device2);
    return Integer.compare(distance2, distance1);
    });

    结论

    通过以上方法,我们能够实现对体重秤蓝牙设备的精准过滤和排序。这一机制能够提供更加直观和便捷的用户体验。

    上一篇:crontab定时管理器使用案例
    下一篇:解决VM宿主机(非虚拟机)VM1网卡无法获取正确的IP地址

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月19日 03时00分34秒