Wi-Fi Direct (also known as peer-to-peer or P2P) allows your application to quickly find and interact with nearby devices, at a range beyond the capabilities of Bluetooth. Show
The Wi-Fi Direct (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot. If your app is designed to be a part of a secure, near-range network, Wi-Fi Direct is a more suitable option than traditional Wi-Fi ad-hoc networking for the following reasons:
This lesson shows you how to find and connect to nearby devices using Wi-Fi P2P. Set up application permissionsTo use Wi-Fi Direct, add the <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.nsdchat" ... <!-- If your app targets Android 13 (API level 33) or higher, you must declare the NEARBY_WIFI_DEVICES permission. --> <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" <!-- If your app derives location information from Wi-Fi APIs, don't include the "usesPermissionFlags" attribute. --> android:usesPermissionFlags="neverForLocation" /> <uses-permission android:required="true" android:name="android.permission.ACCESS_FINE_LOCATION" <!-- If any feature in your app relies on precise location information, don't include the "maxSdkVersion" attribute. --> android:maxSdkVersion="32" /> <uses-permission android:required="true" android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:required="true" android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:required="true" android:name="android.permission.INTERNET"/> ... Besides the preceding permissions, the following APIs also require Location Mode to be enabled:
Set up a broadcast receiver and peer-to-peer managerTo use Wi-Fi Direct, you need to listen for broadcast intents that tell your application when certain events have occurred. In your application, instantiate an WIFI_P2P_STATE_CHANGED_ACTION Indicates whether Wi-Fi Direct is enabledWIFI_P2P_PEERS_CHANGED_ACTION Indicates that the available peer list has changed.WIFI_P2P_CONNECTION_CHANGED_ACTION Indicates the state of Wi-Fi Direct connectivity has changed.
Starting with Android 10, this is not sticky. If your app has relied on receiving these broadcasts at registration because they had been sticky, use the appropriate get method at initialization to obtain the information instead. WIFI_P2P_THIS_DEVICE_CHANGED_ACTION Indicates this device's configuration details have changed. Starting with Android 10, this is not sticky. If your app has relied on receiving these broadcasts at registration because they had been sticky, use the appropriate
get method at initialization to obtain the information instead.
At the end of the
Now create a new
Finally, add code to register
the intent filter and broadcast receiver when your main activity is active, and unregister them when the activity is paused. The best place to do this is the
Initiate peer discoveryTo start searching for nearby devices with Wi-Fi P2P, call
Keep in mind that this only initiates peer discovery. The Fetch the list of peersNow write the code that fetches and processes the list of peers. First implement the
Now modify your broadcast receiver's
Now, an intent with the action Connect to a peerIn order to connect to a peer, create a new
If each of the devices in your group supports Wi-Fi direct, you don't need to explicitly ask for the group's password when connecting. To allow a device that doesn't support Wi-Fi Direct to join a group, however, you need to
retrieve this password by calling
Note that the
Now go back to the
Create a groupIf you want the device running your app to serve as the group owner for a network that includes legacy devices—that is, devices that don't support Wi-Fi Direct—you follow the same sequence of steps as in the Connect to a Peer section, except you create a new
Note: If all the devices in a network support Wi-Fi Direct, you can use the After you create a group,
you can call Which of the following are types of data that can be synchronized between a mobile device and a computer?The term "Mobile device synchronization" refers to the process of making the same data available on multiple devices. Synchronized data might include contacts, applications, emails, pictures, music, video, calendars, bookmarks, documents, location data, social media data, e-books, or passwords.
Which are examples of universal connection types that allow mobile device synchronization?When dealing with synchronization, a mobile device can connect to a PC via USB (the most common), RS-232 serial connections (less common), Wi-Fi, and Bluetooth.
Which of the following is the unique number used to identify a physical mobile device?Every phone has an IMEI. This stands for International Mobile Station Equipment Identity. This is a unique identifier that allows the mobile provider to identify your specific physical mobile device.
Which of the following are connection types to enable synchronization?Connection types to enable synchronization
Connection types that enable synchronization are often high speed and reliable. These include your home 802.11 Wi-Fi connection as well as USB and FireWire.
|