When we use a conventional Wi-Fi network we need a controller device known as WAP (Wireless Access Point). In a scenario where just two devices want to be connected with each other there must WAP to provide support. To avoid this complexity, Wi-Fi Alliance introduced a certification for wireless devices known as Wi-Fi Direct which allows two devices connect directly.
This is a game-changing technology for Wi-Fi devices to connect directly, making it simple and convenient to do things like print, share, synch and display.
Wi-Fi Direct in Android:
Wi-Fi Direct allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point.
- WifiP2pManager class provides all methods to discover , request and connect to peers.
- Each method in WifiP2pManager implements a specific listener which is passed as parameter to allow you to be notified of the success or failure of method calls,
- Intents that notify you of specific events detected by the Wi-Fi Direct framework, such as a dropped connection or a newly discovered peer.
Connecting via Wi-Fi Direct:
- Application Permissions :
- To use Wi-Fi Direct, we need to add the
INTERNETpermissions to your manifest. Wi-Fi Direct doesn’t require an internet connection, but it does use standard Java sockets, which require the
- Broadcast Receiver and Peer-To-Peer manager :
- We have to listen particular broadcasts when we use Wi-Fi Directs. These Broadcasts tell us about certain event that occurred.
- At the end of the
onCreate()method, get an instance of the
WifiP2pManager, and call its
initialize()method. This method returns a
WifiP2pManager.Channelobject, which you’ll use later to connect your app to the Wi-Fi Direct Framework.
- Initiate Peer Discovery :
- To search near by Peers, WifiP2pManager class has one method discoverPeers() method. There are two argument supposed to be passed in this method:
WifiP2pManager.Channelyou received back when you initialized the peer-to-peer
- An implementation of
WifiP2pManager.ActionListenerwith methods the system invokes for successful and unsuccessful discovery.
- Fetch list of Peers :
- Implement the
WifiP2pManager.PeerListListenerinterface, which provides information about the peers that Wi-Fi Direct has detected.
- Connect to Peer :
- In order to connect to a peer, create a new
WifiP2pConfigobject, and copy data into it from the
WifiP2pDevicerepresenting the device you want to connect to. Then call the