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 8M Mini SBC as it plays a critical role accessing Internet and communicating with other devices.
The ConnectCore 8M Mini SBC provides one Gigabit Ethernet interface for networking communication. You can find more information about it in the ConnectCore 8M Mini 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 |
---|---|
|
Returns the Ethernet interface name |
|
Returns the configured connection mode of the interface: |
|
Returns the configured IP address of the interface |
|
Returns the configured netmask address of the interface |
|
Returns the configured gateway address of the interface |
|
Returns the configured DNS1 address of the interface |
|
Returns the configured DNS2 address of the interface |
|
Returns the MAC address of the interface |
|
Returns whether the interface is connected or not |
|
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 |
---|---|
|
Configures the interface with the given configuration. It requires an |
|
Enables or disables the interface |
|
Resets the interface |
The configureInterface(EthernetConfiguration)
method may fail for the following reasons:
-
If the given configuration is
null
, throwing aNullPointerException
. -
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 |
---|---|
|
Sets the Ethernet connection mode |
|
Returns the Ethernet connection mode |
|
Sets the interface name |
|
Returns the interface name |
`setIpAddress(InetAddress) |
Sets the IP address |
|
Returns the IP address |
|
Sets the gateway address |
|
Returns the gateway address |
|
Sets the net mask address |
|
Returns the net mask address |
|
Sets the DNS 1 address |
|
Returns the DNS 1 address |
|
Sets the DNS 2 address |
|
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.
|