Introduction
Android* applications may require a peer-to-peer connection between two or more Android* devices without a Wi-Fi access point or Internet access. Two examples of this are file sharing applications and multiplayer games.
This functionality can be implemented using NFC, Bluetooth or Wi-Fi Peer-to-Peer technologies. The preferred technology in any specific case depends upon the required distance, connection speed, power consumption and unique technology features.
Wi-Fi Peer-to-Peer technology will be reviewed in this article. Wi-Fi Peer-to-Peer (P2P) allows Android 4.0 or later devices with the appropriate hardware to connect to each other via Wi-Fi without an access point. Android Wi-Fi P2P framework complies with the Wi-Fi Direct standard. Wi-Fi Direct generally provides more speed and distance, but requires more power than Bluetooth connection.
Implementation
This article will highlight the basic steps to setup the socket connection between two Android devices.
To be able to use Wi-Fi P2P, an application must correctly set a minimum SDK version to 14 and request the following permissions in AndroidManifest.xml file:
Application should call initialize()
method of WifiP2pManager
object to register the application with the Wi-Fi P2P framework. This method must be called before any Wi-Fi P2P operations.
Application should create and register a broadcast receiver for the following Wi-Fi P2P intents:
After these steps an application will be able to call Wi-Fi P2P methods in WifiP2pManager object and receive Wi-Fi P2P intents. Most of WifiP2pManager methods are asynchronous so the developer need to provide the listener to each method call to obtain the status and the result.
To discover available Wi-Fi P2P devices, call the discoverPeers()
method of WifiP2pManager
object. The discover process will continue until the device starts the Wi-Fi P2P connection or the stopPeerDiscovery
method will be called.
When the application receives the WIFI_P2P_PEERS_CHANGED_ACTION
intent, a list of discovered peers can be obtained using the requestPeers()
method of WifiP2pManager object.
To connect to the specific device from the fetched peers list, prepare the WifiP2pConfig
object with the completed deviceAddress
field and call the connect()
method of WifiP2pManager object.
After the successful connect, obtain the device IP address by calling requestConnectInfo()
method of WifiP2pManager
object.
After that, create a Socket and ServerSocket objects using the IP address. Perform communications using a standard socket interface.
Below is a diagram of method calls and received intents to prepare a connection for a Wi-Fi P2P device:
These steps are the minimum requirement to create a Wi-Fi P2P connection between two Android devices. An application should implement handlers for all Wi-Fi P2P intents and listeners to adequately react to different events such as connection loss or disabling Wi-Fi. Please refer to Android documentation for more detailed information.
For more such Android resources and tools from Intel, please visit the Intel® Developer Zone
Source: https://software.intel.com/en-us/android/articles/peer-to-peer-applications-on-intel-android-developer-guide