Added so pepper sends its ip to another ip
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.example.fitbot.ui.activities;
|
package com.example.fitbot.ui.activities;
|
||||||
|
|
||||||
|
import static com.example.fitbot.util.Networking.sendIpAddress;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -17,6 +18,10 @@ import android.widget.Button;
|
|||||||
import com.example.fitbot.R;
|
import com.example.fitbot.R;
|
||||||
import com.example.fitbot.util.NavigationManager;
|
import com.example.fitbot.util.NavigationManager;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
@@ -30,7 +35,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
// Set full screen mode to hide status bar
|
// Set full screen mode to hide status bar
|
||||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||||
@@ -49,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
// Hide system UI
|
// Hide system UI
|
||||||
NavigationManager.hideSystemUI(this);
|
NavigationManager.hideSystemUI(this);
|
||||||
|
sendIpAddress(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpUi() {
|
private void setUpUi() {
|
||||||
@@ -106,4 +111,8 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onBackPressed();
|
super.onBackPressed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -0,0 +1,76 @@
|
|||||||
|
package com.example.fitbot.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class Networking {
|
||||||
|
|
||||||
|
public static void sendIpAddress(final Context context) {
|
||||||
|
new AsyncTask<Void, Void, Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params) {
|
||||||
|
String ipAddress = getIPAddress(context);
|
||||||
|
String jsonInputString = "{\"ip\":\"" +
|
||||||
|
ipAddress +
|
||||||
|
"\"}";
|
||||||
|
|
||||||
|
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
||||||
|
HttpURLConnection conn = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL("http://145.109.171.85:42069/set-ip"); // Replace with your Node server URL
|
||||||
|
conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
|
||||||
|
OutputStream os = conn.getOutputStream();
|
||||||
|
os.write(input, 0, input.length);
|
||||||
|
os.close();
|
||||||
|
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
Log.i("NetworkUtils", "Response Code: " + responseCode);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e("NetworkUtils", "Error sending IP address", e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String getIPAddress(Context context) {
|
||||||
|
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||||
|
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
|
||||||
|
|
||||||
|
// Convert little-endian to big-endian if needed
|
||||||
|
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
|
||||||
|
ipAddress = Integer.reverseBytes(ipAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
|
||||||
|
|
||||||
|
String ip = "";
|
||||||
|
try {
|
||||||
|
ip = InetAddress.getByAddress(ipByteArray).getHostAddress();
|
||||||
|
} catch (UnknownHostException ex) {
|
||||||
|
Log.e("WIFIIP", "Unable to get host address.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user