Android net wifi p2p

WifiP2pManager

This class provides the API for managing Wi-Fi peer-to-peer connectivity. This lets an application discover available peers, setup connection to peers and query for the list of peers. When a p2p connection is formed over wifi, the device continues to maintain the uplink connection over mobile or any other available network for internet connectivity on the device.

The API is asynchronous and responses to requests from an application are on listener callbacks provided by the application. The application needs to do an initialization with initialize(Context, Looper, WifiP2pManager.ChannelListener) before doing any p2p operation.

Most application calls need a WifiP2pManager.ActionListener instance for receiving callbacks onSuccess() or onFailure(int) . Action callbacks indicate whether the initiation of the action was a success or a failure. Upon failure, the reason of failure can be one of ERROR , P2P_UNSUPPORTED or BUSY .

An application can initiate discovery of peers with discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener) . An initiated discovery request from an application stays active until the device starts connecting to a peer ,forms a p2p group or there is an explicit stopPeerDiscovery(WifiP2pManager.Channel, WifiP2pManager.ActionListener) . Applications can listen to WIFI_P2P_DISCOVERY_CHANGED_ACTION to know if a peer-to-peer discovery is running or stopped. Additionally, WIFI_P2P_PEERS_CHANGED_ACTION indicates if the peer list has changed.

When an application needs to fetch the current list of peers, it can request the list of peers with requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener) . When the peer list is available onPeersAvailable(WifiP2pDeviceList) is called with the device list.

An application can initiate a connection request to a peer through connect(WifiP2pManager.Channel, WifiP2pConfig, WifiP2pManager.ActionListener) . See WifiP2pConfig for details on setting up the configuration. For communication with legacy Wi-Fi devices that do not support p2p, an app can create a group using createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) which creates an access point whose details can be fetched with requestGroupInfo(WifiP2pManager.Channel, WifiP2pManager.GroupInfoListener) .

After a successful group formation through createGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener) or through connect(WifiP2pManager.Channel, WifiP2pConfig, WifiP2pManager.ActionListener) , use requestConnectionInfo(WifiP2pManager.Channel, WifiP2pManager.ConnectionInfoListener) to fetch the connection details. The connection info WifiP2pInfo contains the address of the group owner groupOwnerAddress and a flag isGroupOwner to indicate if the current device is a p2p group owner. A p2p client can thus communicate with the p2p group owner through a socket connection.

Читайте также:  Доступные сети вай фай поблизости

With peer discovery using discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener) , an application discovers the neighboring peers, but has no good way to figure out which peer to establish a connection with. For example, if a game application is interested in finding all the neighboring peers that are also running the same game, it has no way to find out until after the connection is setup. Pre-association service discovery is meant to address this issue of filtering the peers based on the running services.

With pre-association service discovery, an application can advertise a service for a application on a peer device prior to a connection setup between the devices. Currently, DNS based service discovery (Bonjour) and Upnp are the higher layer protocols supported. Get Bonjour resources at dns-sd.org and Upnp resources at upnp.org As an example, a video application can discover a Upnp capable media renderer prior to setting up a Wi-fi p2p connection with the device.

An application can advertise a Upnp or a Bonjour service with a call to addLocalService(WifiP2pManager.Channel, WifiP2pServiceInfo, WifiP2pManager.ActionListener) . After a local service is added, the framework automatically responds to a peer application discovering the service prior to establishing a p2p connection. A call to removeLocalService(WifiP2pManager.Channel, WifiP2pServiceInfo, WifiP2pManager.ActionListener) removes a local service and clearLocalServices(WifiP2pManager.Channel, WifiP2pManager.ActionListener) can be used to clear all local services.

An application that is looking for peer devices that support certain services can do so with a call to discoverServices(WifiP2pManager.Channel, WifiP2pManager.ActionListener) . Prior to initiating the discovery, application can add service discovery request with a call to addServiceRequest(WifiP2pManager.Channel, WifiP2pServiceRequest, WifiP2pManager.ActionListener) , remove a service discovery request with a call to removeServiceRequest(WifiP2pManager.Channel, WifiP2pServiceRequest, WifiP2pManager.ActionListener) or clear all requests with a call to clearServiceRequests(WifiP2pManager.Channel, WifiP2pManager.ActionListener) . When no service requests remain, a previously running service discovery will stop. The application is notified of a result of service discovery request through listener callbacks set through setDnsSdResponseListeners(WifiP2pManager.Channel, WifiP2pManager.DnsSdServiceResponseListener, WifiP2pManager.DnsSdTxtRecordListener) for Bonjour or setUpnpServiceResponseListener(WifiP2pManager.Channel, WifiP2pManager.UpnpServiceResponseListener) for Upnp.

Note: Registering an application handler with initialize(Context, Looper, WifiP2pManager.ChannelListener) requires the permissions ACCESS_WIFI_STATE and CHANGE_WIFI_STATE to perform any further peer-to-peer operations. Get an instance of this class by calling Context.getSystemService(Context.WIFI_P2P_SERVICE) .

See Also

Summary

