java - 我如何刷新java中的蓝牙设备列表

标签 java android-studio bluetooth localization

我正在尝试制作一个关于本地化的 Android 应用程序,但是我正在尝试制作我的智能手机找到的蓝牙设备列表,并每 10 秒刷新一次列出信息。我想这样做是因为我还记录信号强度 (RSSI),并希望记录信标在移动时超出范围或信号变弱的情况。因此,刷新列表将对此有所帮助。我在下面提供了一些代码。

@Override protected void onResume() {
    super.onResume();
    registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
        registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
        registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
    }

    private final BroadcastReceiver devicesFoundReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int rssid = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
                listAdapter.add(device.getName() + "\n" + device.getAddress() + "\n" + " RSSI: " + rssid + "dBm");
                listAdapter.notifyDataSetChanged();
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                scanningButton.setText("Scan Bluetooth Devices");
            }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
                scanningButton.setText("Scanning in progress ...");
            }
        }
    };

最佳答案

使用 ListView ,然后在适配器对象上调用notifyDataSetChanged()

关于java - 我如何刷新java中的蓝牙设备列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862100/

相关文章:

java - 通过 Spring Boot 和 Vaadin 传播代码更​​改,无需在 Eclipse 中重新启动应用程序

android - 根据 Android 专用设备文档找不到 getContext()

Android在蓝牙套接字上设置超时

ios - 与非 iOS 设备的多点蓝牙连接

Android:如何通过蓝牙浏览远程设备文件系统?

java - Log4j2 : RollingFile appender does not write logs into file

java - App Engine 的替代/兼容数据存储?

Java JPanels 在框架中不可见

java - 为什么Room数据库在重启应用后会丢失数据?

java - 错误 'Didn' t 在路径 : DexPathList' 上找到类