2
I am programming a bluetooth app in Android Studio. I have an arrayList and I place devices that have been discovered in a bluetooth connection. It turns out that when I perform the task of searching new devices for the second, third ... time it add the same device being that it was already in the list.
That’s the part of the code in question:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int i = 0;
while(i < itens.size())
{
Log.i("Script", "Elemento na lista: "+itens.get(i).getNome());
i++;
}
// Get the "RSSI" to get the signal strength as integer,
// but should be displayed in "dBm" units
Item dispositivo = new Item();
dispositivo.setNome(device.getName());
dispositivo.setMac(device.getAddress());
if (!itens.contains(device.getName())) {
Log.i("Script ", "Dispositivo " +device.getName()+" não existe na lista, ele foi adicionado");
updateList(dispositivo);
}
}
}
};
At the first execution of the method, then appears in my Log:
Note that, he verified that a device did not exist in the list and added it, then to the second device, as the list already had an item he was shown (Element in the list: Galaxy J2 Prime), checked if it existed, as not, was added to the list(Jir device does not exist in the list, it has been added). So far so good!
It turns out that by method calling a second, third time, it always adds the same elements in the list:
You can see that now the elements repeated in the List, I believe that the error is in the comparison, items.contais(), but I do not know how to solve, some help?
What kind of list
itens
what do you use? It’s fromString
? The mistake really is initens.contains()
which seems to be comparing two objects of different types oneString
and another that seems to be oneItem
.– LeoSantana