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();
}
}
Related
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());
}
}
I was trying to make an app that can find the IP address of a device connected to my android phone's hotspot using it's MAC address. I know this can be done using ARP tables but I just don't know how. I'm using android studio with java and my device is running android 10. Please help.
Here's how it's implemented in AndroidNetworkTools library, which worked fine for me:
/**
* Returns all the IP/MAC address pairs currently in the ARP cache (/proc/net/arp).
*/
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
HashMap<String, String> macList = new HashMap<>();
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4) {
// Ignore values with invalid MAC addresses
if (splitted[3].matches("..:..:..:..:..:..")
&& !splitted[3].equals("00:00:00:00:00:00")) {
macList.put(splitted[0], splitted[3]);
}
}
}
return macList;
}
/**
* Method to read lines from the ARP Cache
*/
private static ArrayList<String> getLinesInARPCache() {
ArrayList<String> lines = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return lines;
}
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
I have an android tab with hotspot turned on. When other wifi enabled devices get connected to my android device , their IP Address and other details get stored in the ARP table and I can access those details by reading from "/proc/net/arp" and I am showing them in a list. But when one device gets disconneted from my hotspot enabled android tab then the corresponding details related to that device are not getting cleared from the ARP table(When I read "/proc/net/arp" , the ARP table still holds the details of disconnected device). I want to Clear the details of the device which is disconnected.
Here is the code (accessing ARP table)
public ArrayList<ClientScanResultSO> getClientList(boolean onlyReachables,
int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResultSO> result = null;
try {
result = new ArrayList<ClientScanResultSO>();
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);
String name = InetAddress.getByName(splitted[0]).getHostName();
if (!onlyReachables || isReachable) {
result.add(new ClientScanResultSO(splitted[0],
splitted[3], splitted[5], isReachable,name));
}
}
}
}
} 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;
}
Hope I am clear with my question.
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();
}
}
}