Skip to content
Snippets Groups Projects
Commit 40689b7e authored by akwizgran's avatar akwizgran
Browse files

Avoid NoSuchElementException if the connection stalls.

parent 0728f769
No related branches found
No related tags found
No related merge requests found
...@@ -19,12 +19,16 @@ abstract class BluetoothTest { ...@@ -19,12 +19,16 @@ abstract class BluetoothTest {
out.println(CHALLENGE); out.println(CHALLENGE);
System.out.println("Sent challenge: " + CHALLENGE); System.out.println("Sent challenge: " + CHALLENGE);
Scanner in = new Scanner(s.getInputStream()); Scanner in = new Scanner(s.getInputStream());
String response = in.nextLine(); if(in.hasNextLine()) {
System.out.println("Received response: " + response); String response = in.nextLine();
if(BluetoothClientTest.RESPONSE.equals(response)) { System.out.println("Received response: " + response);
System.out.println("Correct response"); if(BluetoothClientTest.RESPONSE.equals(response)) {
System.out.println("Correct response");
} else {
System.out.println("Incorrect response");
}
} else { } else {
System.out.println("Incorrect response"); System.out.println("No response");
} }
s.dispose(true); s.dispose(true);
} catch(IOException e) { } catch(IOException e) {
...@@ -36,14 +40,18 @@ abstract class BluetoothTest { ...@@ -36,14 +40,18 @@ abstract class BluetoothTest {
void receiveChallengeAndSendResponse(StreamTransportConnection s) { void receiveChallengeAndSendResponse(StreamTransportConnection s) {
try { try {
Scanner in = new Scanner(s.getInputStream()); Scanner in = new Scanner(s.getInputStream());
String challenge = in.nextLine(); if(in.hasNextLine()) {
System.out.println("Received challenge: " + challenge); String challenge = in.nextLine();
if(BluetoothServerTest.CHALLENGE.equals(challenge)) { System.out.println("Received challenge: " + challenge);
PrintStream out = new PrintStream(s.getOutputStream()); if(BluetoothServerTest.CHALLENGE.equals(challenge)) {
out.println(RESPONSE); PrintStream out = new PrintStream(s.getOutputStream());
System.out.println("Sent response: " + RESPONSE); out.println(RESPONSE);
System.out.println("Sent response: " + RESPONSE);
} else {
System.out.println("Incorrect challenge");
}
} else { } else {
System.out.println("Incorrect challenge"); System.out.println("No challenge");
} }
s.dispose(true); s.dispose(true);
} catch(IOException e) { } catch(IOException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment