Android : How to know the ip connected to my hotspot in android - android

How to know the ip connected to my hotspot in android
I try to under code but /proc/net/arp is not reachable file in android 10
https://developer.android.com/about/versions/10/privacy/changes#proc-net-filesystem
BufferedReader br = null;
final ArrayList<ClientScanResult> result = new ArrayList<ClientScanResult>();
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.toString());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}

Related

How to get the Linux application group name programmatically?

How to get the Linux application group name programmatically ?
There is no direct way to get it using the SDK.
public static String getGroupName(Context context) {
String groupName = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
for (String activeProcess : processInfo.pkgList) {
try {
if (activeProcess.compareTo(context.getPackageName()) == 0) {
Process process = Runtime.getRuntime().exec("ps -p " + processInfo.pid);
String psOutput = streamToString(process.getInputStream());
String[] lines = psOutput.split("\n");
String valueLine = lines[1];
int firstSpace = valueLine.indexOf(" ", 0);
groupName = valueLine.substring(0, firstSpace);
}
}
catch (IOException ioe){
Log.e(TAG, "Error while getting group name", ioe);
}
catch (RuntimeException rte){
Log.e(TAG, "Error while getting group name", rte);
}
}
}
return groupName;
}
private static String streamToString(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
Log.e(TAG, "Error while reading stream", e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
Log.e(TAG, "Error while closing stream", e);
}
}
}
return sb.toString();
}

Get the list of all the devices connected within same Network Android

In Android,
How to get the list of devices connected in same WiFi Network?
How to get the nearest devices from your smartphone connected with same WiFi network?
Did frequency check is able to find the nearest device?
I find the solution for list of devices connected in same network,and i placed the code below
Call this method in onCreate -> getClientList(true, 200);
write this method in our class
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
try {
result = new ArrayList<ClientScanResult>();
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
int i = 0;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if ((splitted != null) && (splitted.length >= 4)) {
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
i++;
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
return result;
}
if any clarifications, please reply a message for this post,
THANKS

How to find index of the line?

i am using following code to read data from file but i am unable to find index of the line?
public static ArrayList<String> searchInFile(String word) {
ArrayList<String> result = new ArrayList<String>();
FileReader fileReader = null;
try {
fileReader = new FileReader(new File("input.txt"));
} catch (FileNotFoundException e1) {
System.err.println("File input.txt not found!");
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(file);
String line;
try {
while ((line = br.readLine()) != null) {
if (line.contains(word)) {
result.Add(line);
}
}
} catch (IOException e) {
System.err.println("Error when processing the file!");
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.err.println("Unexpected error");
e.printStackTrace();
}
}
}
return result;
}
so how can i find index of the line??
You would need to create a counter integer variable that can keep track of the line index for you.
int ctr =0;
while ((line = br.readLine()) != null) {
if (line.contains(word)) {
result.Add(line);
}
ctr++;
}
Hope it helps!

Get a notification when client connects to access point?

I have a question:
Is there any why to get a notification (intent, broadcast etc..) in my MainActivity when a client connects to the Wifi Access Point I openend before?
I want to print information about connected clients to a console when they connect.
thanks for any help!
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
macCount++;
}
}
}
} catch (Exception e) { e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Fetch ip address of pc on android emulator through code android

I want to get the ip address of my pc in android emulator through code....or tell me to achive ip address of all devices connected in a lan to identify each one uniquely .......please help me to sort out this problem
Thanks in advance
The above functions are possible only by checking the arp cache where the IP address will be added one by one depending on how each one connect to the device. USe the below code and check. Just put button with proper name and call this method on click
public void getClientList() {
int macCount = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
// Basic sanity check
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
macCount++;
ClientList.add("Client(" + macCount + ")");
IpAddr.add(splitted[0]);
HWAddr.add(splitted[3]);
Device.add(splitted[5]);
Toast.makeText(
getApplicationContext(),
"Mac_Count " + macCount + " MAC_ADDRESS "
+ mac, Toast.LENGTH_SHORT).show();
for (int i = 0; i < splitted.length; i++)
System.out.println("Addressssssss "
+ splitted[i]);
}
}
}
// ClientList.remove(0);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use this code fetch the external ip address
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://api.externalip.net/ip/");
HttpResponse response = null;
try
{
response = httpclient.execute(httpget);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Log.e("",""+response);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 1024)
{
try
{
str=EntityUtils.toString(entity);
Log.e("",""+str);
}
catch (ParseException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Categories

Resources