public class NetworkInterface
extends java.lang.Object
Each NetworkInterface supports one or more active AccessPoints as defined
by AccessPoint parameters. A NetworkInterface provides methods
to connect
,
to disconnect
or to
get the current
AccessPoints of a NetworkInterface.
The maximum number of simultaneously connected AccessPoints
is implementation specific. A network interface must support
at least one connected AccessPoint.
Each NetworkInterface supports a single network
connection technology and has a name and type.
The supported NetworkInterface types are available
from getNetworkTypes
.
The well known types are "3GPP", "CDMA", "WIFI",
"WIRED", "DIALUP", and "LOOPBACK".
The NetworkInterfaces supported are available from
getNetworkInterfaces
.
Connect to a WiFi SSID using a named NetworkInterface:
static AccessPoint connectByName(String ssid, char[] password, String interfaceName) throws IOException { // Create or find the desired AccessPoint for the SSID ConnectionOption<String> ssidOption = new ConnectionOption<>("ssid", ssid); ConnectionOption<char[]> passwordOption = new ConnectionOption<>("password", password); AccessPoint ap = AccessPoint.of("WIFI", ssidOption, passwordOption); // Enumerate over all network interfaces of type NetworkInterface[] interfaces = NetworkInterface.getNetworkInterfaces(ap.getType()); for (NetworkInterface ni : interfaces) { if (interfaceName.equals(ni.getName())) { // Found the desired network interface if (ap.isConnected() && !interfaceName.equals(ap.getNetworkInterface().getName())) { // AP is connected to a different network; disconnect first ap.disconnect(); } ni.connect(ap); // Successful connection to the requested NetworkInterface } } // No NetworkInterface available }
AccessPoint
,
Connector.open(java.lang.String)
Modifier and Type | Method and Description |
---|---|
void |
connect(AccessPoint accessPoint)
Connects the AccessPoint using this NetworkInterface.
|
boolean |
disconnect(AccessPoint accessPoint)
Disconnects an AccessPoint from this NetworkInterface.
|
boolean |
disconnectAll()
Disconnects all AccessPoints from this NetworkInterface.
|
AccessPoint[] |
getConnectedAccessPoints()
Returns the AccessPoints connected on the NetworkInterface.
|
java.lang.String |
getName()
Returns the name of this NetworkInterface.
|
static NetworkInterface[] |
getNetworkInterfaces(java.lang.String type)
Returns the NetworkInterfaces supported by the device for a type.
|
static java.lang.String[] |
getNetworkTypes()
Returns the supported network types.
|
java.lang.String |
getType()
Returns the type of this NetworkInterface.
|
boolean |
isRoaming()
Returns whether the NetworkInterface is roaming or not.
|
boolean |
supportsConcurrentAccessPoints()
Returns a boolean indicating that the NetworkInterface allows
more than one AccessPoint to be concurrently connected.
|
public void connect(AccessPoint accessPoint) throws java.io.IOException
The connect
method blocks until the connection succeeds or
an exception is thrown. When and if the connection of the AccessPoint
to the NetworkInterface is successful an
AccessPointListener.EVENT_TYPE_CONNECTED
event is delivered to the listeners, if any, of the AccessPoint.
accessPoint
- the AccessPoint to connect to this NetworkInterface; not nulljava.lang.NullPointerException
- if the accessPoint
is null
java.lang.IllegalArgumentException
- if the type of accessPoint
is not
equal to the type of this NetworkInterfacejava.lang.IllegalStateException
- if the AccessPoint has been removed or
is invalid and can not be used to make connectionsjava.io.IOException
- if this NetworkInterface can not support an additional AccessPoint or
if the accessPoint
is already connected to a different NetworkInterface or
if the initialization of the NetworkInterface with the accessPoint
failsjava.lang.SecurityException
- if the SecurityManager policy does not permit
AccessPointPermission("manage")
getConnectedAccessPoints()
,
disconnect(AccessPoint)
,
supportsConcurrentAccessPoints()
public boolean disconnect(AccessPoint accessPoint) throws java.io.IOException
GCF Connections
that may be using the AccessPoint will be closed.
If the AccessPoint is not connected on this NetworkInterface, no change is made.
When the disconnect is successful an
AccessPointListener.EVENT_TYPE_DISCONNECTED
event is delivered to the AccessPoint listeners.
accessPoint
- the AccessPoint to disconnect on this NetworkInterface; not nulltrue
if was connected;
false
if not connected on this NetworkInterfacejava.io.IOException
- if an error occursjava.lang.SecurityException
- if the SecurityManager policy does not permit
AccessPointPermission("manage")
java.lang.NullPointerException
- if the accessPoint
is null
getConnectedAccessPoints()
,
disconnect(AccessPoint)
,
Connection.close()
public boolean disconnectAll() throws java.io.IOException
GCF Connections
that may be using the AccessPoint will be closed.true
if any AccessPoint was connected;
false
if not no AccessPoints were disconnectedjava.io.IOException
- if an error occursjava.lang.SecurityException
- if the SecurityManager policy does not permit
AccessPointPermission("manage")
getConnectedAccessPoints()
,
disconnect(AccessPoint)
,
Connection.close()
public AccessPoint[] getConnectedAccessPoints()
AccessPoint
s connected on this NetworkInterface;
if no AccessPoints are connected then an empty array is returned.connect(AccessPoint)
,
disconnect(AccessPoint)
public java.lang.String getName()
public static NetworkInterface[] getNetworkInterfaces(java.lang.String type)
getNetworkInterfaces
.type
- the type of NetworkInterfaces to be listed; not null;
one of the values of getNetworkTypes
NetworkInterface
array of supported NetworkInterfaces;
if none are supported a zero length array is returned.java.lang.IllegalArgumentException
- if type
is not one of the values
returned from getNetworkTypes
java.lang.NullPointerException
- if type
is null
getNetworkTypes()
public static java.lang.String[] getNetworkTypes()
getNetworkTypes
.getType()
public java.lang.String getType()
public boolean isRoaming()
public boolean supportsConcurrentAccessPoints()
true
if multiple AccessPoints can be concurrently connected;
false
otherwiseconnect(AccessPoint)
,
getConnectedAccessPoints()
Copyright (c) 2014, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.