如何解决蓝牙两次添加到ListView中,而不是一次
| 我正在搜索范围内的可用“ 0”。出于某种原因,每个找到的设备只应显示一次,就被两次添加到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);
}
}
解决方法
在代码中,您使用equals操作。这就是为什么将两次相加。已配对的设备列表中的一个。并将相同的项目再次添加到新发明的列表中。试试这个代码。
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice 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) {
mNewDevicesArrayAdapter.add(device.getName() + \"\\n\" + device.getAddress());
}
// When discovery is finished,change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。