我正在尝试制作一个关于本地化的 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/