connectToWifi in Future
This commit is contained in:
parent
e248dc3e0c
commit
ffb38996e8
@ -17,13 +17,17 @@ import com.example.lfrmobileapp.databinding.ActivityMainBinding;
|
|||||||
|
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private ActivityMainBinding binding;
|
private ActivityMainBinding binding;
|
||||||
private ExtendedSocket socket;
|
private ExtendedSocket socket;
|
||||||
private DataOutputStream outputStream;
|
|
||||||
private DataTransferAsyncTask dataTransferAsyncTask;
|
private DataTransferAsyncTask dataTransferAsyncTask;
|
||||||
Boolean connected = false;
|
Boolean connected = false;
|
||||||
|
|
||||||
@ -46,21 +50,29 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean connectToWifi(String ipAddress, int port){
|
public boolean connectToWifi(String ipAddress, int port){
|
||||||
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
new Thread(new Runnable(){
|
Future<Boolean> future = executor.submit(() -> {
|
||||||
public void run(){
|
Socket tempSocket = new Socket();
|
||||||
|
tempSocket.connect(new InetSocketAddress(ipAddress, port), 1000);
|
||||||
try {
|
try {
|
||||||
Socket tempSocket = new Socket(ipAddress, port);
|
|
||||||
socket = new ExtendedSocket(tempSocket);
|
socket = new ExtendedSocket(tempSocket);
|
||||||
socket.sendMessage("Verbindung aufbauen");
|
socket.sendMessage("Verbindung aufbauen");
|
||||||
connected = true;
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
socket.closeSocket();
|
||||||
connected = false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
connected = future.get();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.err.println("InterruptedException: " + e.getMessage());
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
System.err.println("ExecutionException: " + e.getCause().getMessage());
|
||||||
}
|
}
|
||||||
}).start();
|
|
||||||
|
executor.shutdown();
|
||||||
|
|
||||||
if(connected){
|
if(connected){
|
||||||
dataTransferAsyncTask = new DataTransferAsyncTask(socket);
|
dataTransferAsyncTask = new DataTransferAsyncTask(socket);
|
||||||
@ -70,5 +82,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendTelegram(String telegram)
|
||||||
|
{
|
||||||
|
this.dataTransferAsyncTask.writeTelegram(telegram);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user