I don't udnerstand why this code is giving me a NoSuchElementException
Essentially what I'm doing is iterating across a HashSet of Gatt servers and closing their connections with a device if their device addresses match.
BluetoothDevice device = mDeselectedDeviceData.device;
Iterator<BluetoothGatt> it = BleManager.getInstance().myGattConnections.iterator();
while (it.hasNext()) {
if(it.next().getDevice().getAddress() == device.getAddress()){
it.next().close();
}
}
This line
if(it.next().getDevice().getAddress() == device.getAddress()){
is throwing the error
>NoSuchElementException
However, it.hasNext() has is true if I log it
while (it.hasNext()) {
Log.(TAG,"it.hasNext() is "+String.valueOf(it.hasNext())); // Prints true
if(it.next().getDevice().getAddress() == device.getAddress()){
it.next().close();
}
}
if(it.next().getDevice().getAddress() == device.getAddress()){
it.next().close();
}
You call it.next() twice here, and it advances the iterator two positions. I'm pretty sure what you mean here is
BluetoothGatt gatt = it.next();
if(gatt.getDevice().getAddress() == device.getAddress()){
gatt.close();
}