java - 如何在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 ...");
}
}
};
解决方案
使用列表视图然后调用 notifyDataSetChanged()
您的适配器对象
推荐阅读
- matplotlib - python 3数字在ubuntu上冻结
- django - 如何在 django 模型中查询特定的外部记录
- machine-learning - Inception V3 图像分类
- ssas - 动态测量测量 (MDX)
- c# - Serilog 排除了自定义记录器提供程序
- c# - MongoDb 命令获取更多 Mongo C# 驱动程序失败
- android - 房间持久性库@Query 不工作
- julia - 从离线安装加载 Ipopt 时 Julia 出错
- namespaces - PhpStorm 使用不正确的命名空间
- php - 在 wordpress 中动态更改样式表中的 url 路径