Determine if IP Address is STATIC or DHCP using Android? - java

I am working on android system application. I would like to know if IP address is DHCP or STATIC.
Do we have any android java class for this? Or is there any way to get it from sysfs like /sysfs/class/net/eth0?

I used this to check wether is DHCP or STATIC years ago, perhaps you can try it out, if it does not work I'll remove the answer.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wificonf : configuredNetworks){
if (wificonf.networkId == connectionInfo.getNetworkId()){
if (wificonf.toString().toLowerCase().indexOf("DHCP".toLowerCase())>-1){
//DHCP
}else if(wificonf.toString().toLowerCase().indexOf("STATIC".toLowerCase())>-1){
//STATIC
}
break;
}
}

Related

WifiInfo getSSID return last SSID when disconnected

I create an app which need to deal with WiFi.
I have this piece of code
this.wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
String currentSSID = wifiManager.getConnectionInfo().getSSID();
The problem is that the current SSID is valid when I'm already connected to hotspot but when i'm disconnected it return the SSID of the last hotspot instead of something like null or equivalent.
After some experiment i figured out that the wifiInfo don't update when the connection to hospot is loss so to fix it you can use
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnected = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
this.wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
String currentSSID = wifiManager.getConnectionInfo().getSSID();
/*
Surround your ssid with " when you compare it with the ssid of the wifimanager
because it will return your SSID surouded by quotes
*/
if(currentSSID.equals("\"" + your_net_ssid + "\"") && isConnected){
//You are realy connected to the hospot
}else{
//The connection dont exist
}
Hope this will be usefull !

Connect to Wifi [without Internet] with a high priority [block auto connection]?

I have already asked this question, but it has no answers and I have to keep working. So here I ask this question for the second time.
old question: Connect to Wifi [without Internet] with a high priority?
I have a problem. I develop a android app, which should also connect to a wifi [without internet, just to controll a robot, webinterface: 10.10.0.1].
MY PROBLEM: If I connect to the robot wifi, Android prevents the connection and connect to my default home wlan.
What can I change to connect to robot wifi without problems?
My connection function:
public static void connectToWifi(String ssid, String pass){
WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
conf.preSharedKey = "\""+ pass +"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
List<WifiConfiguration> list = MainActivity.wifi.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
MainActivity.wifi.disconnect();
MainActivity.wifi.enableNetwork(i.networkId, true);
MainActivity.wifi.reconnect();
break;
}
}
}

Java IP Address,MAC address return with name of device [duplicate]

This question already has an answer here:
Android Quickly Find All Local Devices on Network
(1 answer)
Closed 6 years ago.
I am trying to code in android to return the name of all devices and their ip on the wifi. I am having trouble on how to approach this. All of the examples online return only the localhost and its information. i.e.
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println(" IP Addr: " + localhost.getHostAddress());
// Just in case this host has multiple IP addresses....
InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
if (allMyIps != null && allMyIps.length > 1) {
System.out.println(" Full list of IP addresses:");
for (int i = 0; i < allMyIps.length; i++) {
System.out.println(" " + allMyIps[i]);
}
}
} catch (UnknownHostException e) {
System.out.println(" (error retrieving server host name)");
}
try {
System.out.println("Full list of Network Interfaces:");
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
System.out.println(" " + intf.getName() + " " + intf.getDisplayName());
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
System.out.println(" " + enumIpAddr.nextElement().toString());
}
}
} catch (SocketException e) {
System.out.println(" (error retrieving network interface list)");
}
This is not necessarily What I want, since I want to return all of the devices on the wifi.
I don't know how to go about this.
please help, thank you !
WifiManager This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:
The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
Results of access point scans, containing enough information to make decisions about what access point to connect to.
It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
WifiInfo Describes the state of any Wifi connection that is active or is in the process of being set up.
Example:
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
String macAddress = wifiInfo.getMacAddress();
Ensure you have modified your manifest with the following:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Sources:
1. WifiManager
2. WifiInfo API