Nested Classes
WifiP2pManager.ActionListener Interface for callback invocation on an application action
WifiP2pManager.Channel A channel that connects the application to the Wifi p2p framework.
WifiP2pManager.ChannelListener Interface for callback invocation when framework channel is lost
WifiP2pManager.ConnectionInfoListener Interface for callback invocation when connection info is available
WifiP2pManager.DnsSdServiceResponseListener Interface for callback invocation when Bonjour service discovery response is received
WifiP2pManager.DnsSdTxtRecordListener Interface for callback invocation when Bonjour TXT record is available for a service
WifiP2pManager.GroupInfoListener Interface for callback invocation when group info is available
WifiP2pManager.PeerListListener Interface for callback invocation when peer list is available
WifiP2pManager.ServiceResponseListener Interface for callback invocation when service discovery response other than Upnp or Bonjour is received
WifiP2pManager.UpnpServiceResponseListener Interface for callback invocation when upnp service discovery response is received
Constants
int BUSY Passed with onFailure(int) .
int ERROR Passed with onFailure(int) .
String EXTRA_DISCOVERY_STATE The lookup key for an int that indicates whether p2p discovery has started or stopped.
String EXTRA_NETWORK_INFO The lookup key for a NetworkInfo object associated with the Wi-Fi network.
String EXTRA_WIFI_P2P_DEVICE The lookup key for a WifiP2pDevice object Retrieve with getParcelableExtra(String) .
String EXTRA_WIFI_P2P_INFO The lookup key for a WifiP2pInfo object Retrieve with getParcelableExtra(String) .
String EXTRA_WIFI_STATE The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled.
int NO_SERVICE_REQUESTS Passed with onFailure(int) .
int P2P_UNSUPPORTED Passed with onFailure(int) .
String WIFI_P2P_CONNECTION_CHANGED_ACTION Broadcast intent action indicating that the state of Wi-Fi p2p connectivity has changed.
String WIFI_P2P_DISCOVERY_CHANGED_ACTION Broadcast intent action indicating that peer discovery has either started or stopped.
int WIFI_P2P_DISCOVERY_STARTED p2p discovery has started
int WIFI_P2P_DISCOVERY_STOPPED p2p discovery has stopped
String WIFI_P2P_PEERS_CHANGED_ACTION Broadcast intent action indicating that the available peer list has changed.
String WIFI_P2P_STATE_CHANGED_ACTION Broadcast intent action to indicate whether Wi-Fi p2p is enabled or disabled.
int WIFI_P2P_STATE_DISABLED Wi-Fi p2p is disabled.
int WIFI_P2P_STATE_ENABLED Wi-Fi p2p is enabled.
String WIFI_P2P_THIS_DEVICE_CHANGED_ACTION Broadcast intent action indicating that this device details have changed.
Public Methods

The function call immediately returns after sending a connection cancellation request to the framework.

The function call immediately returns after sending a request to remove a local service to the framework.

The function call immediately returns after sending a request to remove service discovery request to the framework.

The function call immediately returns after sending a stop request to the framework.

Causes a thread which is waiting on this object’s monitor (by means of calling one of the wait() methods) to be woken up.

Causes all threads which are waiting on this object’s monitor (by means of calling one of the wait() methods) to be woken up.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Constants

public static final int BUSY

Passed with onFailure(int) . Indicates that the operation failed because the framework is busy and unable to service the request

public static final int ERROR

Passed with onFailure(int) . Indicates that the operation failed due to an internal error.

public static final String EXTRA_DISCOVERY_STATE

The lookup key for an int that indicates whether p2p discovery has started or stopped. Retrieve it with getIntExtra(String, int) .

See Also

public static final String EXTRA_NETWORK_INFO

The lookup key for a NetworkInfo object associated with the Wi-Fi network. Retrieve with getParcelableExtra(String) .

public static final String EXTRA_WIFI_P2P_DEVICE

The lookup key for a WifiP2pDevice object Retrieve with getParcelableExtra(String) .

public static final String EXTRA_WIFI_P2P_INFO

The lookup key for a WifiP2pInfo object Retrieve with getParcelableExtra(String) .

public static final String EXTRA_WIFI_STATE

The lookup key for an int that indicates whether Wi-Fi p2p is enabled or disabled. Retrieve it with getIntExtra(String, int) .

See Also

public static final int NO_SERVICE_REQUESTS

public static final int P2P_UNSUPPORTED

Passed with onFailure(int) . Indicates that the operation failed because p2p is unsupported on the device.

public static final String WIFI_P2P_CONNECTION_CHANGED_ACTION

Broadcast intent action indicating that the state of Wi-Fi p2p connectivity has changed. One extra EXTRA_WIFI_P2P_INFO provides the p2p connection info in the form of a WifiP2pInfo object. Another extra EXTRA_NETWORK_INFO provides the network info in the form of a NetworkInfo .

See Also

public static final String WIFI_P2P_DISCOVERY_CHANGED_ACTION

Broadcast intent action indicating that peer discovery has either started or stopped. One extra EXTRA_DISCOVERY_STATE indicates whether discovery has started or stopped. Note that discovery will be stopped during a connection setup. If the application tries to re-initiate discovery during this time, it can fail.

Источник

Оцените статью
Adblock
detector