Connectivity of andriod-Wi-Fi direct (1)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/connectivity/wifip2p.html

Wi-Fi direct allows devices later than Android (API Level 14) to directly connect to each other using the corresponding hardware without intermediate access points. When every device supports Wi-Fi direct, you can use these APIs to discover and connect to another peer device, and the communication distance is far beyond the Bluetooth connection. This is useful for sharing application data among users, such as multiplayer games or image sharing applications.

Wi-Fi direct consists of the following parts:

1. The wifip2pmanager class defines methods for Peer-to-Peer Discovery, request, and connection;

2. Listener used to obtain the wifip2pmanager method call result (success or failure) notification. When the wifip2pmanager method is called, each method can receive a special listener that is passed in as a parameter;

3. Intent objects used to notify you of specific events detected by the Wi-Fi direct framework, such as deleting links or discovering new devices.

These three main API components are often used together. For example, a wifip2pmanager. actionlistener listener is provided for the discoverpeers () method to obtain notifications using the actionlistener. onsuccess () and actionlistener. onfailure () methods. If the discoverpeers () method finds that the Peer device list has changed, a wifi_p2p_peers_changed_action intent broadcast will be sent.

API Overview

Wifip2pmanager provides methods for interacting with the Wi-Fi hardware of devices to discover and connect devices. The following operations are valid:

Table 1. Wi-Fi Direct Method

Method

Introduction

Initialize ()

Register an application to the Wi-Fi framework. It must be called before other Wi-Fi direct methods are called.

Connect ()

Use the specified configuration to start the peering connection between devices.

Cancelconnect ()

Cancels any connection requests from ongoing peer devices.

Requestconnectinfo ()

Request the connection information of the device.

Creategroup ()

Use the current device as the Group Administrator to create a peer group.

Removegroup ()

Delete the current peer device group.

Requestgroupinfo ()

Request peer group information.

Discoverpeers ()

Start Peer Discovery.

Requestpeers ()

Request the list of currently discovered Peer Points.

 

The wifip2pmanager class method will allow you to input a listener so that the Wi-Fi direct framework can notify your activity of the call status. The following table describes the listener interfaces available for wifip2pmanager class method calls:

Table 2. Wi-Fi direct listener

Listener Interface

Associated operations

Wifip2pmanager. actionlistener

Connect (), cancelconnect (), creategroup (), removegroup (),
And discoverpeers ()

Wifip2pmanager. channellistener

Initialize ()

Wifip2pmanager. connectioninfolistener

Requestconnectinfo ()

Wifip2pmanager. groupinfolistener

Requestgroupinfo ()

Wifip2pmanager. peerlistener

Requestpeers ()

 

The Wi-Fi direct API also defines some intent objects that are broadcast when some Wi-Fi direct events occur, if the Wi-Fi status of a peer device or device changes. You can register and receive these intent objects in your application and create a broadcast receiver to process these intent objects:

Table 3. Wi-Fi direct intent

Intent

Introduction

Wifi_p2p_connection_changed_action

This broadcast is sent when the Wi-Fi connection status of the device changes.

Wifi_p2p_peers_changed_action

This broadcast is sent when the discoverpeers () method is called. If you want to process the intent in the application, you usually want to call the requestpeers () method to obtain the update list of the Peer device.

Wifi_p2p_state_changed_action

This broadcast is sent when wi-fi direct on the device is enabled or disabled.

Wifi_p2p_this_device_changed_action

This broadcast is sent when the device details (such as the device name) change.

 

Create a broadcast receiver for the intent of Wi-Fi direct

Broadcast receivers allow you to receive intent broadcasts from Android so that your applications can respond to events of interest. Follow these steps to create an intent receiver that processes Wi-Fi direct:

1. Create a class that inherits the broadcastreceiver class. The constructor of this class must have parameters of the wifip2pmanager, wifip2pmanager. Channel, and activity type. This allows the broadcast receiver to send updates to the activity and the Wi-Fi hardware and communication channel to be accessed.

2. Check the intent object you are interested in the onreceive () method of the broadcast receiver and perform the necessary operations based on the received intent. For example, if the broadcast receiver receives a wifi_p2p_peers_changed_action intent, you can call the requestpeers () method to obtain the list of discovered peer devices.

The following code shows how to create a typical broadcast receiver. The broadcast receiver requires a wifip2pmanager object and an activity object as parameters, and uses these two classes to perform the appropriate operations when the broadcast receiver receives the intent:

/**

* A broadcastreceiver that notifies of important Wi-Fi P2P events.

*/

Public class wifidirectbroadcastreceiver extends broadcastreceiver {

 

Private wifip2pmanager manager;

Private channel;

Private mywifiactivity;

 

Public wifidirectbroadcastreceiver (wifip2pmanager manager, channel,

Mywifiactivity ){

Super ();

This. Manager = manager;

This. Channel = channel;

This. Activity = activity;

}

 

@ Override

Public void onreceive (context, intent ){

String action = intent. getaction ();

 

If (wifip2pmanager. wifi_p2p_state_changed_action.equals (Action )){

// Check to see if Wi-Fi is enabled and your y appropriate activity

} Else if (wifip2pmanager. wifi_p2p_peers_changed_action.equals (Action )){

// Call wifip2pmanager. requestpeers () to get a list of current peers

} Else if (wifip2pmanager. wifi_p2p_connection_changed_action.equals (Action )){

// Respond to new connection or disconnections

} Else if (wifip2pmanager. wifi_p2p_this_device_changed_action.equals (Action )){

// Respond to this device's wifi state changing

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.