Is there any method to find the ip address of nearest access point in my wifi network?

I use inssider and kismac for checking the wifi indoor and outdoor accesspoints in my network. But both these tools doesn't provide any info on the ip of the access point currently connected. Its seen that netstumbler in windows had an option to display ip address and subnet but, though they have the fields, it doesn't display anything. I don't find any app that fulfills this requirement.
Is there any method to find the ip address of the nearest accesspoint in my wifi network? programmatically or is there any app?
is there any method in python , objective c , java where a request for ip-address of an ap returns it.
I use osx 10.9
use following way to achieve
public String getIpAddr() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return ipString;
}
Please Note: You need to add android.permission.INTERNET and android.permission.ACCESS_WIFI_STATE in your AndroidManifest.xml as to access the code.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
for google play similar type of app look at https://play.google.com/store/apps/details?id=com.roamingsoft.manager&hl=en
You cant get ip of AP without connecting to it.
You can get ip of connected AP by following
DhcpInfo dhcp;
WifiManager wifii;
wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE);
dhcp = wifii.getDhcpInfo();
s_dns1 = "DNS 1: " + String.valueOf(dhcp.dns1);
s_dns2 = "DNS 2: " + String.valueOf(dhcp.dns2);
s_gateway = "Default Gateway: " + String.valueOf(dhcp.gateway);
s_ipAddress = "IP Address: " + String.valueOf(dhcp.ipAddress);
s_leaseDuration = "Lease Time: " + String.valueOf(dhcp.leaseDuration);
s_netmask = "Subnet Mask: " + String.valueOf(dhcp.netmask);
s_serverAddress = "Server IP: " + String.valueOf(dhcp.serverAddress);

How to obtain MAC address of WiFi network interface?

It seems the java.net.NetworkInterface implementation of android does not have a
byte[] getHardwareAddress() method
http://developer.android.com/reference/java/net/NetworkInterface.html
I've found several forums of people trying to do this with no definitive answer, I need to get a somewhat cross-device UUID, so I can't rely on phone numbers or in ANDROID_ID (which can be overwritten and which I think depends on the user having a google account)
http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
In linux you can use ifconfig or read from /proc/net/arp and you can easily get the Hardware address.
Is there a file in android that I can read?
There has to be a way to get this address since it's shown in the "Settings > About Phone > Status" of the phone.
Late answer, but it can help others with the same "problem".
The answer is really straight forward:
WifiManager wifiMan = (WifiManager) this.getSystemService(
Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();
The above code will get you the MAC address of your device, remember to have wifi enabled when grabbing the address. This code snippet should be used in your Activity.
There has to be a way to get this
address since it's shown in the
"Settings > About Phone > Status" of
the phone.
Which means, if nothing else, you can go putter around the Android open source code, perhaps using Google Code Search, to figure out where it pulls that from.
Doing a bit of puttering myself, it would appear it is using getMacAddress() from WifiInfo.
UPDATE:
Beginning Android 6.0, above API will give you constant MAC address for all the devices, which is 02:00:00:00:00:00. Refer below for detailshttp://developer.android.com/about/versions/marshmallow/android-6.0-changes.html Found another post that claims to find MAC address in 6.0, not tested it thoughHow to get Wi-Fi Mac address in Android Marshmallow
On Android Q, there is no way to access mac address anymore.
WifiInfo.getMacAddress() will always return 02:00:00:00:00:00.
And WifiConfiguration.getRandomizedMacAddress() will not available anymore.
Add Following Permission.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
WifiManager initialize in onCreate.
WifiManager wifiMgr = (WifiManager) getContext().getSystemService(context.WIFI_SERVICE);
Use following function.
public void WI-FI_MAC() {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
String macAddress = wifiInfo.getMacAddress();
}
this my code and work well in android 5 +.
public static String getMacAddress() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
// res1.append(Integer.toHexString(b & 0xFF) + ":");
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
//handle exception
}
return "";
}

Categories