I have run and debugged this java code in Android and I have noticed that the return true
has no effect because it act as a break
and then the return false
is finally executed. I have used the Android Studio and its Step Over function for debugging.
protected Boolean doInBackground(Void... params) {
// Cancel discovery because it will slow down the connection
mAdapter.cancelDiscovery();
// Start connection attempt
for(int i=0;i<10;i++) {
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
// connection successful [it should return true]
return true;
}
catch (IOException connectException) {
// Unable to connect; close the socket and get out
try { mmSocket.close(); }
catch (IOException closeException) { }
}
}
// connection failed 10 times [but it always returns false]
return false;
}
UPDATE:
Like EJP and others say, return true
does not act as a break. The doInBackground
function runs as expected. The problem was that, the AsyncTask was always cancelled just after the execution of return true
because of a external code block. I'm sorry about that.
This post can be closed. Thank you.