蓝牙添加到 ListView 两次而不是一次
我正在搜索范围内可用的蓝牙设备
。由于某种原因,每个找到的设备都会被添加到 ListView 中两次,而实际上它只应显示一次。
有谁知道我在这里做错了什么?代码如下。
当发现发现设备时
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.v("test", "test");
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// When discovery is finished, change the Activity title
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
I'm searching through available Bluetooth devices
in range. For some reason, each found device is added to the ListView twice, when it should only be shown once.
Does anyone have any idea what I am doing wrong here? Code included below.
When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.v("test", "test");
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// When discovery is finished, change the Activity title
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,您使用等于操作。这就是为什么它增加了两次。已配对设备列表中的一个。并且相同的项目再次添加到新发明的列表中。试试这个代码。
In your code you use the equals operation. that's why it adds the two times. one in already paired device list. And the same items are added again int the newly invented list. Try this code.