Ethernet is a family of computer networking technologies commonly used in local area networks (LANs) and metropolitan area networks (MANs). It describes how networked devices can format data for transmission to other network devices, and how to put that data out on the network connection.
Ethernet is one of the most important interfaces of the ConnectCore 8X SBC as it plays a critical role accessing Internet and communicating with other devices.
The ConnectCore 8X SBC provides one Gigabit Ethernet interface for networking communication. You can find more information about it in the ConnectCore 8X Hardware Reference Manual.
Digi adds a new API to Android that allows you to manage this Ethernet interface. You can enable, disable, or reset the interface in addition to reading and changing its configuration. In the Digi APIx javadoc you can find a complete list of the available methods in this API.
Unless noted, all API methods require the android.permission.ACCESS_NETWORK_STATE and/or android.permission.CHANGE_NETWORK_STATE permissions, depending on whether you are accessing or changing the Ethernet configuration.
If your application does not have the android.permission.ACCESS_NETWORK_STATE and/or android.permission.CHANGE_NETWORK_STATE permissions, it will not have access to any Ethernet service feature. |
First you have to instantiate the EthernetManager object by passing the Android Application Context.
import android.app.Activity;
import android.os.Bundle;
import com.digi.android.ethernet.EthernetManager;
public class EthernetSampleActivity extends Activity {
EthernetManager ethernetManager;
[...]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Instantiate the Ethernet manager object.
ethernetManager = new EthernetManager(this);
[...]
}
[...]
}
Read the current configuration
The EthernetManager class allows you to read the current interface configuration using the following methods:
Method | Description |
---|---|
getInterfaceName() |
Returns the Ethernet interface name |
getConnectionMode() |
Returns the configured connection mode of the interface: EthernetConnectionMode.DHCP, EthernetConnectionMode.STATIC, or EthernetConnectionMode.UNKNOWN |
getIp() |
Returns the configured IP address of the interface |
getNetmask() |
Returns the configured netmask address of the interface |
getGateway() |
Returns the configured gateway address of the interface |
getDns1() |
Returns the configured DNS1 address of the interface |
getDns2() |
Returns the configured DNS2 address of the interface |
getMacAddress() |
Returns the MAC address of the interface |
isConnected() |
Returns whether the interface is connected or not |
isEnabled() |
Returns whether the interface is enabled or not |
The getMacAddress() method may fail if the configured interface is null, throwing a NullPointerException.
import com.digi.android.ethernet.EthernetManager;
[...]
EthernetManager ethernetManager = ...;
[...]
if (ethernetManager.isEnabled && ethernetManager.isConnected) {
System.out.println("Name: " + ethernetManager.getInterfaceName());
System.out.println("MAC Address: " + ethernetManager.getMacAddress());
System.out.println("Mode: " + ethernetManager.getConnectionMode());
System.out.println("IP: " + ethernetManager.getIp());
System.out.println("Netmask: " + ethernetManager.getNetmask());
System.out.println("Gateway: " + ethernetManager.getGateway());
System.out.println("DNS1: " + ethernetManager.getDns1());
System.out.println("DNS2: " + ethernetManager.getDns2());
}
[...]
All these operations require the android.permission.ACCESS_NETWORK_STATE permission. |
Configure the interface
It is also possible to configure and enable or disable the Ethernet interface using this API. You can use these methods to achieve it:
Method | Description |
---|---|
configureInterface(EthernetConfiguration) |
Configures the interface with the given configuration. It requires an object of type EthernetConfiguration. Note that this method resets the interface to apply the changes |
setEnabled(boolean) |
Enables or disables the interface |
resetInterface() |
Resets the interface |
The configureInterface(EthernetConfiguration) method may fail for the following reasons:
-
If the given configuration is null, throwing a NullPointerException.
-
If the given IP address cannot be resolved, throwing an UnknownHostException.
The EthernetConfiguration class offers a series of methods to fill and read the configuration object:
Method | Description |
---|---|
setConnectionMode(EthernetConnectionMode) |
Sets the Ethernet connection mode |
getConnectionMode() |
Returns the Ethernet connection mode |
setInterfaceName(String) |
Sets the interface name |
getInterfaceName() |
Returns the interface name |
setIpAddress(InetAddress) |
Sets the IP address |
getIpAddress() |
Returns the IP address |
setGateway(InetAddress) |
Sets the gateway address |
getGateway() |
Returns the gateway address |
setNetMask(InetAddress) |
Sets the net mask address |
getNetMask() |
Returns the net mask address |
setDns1Addr(InetAddress) |
Sets the DNS 1 address |
getDns1Addr() |
Returns the DNS 1 address |
setDns2Addr(InetAddress) |
Sets the DNS 2 address |
getDns2Addr() |
Returns the DNS 2 address |
import com.digi.android.ethernet.EthernetManager;
import com.digi.android.ethernet.EthernetConfiguration;
import com.digi.android.ethernet.EthernetConnectionMode;
import java.net.InetAddress;
[...]
EthernetManager ethernetManager = ...;
[...]
// Enable the interface if it isn't.
if (!ethernetManager.isEnabled())
ethernetManager.setEnabled(true);
// Set a static configuration.
EthernetConfiguration config = new EthernetConfiguration();
config.setInterfaceName(ethernetManager.getInterfaceName());
config.setConnectionMode(EthernetConnectionMode.STATIC);
config.setIp(InetAddress.getByName("192.168.1.150"));
config.setNetmask(InetAddress.getByName("255.255.255.0"));
config.setGateway(InetAddress.getByName("192.168.1.1"));
config.setDns1(InetAddress.getByName("8.8.8.8"));
ethernetManager.configureInterface(config);
[...]
All these operations require the android.permission.CHANGE_NETWORK_STATE permission. |