I have ethernet connected to my android board.
I want to manually set IP from code. I was able to set IP address for WIFI.
I have looked into following links for ethernet
Assign static IP to ethernet card from OTG
So far I have not found how to set static IP to ethernet via android code.
You may need to create /data/misc/ethernet/ipconfig.txt file to configure static IP address.
As you successfully configured static IP address for WiFi already,
I think /data/misc/wifi/ipconfig.txt was also created and it will be valid for Ethernet config, too.
Please refer to following links for file path and data format.
EthernetConfigStore.java
IpConfigStore.java
I was able to set Ip to Ethernet connection as follows.
I was using Allwinner A31s android board.
String command1 = "su -c ifconfig eth0 "
+ terminalIpAddressString+" netmask "
+ subnetMaskAddressString
+" up";
String command2 = "route add default gw "
+ gatewayAddressString+" dev eth0";
String command3 = "mount -o remount,rw /system";
String command4 = "echo \"su -c ifconfig eth0 "
+terminalIpAddressString+" netmask "
+subnetMaskAddressString+" up;" +
"route add default gw "
+gatewayAddressString
+" dev eth0\" > /system/bin/preinstall.sh";
String command5 = "busybox sed -i 's/su -c ifconfig eth0 "
+terminalIpAddressString
+" netmask "+subnetMaskAddressString+" up;"
+"route add default gw 172.19.10.2 dev eth0"
+ "/su -c ifconfig eth0 "+terminalIpAddressString
+" netmask "+subnetMaskAddressString+" up;"
+"route add default gw "+gatewayAddressString
+" dev eth0/g' /system/bin/preinstall.sh";
The steps will vary with different versions of android.
Go to Settings, click on Connections then WiFi.
Tap and hold on the network you wish to adjust and click Manage Network Settings.
Mark the Show Advanced Options check box.
Click on the bottom checkbox to show advanced options
Under IP Settings, change it from DHCP to Static.
Change DHCP to Static
When using static IP addresses on home and other private networks, they should be chosen from within the standard private IP address ranges listed:
10.0.0.0 through 10.255.255.255
172.16.0.0 through 172.31.255.255
192.168.0.0 through 192.168.255.255
Now enter the IP address. This step is specific to each network. Ex: 192.168.1.128
The Gateway should fill in automatically based on the IP address. If not, copy the IP address and replace the last number with a 1. (Based on previous example: 192.168.1.1)
Tap Save and let the network reconnect.
If you intend to convert a wifi connection to ethernet or vice-versa check this.
Related
Is there an easy way, to see which ports are open on my Android device and a way to close the open ports?
Create a wifi hotspot on your phone
Connect your computer to the hotspot
use ipconfig or ifconfig to know the gateway IP adresse ( ie: your phone's IP adress )
Download nmap : http://nmap.org/
Use the nmap command : nmap -sS -Pn -p- your_phone_ip_adress
the open TCP ports will be shown as follow :
65531 closed ports PORT
STATE SERVICE
53/tcp open domain
8187/tcp open unknown
38647/tcp open unknown
42761/tcp open
unknown MAC Address: A4:9A:58:::** (Samsung Electronics Co.)
PS : For UDP ports use: nmap -sU -Pn -p- your_phone_ip_adress
You can determine the currently open ports by reading the textual /proc pseudo-files such as
/proc/net/tcp
/proc/net/udp
This is basically what a netstat command (where implemented) does - you may wish to find and examine the source of a simple netstat implementation (it should be possible to port such to java)
However, when running as an unprivileged app userid, you will only be able to close sockets belonging to your own process (or by roundabout means involving ptrace or process killing, other processes belonging to your userid). Also note that closing a socket does not necessarily make that port available for immediate re-use.
You can try different network commands through runtime and check the results
// netstat -lptu
// netstat -vat
Process su = Runtime.getRuntime().exec("netstat -vat ");
I am developing an Android app which will be used by custom devices which will have ethernet support (and also wifi).
The app has to enable a settings activity for Ethernet.
Please NOTE that these settings have to be run by the app and not by the Android settings, since the app will be the only thing running on the device and the user will not have access to the Android running in the background.
The user has to be able to:
ENABLE/DISABLE Ethernet
Choose DHCP or STATIC
If choosing STATIC - set IP, gateway
The problem is that I cannot access the android.net.ethernet programmatically and there is no explanation about this issue online.
So if someone has done something like this, please help me get into the right direction.
I know it is very late but it might help someone else.
I had some of the requirements you mentioned for my android application.
This is how I achieved some of the points
1. ENABLE/DISABLE Ethernet
//Enable Ethernet
ifconfig eth0 up
//Disable Ethernet
ifconfig eth0 down
3. If chosen STATIC - set IP, gateway
Fire these commands from java code.
su -c ifconfig eth0 172.19.10.105 netmask 255.255.255.0 up
route add default gw 172.19.10.2 dev eth0
You can execute these commands using following code.
Here command variable is one of the commands mentioned above.
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
-Environment-
VPN connection(OpenVPN)
-Default gateway is changed into VPN server IP
-Virtual interface(tunXX) for VPN connection directly connects to a default gateway
Interfaces & default gateway
interfaces :
lo inet addr:127.0.0.1
wlan0 inet addr:150.149.131.5
tun0 inet addr:10.8.0.14
default gateway : 10.8.0.2
In this environment, I want to know "10.8.0.14" regardless of the interface name using Android API.
I cound find only a way to get default route IP address below
mSocket = new Socket(dstAddress, peerPortNum);
mStrMyIPAddr = mSocket.getLocalAddress().getHostAddress();
But, I don not want to use Socket
I connect my Android phone with my PC through USB. And open the usb tether option on the phone.
So, there is a new remote NDIS network adapter displayed in the PC's network connections, and there is a rnndis0 IFface on the android.
I do the following configurations:
PC: ip: 192.168.42.1 netmask: 255.255.255.0
Phone: ip 192.168.42.2 netmask: 255.255.255.0
And when ping each other, both work ok.
But when I configure the default gateway of my phone to be the address of the PC, it failed.
busybox route add default gw 192.168.42.1
busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default bogon 0.0.0.0 UG 0 0 0 rndis0
192.168.42.0 * 255.255.255.0 U 0 0 0 rndis0
can anyone tell me the reason?
I believe it's marked as bogon as it is an address which should not appear in a routing table, ie a bogon route.
This is because it is a private network address. It's only a warning though.
see http://packetlife.net/blog/2009/jan/21/whats-bogon/
What is the output of
$ ip route
on Android?
SivlerZhao
Yesterday, I was unable to reproduce the problem.
Now, it reproduced.
busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default bogon 0.0.0.0 UG 0 0 0 rndis0
192.168.137.0 * 255.255.255.0 U 0 0 0 rndis0
And the output of ip route is:
busybox ip route
default via 192.168.137.1 dev rndis0
192.168.137.0/24 dev rndis0 src 192.168.137.2
Actually, it works ok, but the "bogon" word apears
In my Android mobile, I clicked from the phone and saw settings-> Wireless Controls->Wifi Settings and tapped on the network I have connected to. It popped up a Dialog with network status, level, speed, signal strength, IP address and security type. So just I got an IP address of network.
How can I get IP addresses of other wifi enabled devices by giving (known IP address got from my mobile) as a Input?
Actually to be clear, how to give specific known IP address as a Input and must be able to get other Unknown Ip addresses of Wifi enabled devices connected on the same network programmatically in Android 2.3 ?
For getting the IP Address you can check getIpAddress() in WifiInfo
Pseudo Code,
WifiManager mWifiManager = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
Log.e("IP in Mask Integer", mWifiInfo.getIpAddress()+"");
Log.e("IP Address", intToIP(mWifiInfo.getIpAddress())+"");
public String intToIP(int i) {
return (( i & 0xFF)+ "."+((i >> 8 ) & 0xFF)+
"."+((i >> 16 ) & 0xFF)+"."+((i >> 24 ) & 0xFF));
}
To find other devices in the same network you need to SCAN that network.
Scanning means to find the possible IPs in your subnet (using your IP address and subnet mask you can find the possible range and usually for home routers it is between x.x.x.1 ~ x.x.x.254) then iterate through these IPs and for each IP do PING or try to connect with TCP/UDP to some/all ports until you get a response which means there is an active device using that IP. However, it you get no response at all then it might be that there is no device using that IP or the device is filtering these requests (a firewall dropping your packages) or simply that device doesn't have any listening services.
To learn more about this check out some well known tools like nmap
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int myIp = myWifiInfo.getIpAddress();
textWifiManager.setText(myWifiManager.toString());
textWifiInfo.setText(myWifiInfo.toString());
int intMyIp3 = myIp/0x1000000;
int intMyIp3mod = myIp%0x1000000;
int intMyIp2 = intMyIp3mod/0x10000;
int intMyIp2mod = intMyIp3mod%0x10000;
int intMyIp1 = intMyIp2mod/0x100;
int intMyIp0 = intMyIp2mod%0x100;
textVIewIp.setText(String.valueOf(intMyIp0)
+ "." + String.valueOf(intMyIp1)
+ "." + String.valueOf(intMyIp2)
+ "." + String.valueOf(intMyIp3)
);
Also have to modify AndroidManifest.xml to grant permission of android.permission.ACCESS_WIFI_STATE.
click settings, > wireless & networks > then wlan > click the options button on your phone (it should be beside your home button thingy) then click advanced :)