Introduction
When there is no Wi-Fi access point or Internet access, the Android* app may require a peering connection to establish a connection between two or more android* devices. For example, file sharing apps and multiplayer games.
This feature can be implemented using NFC, Bluetooth, or Wi-Fi peering technology. The preferred technology in a particular case needs to be determined based on the desired distance, connection speed, power dissipation, and unique technical characteristics.
This article evaluates the Wi-Fi peering technology. Wi-Fi peering enables Android 4.0 or later with the appropriate app to connect to each other over Wi-Fi without an access point. The Android Wi-Fi peer framework complies with Wi-Fi Direct standards. In general, Wi-Fi Direct supports faster and farther distances, but requires a higher power consumption than Bluetooth connectivity.
Implement
This article focuses on the basic steps for establishing a socket connection between two Android devices.
To use Wi-Fi peer, the app must have the minimum Software Development Kit version correctly set to 14 and request the following license in the Androidmanifest.xml file:
- ACCESS_WIFI_STATE
- change_wifi_state
- ACCESS_NETWORK_ State
- change_network_state
- internet
Apply methods that need to invoke WifiP2pManager the object initialize() to register the app using the Wi-Fi peer framework. This method must be called before performing a Wi-Fi peer operation.
The app needs to create and register a broadcast sink for the following Wi-Fi peer intent:
- Wifi_p2p_connection_changed_action
- Wifi_p2p_peers_changed_action
- Wifi_p2p_state_changed_action
- Wifi_p2p_this_device_changed_action
After performing the above steps, the app will be able to call the Wi-Fi peer-to Wifip2pmanager object and receive Wi-Fi peer intent. Most of the Wifip2pmanager methods are asynchronous, so developers need to provide listener for each method call to get state and results.
If you want to find available Wi-Fi peer devices, you need to call wifip2pmanager Object discoverpeers () method. The discovery process will always be executed, knowing that the device is turning on the Wi-Fi peer connection or calling stoppeerdiscovery method.
When the application receives wifi_p2p_peers_changed_action intent, use requestpeers () method will be able to get a list of discovered peer devices.
deviceaddress field wifip2pconfig object, and calls connect () method.
After a successful connection, WifiP2pManager the device IP address is obtained by invoking the object's requestConnectInfo() method.
Then, use the IP address to create a Socket and ServerSocket object. Use the standard socket interface to perform communication.
The following is a diagram that invokes a method and receives intent to prepare a connection for a Wi-Fi peer device:
To establish a Wi-Fi peer connection between two Android devices, you need to perform these steps at least. Apps should implement handlers for all Wi-Fi peer intent and listener to fully respond to different events, such as lost connections or Wi-fi disabled. Please refer to the Android documentation for more information.