</map> | </map> | ||||
</option> | </option> | ||||
</component> | </component> | ||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/build/classes" /> | <output url="file://$PROJECT_DIR$/build/classes" /> | ||||
</component> | </component> | ||||
<component name="ProjectType"> | <component name="ProjectType"> |
import java.util.Objects; | import java.util.Objects; | ||||
public class Communication { | public class Communication { | ||||
public boolean sendData(String data, String ip, int port) { | |||||
try { | |||||
Socket socket = new Socket(ip, port); | |||||
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream()); | |||||
DOS.writeUTF(data); | |||||
socket.close(); | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
return true; //ToDo: change to false to test connection - currently always true is delivered | |||||
} | |||||
return true; | |||||
} | |||||
public String telegram(int[] wheels, int strenght){ | public String telegram(int[] wheels, int strenght){ | ||||
String tel = ""; | |||||
tel += "0;"; //Manuel mode | |||||
String tel = "aa"; | |||||
tel += "0"; //Manuel mode | |||||
for(int i = 0; i < wheels.length; i++){ | for(int i = 0; i < wheels.length; i++){ | ||||
if(wheels[i] == 1){ | if(wheels[i] == 1){ | ||||
tel += Float.toString(((float)strenght)/100) + ";"; | |||||
tel += ";" + Float.toString(((float)strenght)/100) ; | |||||
}else if(wheels[i] == -1) | }else if(wheels[i] == -1) | ||||
tel += Float.toString(((float)-strenght)/100) + ";"; | |||||
tel += ";" + Float.toString(((float)-strenght)/100); | |||||
else{ | else{ | ||||
tel += "0.00;"; | |||||
tel += ";0.00"; | |||||
} | } | ||||
} | } | ||||
Log.v("TAG", tel + "\n"); | |||||
tel += "zz"; | |||||
return tel; | return tel; | ||||
} | } | ||||
public String telegram(boolean startStop, ArrayList<String> list){ | public String telegram(boolean startStop, ArrayList<String> list){ | ||||
String tel = ""; | String tel = ""; | ||||
tel += "1;"; //Autonomous mode | |||||
tel += "1"; //Autonomous mode | |||||
if(startStop){ | if(startStop){ | ||||
tel += "1;"; | |||||
tel += ";1"; | |||||
}else{ | }else{ | ||||
tel += "0;"; | |||||
tel += ";0"; | |||||
} | } | ||||
for (String i : list) { | for (String i : list) { | ||||
if(i.equals("Links")){ | if(i.equals("Links")){ | ||||
tel += "0;"; | |||||
tel += ";0"; | |||||
}else if (i.equals("Rechts")){ | }else if (i.equals("Rechts")){ | ||||
tel += "1;"; | |||||
tel += ";1"; | |||||
} | } | ||||
} | } | ||||
Log.v("TAG", tel + "\n"); | |||||
return tel; | return tel; | ||||
} | } | ||||
} | } |
package com.example.lfrmobileapp; | |||||
import android.os.AsyncTask; | |||||
import android.util.Log; | |||||
import java.io.DataOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.OutputStream; | |||||
import java.net.Socket; | |||||
import java.util.concurrent.BlockingQueue; | |||||
import java.util.concurrent.LinkedBlockingQueue; | |||||
import java.util.concurrent.TimeUnit; | |||||
public class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> { | |||||
private static final double sendRatePerSecond = 4.0; | |||||
private boolean connected; | |||||
private final ExtendedSocket socket; | |||||
private BlockingQueue<String> blockingQueue; | |||||
public DataTransferAsyncTask(ExtendedSocket socket) { | |||||
this.socket = socket; | |||||
this.connected = true; | |||||
this.blockingQueue = new LinkedBlockingQueue<>(); | |||||
} | |||||
public void writeTelegram(String telegram) { | |||||
// Schau mal hier bei Fehler | |||||
blockingQueue.offer(telegram); | |||||
} | |||||
@Override | |||||
protected Void doInBackground(Void... params) { | |||||
while(!isCancelled() && connected) { | |||||
try { | |||||
synchronized (socket) { | |||||
String message = blockingQueue.poll((long) (1000.0/sendRatePerSecond), TimeUnit.MILLISECONDS); | |||||
if(message != null){ | |||||
Log.v("TAG", message + "\n"); | |||||
socket.sendMessage(message); | |||||
} | |||||
} | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
connected = false; | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
} |
package com.example.lfrmobileapp; | |||||
import java.io.DataOutputStream; | |||||
import java.io.IOException; | |||||
import java.net.Socket; | |||||
public class ExtendedSocket { | |||||
private final Socket socket; | |||||
public ExtendedSocket(Socket socket) { | |||||
this.socket = socket; | |||||
} | |||||
public void sendMessage(String message) throws IOException { | |||||
synchronized (socket){ | |||||
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); | |||||
outputStream.writeBytes(message); | |||||
outputStream.flush(); | |||||
} | |||||
} | |||||
public void closeSocket() throws IOException { | |||||
this.socket.close(); | |||||
} | |||||
} |
import android.content.Context; | import android.content.Context; | ||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.widget.Toast; | |||||
import com.google.android.material.bottomnavigation.BottomNavigationView; | import com.google.android.material.bottomnavigation.BottomNavigationView; | ||||
import com.example.lfrmobileapp.databinding.ActivityMainBinding; | import com.example.lfrmobileapp.databinding.ActivityMainBinding; | ||||
import java.io.DataOutputStream; | |||||
import java.io.IOException; | |||||
import java.net.InetSocketAddress; | |||||
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 DataTransferAsyncTask dataTransferAsyncTask; | |||||
public Boolean connected = false; | |||||
@Override | @Override | ||||
protected void onCreate(Bundle savedInstanceState) { | protected void onCreate(Bundle savedInstanceState) { | ||||
NavigationUI.setupWithNavController(binding.navView, navController); | NavigationUI.setupWithNavController(binding.navView, navController); | ||||
} | } | ||||
public boolean connectToWifi(String ipAddress, int port){ | |||||
ExecutorService executor = Executors.newSingleThreadExecutor(); | |||||
Future<Boolean> future = executor.submit(() -> { | |||||
Socket tempSocket = new Socket(); | |||||
tempSocket.connect(new InetSocketAddress(ipAddress, port), 1000); | |||||
try { | |||||
socket = new ExtendedSocket(tempSocket); | |||||
socket.sendMessage("Verbindung aufbauen"); | |||||
} catch (Exception e) { | |||||
socket.closeSocket(); | |||||
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()); | |||||
} | |||||
executor.shutdown(); | |||||
if(connected){ | |||||
dataTransferAsyncTask = new DataTransferAsyncTask(socket); | |||||
dataTransferAsyncTask.execute(); | |||||
return true; | |||||
}else{ | |||||
return false; | |||||
} | |||||
} | |||||
public void sendTelegram(String telegram) | |||||
{ | |||||
this.dataTransferAsyncTask.writeTelegram(telegram); | |||||
} | |||||
} | |||||
} |
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.MainActivity; | |||||
import com.example.lfrmobileapp.R; | import com.example.lfrmobileapp.R; | ||||
import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding; | import com.example.lfrmobileapp.databinding.FragmentAutomatikBinding; | ||||
import kotlin.collections.UArraySortingKt; | import kotlin.collections.UArraySortingKt; | ||||
// Autonomous Mode | |||||
public class DashboardFragment extends Fragment { | public class DashboardFragment extends Fragment { | ||||
String ipKey = "ipkey"; | |||||
String portKey = "portkey"; | |||||
String ipAddress = "192.168.0.1"; | |||||
int port = 8000; | |||||
Communication com = new Communication(); | Communication com = new Communication(); | ||||
private FragmentAutomatikBinding binding; | private FragmentAutomatikBinding binding; | ||||
MainActivity mainActivity; | |||||
//Keys for Shared Preferences | //Keys for Shared Preferences | ||||
String listKey = "listkey"; | String listKey = "listkey"; | ||||
@Override | |||||
public void onAttach(Context context) { | |||||
super.onAttach(context); | |||||
mainActivity = (MainActivity) context; | |||||
} | |||||
public View onCreateView(@NonNull LayoutInflater inflater, | public View onCreateView(@NonNull LayoutInflater inflater, | ||||
ViewGroup container, Bundle savedInstanceState) { | ViewGroup container, Bundle savedInstanceState) { | ||||
DashboardViewModel dashboardViewModel = | DashboardViewModel dashboardViewModel = | ||||
//Get Arraylist from Shared Preferences String | //Get Arraylist from Shared Preferences String | ||||
ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, "")); | ArrayList<String> autoList = stringToArraylist(sharedPref.getString(listKey, "")); | ||||
ipAddress = sharedPref.getString(ipKey, ""); | |||||
port = sharedPref.getInt(portKey, -1); | |||||
binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | binding.startAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | ||||
@Override | @Override | ||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | ||||
if(isChecked){ | |||||
com.sendData(com.telegram(true, autoList), ipAddress, port); | |||||
}else{ | |||||
com.sendData(com.telegram(false, autoList), ipAddress, port); | |||||
if (mainActivity.connected){ | |||||
if(isChecked){ | |||||
mainActivity.sendTelegram(com.telegram(true, autoList)); | |||||
}else{ | |||||
mainActivity.sendTelegram(com.telegram(false, autoList)); | |||||
} | |||||
} | } | ||||
} | } | ||||
}); | }); |
package com.example.lfrmobileapp.ui.home; | package com.example.lfrmobileapp.ui.home; | ||||
import android.annotation.SuppressLint; | |||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.util.Log; | |||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.MotionEvent; | |||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.MainActivity; | |||||
import com.example.lfrmobileapp.R; | import com.example.lfrmobileapp.R; | ||||
import com.example.lfrmobileapp.databinding.FragmentManuellBinding; | import com.example.lfrmobileapp.databinding.FragmentManuellBinding; | ||||
import com.example.lfrmobileapp.ui.notifications.NotificationsFragment; | import com.example.lfrmobileapp.ui.notifications.NotificationsFragment; | ||||
String ipAddress = "192.168.0.1"; | String ipAddress = "192.168.0.1"; | ||||
int port = 8000; | int port = 8000; | ||||
MainActivity mainActivity; | |||||
Communication com = new Communication(); | Communication com = new Communication(); | ||||
long lastOnMoveCall = System.currentTimeMillis(); | |||||
private FragmentManuellBinding binding; | private FragmentManuellBinding binding; | ||||
@Override | |||||
public void onAttach(Context context) { | |||||
super.onAttach(context); | |||||
mainActivity = (MainActivity) context; | |||||
} | |||||
public View onCreateView(@NonNull LayoutInflater inflater, | public View onCreateView(@NonNull LayoutInflater inflater, | ||||
ViewGroup container, Bundle savedInstanceState) { | ViewGroup container, Bundle savedInstanceState) { | ||||
HomeViewModel homeViewModel = | HomeViewModel homeViewModel = | ||||
final TextView textView = binding.textHome; | final TextView textView = binding.textHome; | ||||
homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); | ||||
binding.rotateLeft.setOnClickListener(new View.OnClickListener(){ | |||||
binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){ | |||||
@SuppressLint("ClickableViewAccessibility") | |||||
@Override | @Override | ||||
public void onClick(View v){ | |||||
com.sendData(com.telegram(new int[]{-1, 1, -1, 1}, 100), ipAddress, port); | |||||
}; | |||||
public boolean onTouch(View v, MotionEvent event) { | |||||
boolean send = false; | |||||
if (System.currentTimeMillis() - lastOnMoveCall > 250){ | |||||
send = true; | |||||
lastOnMoveCall = System.currentTimeMillis(); | |||||
} | |||||
if (send && mainActivity.connected) { | |||||
if (event.getAction() == MotionEvent.ACTION_UP) { | |||||
binding.rotateLeft.performClick(); | |||||
mainActivity.sendTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0)); | |||||
return true; | |||||
} else { | |||||
mainActivity.sendTelegram(com.telegram(new int[]{-1, 1, -1, 1}, 100)); | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
}); | }); | ||||
binding.rotateRight.setOnClickListener(new View.OnClickListener(){ | |||||
binding.rotateRight.setOnTouchListener(new View.OnTouchListener(){ | |||||
@SuppressLint("ClickableViewAccessibility") | |||||
@Override | @Override | ||||
public void onClick(View v){ | |||||
com.sendData(com.telegram(new int[]{1, -1, 1, -1}, 100), ipAddress, port); | |||||
}; | |||||
public boolean onTouch(View v, MotionEvent event) { | |||||
boolean send = false; | |||||
if (System.currentTimeMillis() - lastOnMoveCall > 250){ | |||||
send = true; | |||||
lastOnMoveCall = System.currentTimeMillis(); | |||||
} | |||||
if (send && mainActivity.connected){ | |||||
if(event.getAction() == MotionEvent.ACTION_UP){ | |||||
binding.rotateRight.performClick(); | |||||
mainActivity.sendTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0)); | |||||
return true; | |||||
}else{ | |||||
mainActivity.sendTelegram(com.telegram(new int[]{1, -1, 1, -1}, 100)); | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
}); | }); | ||||
JoystickView joystick = (JoystickView) binding.joystick; | JoystickView joystick = (JoystickView) binding.joystick; | ||||
joystick.setOnMoveListener(new JoystickView.OnMoveListener() { | joystick.setOnMoveListener(new JoystickView.OnMoveListener() { | ||||
@Override | @Override | ||||
public void onMove(int angle, int strength) { | public void onMove(int angle, int strength) { | ||||
boolean send = false; | |||||
if (System.currentTimeMillis() - lastOnMoveCall > 250){ | |||||
send = true; | |||||
lastOnMoveCall = System.currentTimeMillis(); | |||||
} | |||||
String telegram = ""; | |||||
homeViewModel.setText(Integer.toString(angle), Integer.toString(strength)); | homeViewModel.setText(Integer.toString(angle), Integer.toString(strength)); | ||||
//8 segments of the joystick | //8 segments of the joystick | ||||
if((angle >= 338 || angle < 22) && strength != 0){ | if((angle >= 338 || angle < 22) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right); | joystick.setBackgroundResource(R.mipmap.right); | ||||
com.sendData(com.telegram(new int[]{1, -1, -1, 1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{1, -1, -1, 1}, strength); | |||||
}else if((angle >= 22 && angle < 67) && strength != 0){ | }else if((angle >= 22 && angle < 67) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right_forward); | joystick.setBackgroundResource(R.mipmap.right_forward); | ||||
com.sendData(com.telegram(new int[]{1, 0, 0, 1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{1, 0, 0, 1}, strength); | |||||
}else if((angle >=67 && angle < 112) && strength != 0){ | }else if((angle >=67 && angle < 112) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.forward); | joystick.setBackgroundResource(R.mipmap.forward); | ||||
com.sendData(com.telegram(new int[]{1, 1, 1, 1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{1, 1, 1, 1}, strength); | |||||
}else if((angle >=112 && angle < 157) && strength != 0){ | }else if((angle >=112 && angle < 157) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left_forward); | joystick.setBackgroundResource(R.mipmap.left_forward); | ||||
com.sendData(com.telegram(new int[]{0, 1, 1, 0}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{0, 1, 1, 0}, strength); | |||||
}else if((angle >=157 && angle < 202) && strength != 0){ | }else if((angle >=157 && angle < 202) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left); | joystick.setBackgroundResource(R.mipmap.left); | ||||
com.sendData(com.telegram(new int[]{-1, 1, 1, -1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{-1, 1, 1, -1}, strength); | |||||
}else if((angle >=202 && angle < 247) && strength != 0){ | }else if((angle >=202 && angle < 247) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.left_back); | joystick.setBackgroundResource(R.mipmap.left_back); | ||||
com.sendData(com.telegram(new int[]{-1, 0, 0, -1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{-1, 0, 0, -1}, strength); | |||||
}else if((angle >=247 && angle < 292) && strength != 0){ | }else if((angle >=247 && angle < 292) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.back); | joystick.setBackgroundResource(R.mipmap.back); | ||||
com.sendData(com.telegram(new int[]{-1, -1, -1, -1}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{-1, -1, -1, -1}, strength); | |||||
}else if((angle >=292 && angle < 337) && strength != 0){ | }else if((angle >=292 && angle < 337) && strength != 0){ | ||||
joystick.setBackgroundResource(R.mipmap.right_back); | joystick.setBackgroundResource(R.mipmap.right_back); | ||||
com.sendData(com.telegram(new int[]{0, -1, -1, 0}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{0, -1, -1, 0}, strength); | |||||
} | } | ||||
else{ | else{ | ||||
joystick.setBackgroundResource(R.mipmap.blank); | joystick.setBackgroundResource(R.mipmap.blank); | ||||
com.sendData(com.telegram(new int[]{0, 0, 0, 0}, strength), ipAddress, port); | |||||
telegram = com.telegram(new int[]{0, 0, 0, 0}, strength); | |||||
} | |||||
if (send && mainActivity.connected) | |||||
{ | |||||
mainActivity.sendTelegram(telegram); | |||||
// Log.v("TAG", telegram + "\n"); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
// dataTransferAsyncTask.cancel(false); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |
package com.example.lfrmobileapp.ui.notifications; | package com.example.lfrmobileapp.ui.notifications; | ||||
import android.app.Activity; | |||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.Button; | import android.widget.Button; | ||||
import android.widget.TextView; | |||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import com.example.lfrmobileapp.Communication; | import com.example.lfrmobileapp.Communication; | ||||
import com.example.lfrmobileapp.R; | |||||
import com.example.lfrmobileapp.DataTransferAsyncTask; | |||||
import com.example.lfrmobileapp.MainActivity; | |||||
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; | import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; | ||||
import java.io.DataOutputStream; | |||||
import java.net.Socket; | |||||
/*class NetworkThread extends Thread { | |||||
String ip; | |||||
int port; | |||||
boolean connected; | |||||
NetworkThread(String ip, int port) { | |||||
this.ip = ip; | |||||
this.port = port; | |||||
this.connected = false; | |||||
} | |||||
public void run() { | |||||
try { | |||||
Socket socket = new Socket(ip, port); | |||||
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); | |||||
outputStream.writeBytes("Verbindung aufbauen"); | |||||
outputStream.flush(); | |||||
socket.close(); | |||||
connected = true; | |||||
} catch (Exception e) { | |||||
e.printStackTrace(); | |||||
connected = false; | |||||
} | |||||
} | |||||
}*/ | |||||
public class NotificationsFragment extends Fragment { | public class NotificationsFragment extends Fragment { | ||||
Boolean connected = false; | |||||
MainActivity mainActivity; | |||||
String ipKey = "ipkey"; | String ipKey = "ipkey"; | ||||
String portKey = "portkey"; | String portKey = "portkey"; | ||||
String connectionFailed = "Verbindung fehlgeschlagen."; | String connectionFailed = "Verbindung fehlgeschlagen."; | ||||
String connectionSuccessful = "Verbindung erfolgreich."; | String connectionSuccessful = "Verbindung erfolgreich."; | ||||
String ipAddress = "192.168.0.1"; | |||||
int port = 8000; | |||||
String ipAddress = "192.168.4.1"; | |||||
int port = 8080; | |||||
int duration = Toast.LENGTH_LONG; | int duration = Toast.LENGTH_LONG; | ||||
DataTransferAsyncTask dataTransferAsyncTask; | |||||
private FragmentEinstellungenBinding binding; | private FragmentEinstellungenBinding binding; | ||||
@Override | |||||
public void onAttach(Context context) { | |||||
super.onAttach(context); | |||||
mainActivity = (MainActivity) context; | |||||
} | |||||
public View onCreateView(@NonNull LayoutInflater inflater, | public View onCreateView(@NonNull LayoutInflater inflater, | ||||
ViewGroup container, Bundle savedInstanceState) { | ViewGroup container, Bundle savedInstanceState) { | ||||
NotificationsViewModel notificationsViewModel = | NotificationsViewModel notificationsViewModel = | ||||
binding.port.setText(""); | binding.port.setText(""); | ||||
} | } | ||||
// dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port); | |||||
final Button button = binding.connect; | final Button button = binding.connect; | ||||
button.setOnClickListener(new View.OnClickListener() { | button.setOnClickListener(new View.OnClickListener() { | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
ipAddress = String.valueOf(binding.ipAdress.getText()); | |||||
port = Integer.parseInt(String.valueOf(binding.port.getText())); | |||||
if(com.sendData("", ipAddress, port)){ | |||||
try { | |||||
ipAddress = String.valueOf(binding.ipAdress.getText()); | |||||
port = Integer.parseInt(String.valueOf(binding.port.getText())); | |||||
} catch (Exception e){ | |||||
System.err.println("Parse Exception: " + e.getMessage()); | |||||
} | |||||
connected = mainActivity.connectToWifi(ipAddress, port); | |||||
if(connected){ | |||||
editor.putString(ipKey, ipAddress); | editor.putString(ipKey, ipAddress); | ||||
editor.putInt(portKey, port); | editor.putInt(portKey, port); | ||||
editor.apply(); | editor.apply(); | ||||
}else{ | }else{ | ||||
Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration); | Toast toast = Toast.makeText(v.getContext(), connectionFailed, duration); | ||||
toast.show(); | toast.show(); | ||||
// dataTransferAsyncTask.cancel(true); | |||||
} | } | ||||
} | } | ||||
}); | }); | ||||
return root; | return root; | ||||
} | } | ||||
@Override | @Override | ||||
public void onDestroyView() { | public void onDestroyView() { | ||||
super.onDestroyView(); | super.onDestroyView(); | ||||
// dataTransferAsyncTask.cancel(true); | |||||
binding = null; | binding = null; | ||||
} | } | ||||
} | } |
<ListView | <ListView | ||||
android:id="@+id/automaticList" | android:id="@+id/automaticList" | ||||
android:layout_width="413dp" | |||||
android:layout_width="match_parent" | |||||
android:layout_height="470dp" | android:layout_height="470dp" | ||||
android:layout_marginTop="110dp" | android:layout_marginTop="110dp" | ||||
app:layout_constraintEnd_toEndOf="parent" | app:layout_constraintEnd_toEndOf="parent" |