首页 > 解决方案 > 如何在java中刷新蓝牙设备列表

问题描述

我正在尝试制作一个关于本地化的 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 ...");
            }
        }
    };

标签: javaandroid-studiobluetoothlocalization

解决方案


使用列表视图然后调用 notifyDataSetChanged()您的适配器对象


推荐阅读