summaryrefslogtreecommitdiffstats
path: root/docs/html/training/connect-devices-wirelessly/wifi-direct.jd
blob: 99bb243879a03e6ebefc707e8ab149581b6aeea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
page.title=Connecting with Wi-Fi Direct
parent.title=Connecting Devices Wirelessly
parent.link=index.html

trainingnavtop=true
previous.title=Using Network Service Discovery
previous.link=nsd.html
next.title=Service Discovery with Wi-Fi Direct
next.link=nsd-wifi-direct.html

@jd:body

<div id="tb-wrapper">
  <div id="tb">
    <h2>This lesson teaches you how to</h2>
    <ol>
      <li><a href="#permissions">Set Up Application Permissions</a></li>
      <li><a href="#receiver">Set Up the Broadcast Receiver and Peer-to-Peer
        Manager</a></li>
      <li><a href="#discover">Initiate Peer Discovery</a></li>
      <li><a href="#fetch">Fetch the List of Peers</a></li>
      <li><a href="#connect">Connect to a Peer</a></li>
    </ol>
  </div>
</div>

<p>The Wi-Fi Direct&trade; APIs allow applications to connect to nearby devices without
needing to connect to a network or hotspot.  This allows your application to quickly
find and interact with nearby devices, at a range beyond the capabilities of Bluetooth.
</p>
<p>
This lesson shows you how to find and connect to nearby devices using Wi-Fi Direct.
</p>
<h2 id="permissions">Set Up Application Permissions</h2>
<p>In order to use Wi-Fi Direct, add the {@link
android.Manifest.permission#CHANGE_WIFI_STATE}, {@link
android.Manifest.permission#ACCESS_WIFI_STATE},
and {@link android.Manifest.permission#INTERNET}
permissions to your manifest.   Wi-Fi Direct doesn't require an internet connection,
but it does use standard Java sockets, which require the {@link
android.Manifest.permission#INTERNET} permission.
So you need the following permissions to use Wi-Fi Direct.</p>

<pre>
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.nsdchat"
    ...

    &lt;uses-permission
        android:required="true"
        android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
    &lt;uses-permission
        android:required="true"
        android:name="android.permission.CHANGE_WIFI_STATE"/&gt;
    &lt;uses-permission
        android:required="true"
        android:name="android.permission.INTERNET"/&gt;
    ...
</pre>

<h2 id="receiver">Set Up a Broadcast Receiver and Peer-to-Peer Manager</h2>
<p>To 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 {@link
android.content.IntentFilter} and set it to listen for the following:</p>
<dl>
  <dt>{@link android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_STATE_CHANGED_ACTION}</dt>
  <dd>Indicates whether Wi-Fi Peer-To-Peer (P2P) is enabled</dd>
  <dt>{@link android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_PEERS_CHANGED_ACTION}</dt>
  <dd>Indicates that the available peer list has changed.</dd>
  <dt>{@link android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_CONNECTION_CHANGED_ACTION}</dt>
  <dd>Indicates the state of Wi-Fi P2P connectivity has changed.</dd>
  <dt>{@link android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_THIS_DEVICE_CHANGED_ACTION}</dt>
  <dd>Indicates this device's configuration details have changed.</dd>
<pre>
private final IntentFilter intentFilter = new IntentFilter();
...
&#64;Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //  Indicates a change in the Wi-Fi Peer-to-Peer status.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);

    // Indicates a change in the list of available peers.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);

    // Indicates the state of Wi-Fi P2P connectivity has changed.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);

    // Indicates this device's details have changed.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    ...
}
</pre>

  <p>At the end of the {@link android.app.Activity#onCreate onCreate()} method, get an instance of the {@link
android.net.wifi.p2p.WifiP2pManager}, and call its {@link
android.net.wifi.p2p.WifiP2pManager#initialize(Context, Looper, WifiP2pManager.ChannelListener) initialize()}
method.  This method returns a {@link
android.net.wifi.p2p.WifiP2pManager.Channel} object, which you'll use later to
connect your app to the Wi-Fi Direct Framework.</p>

<pre>
&#64;Override

Channel mChannel;

public void onCreate(Bundle savedInstanceState) {
    ....
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
}
</pre>
<p>Now create a new {@link
android.content.BroadcastReceiver} class that you'll use to listen for changes
to the System's Wi-Fi P2P state.  In the {@link
android.content.BroadcastReceiver#onReceive(Context, Intent) onReceive()}
method, add a condition to handle each P2P state change listed above.</p>

<pre>

    &#64;Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            // Determine if Wifi Direct mode is enabled or not, alert
            // the Activity.
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                activity.setIsWifiP2pEnabled(true);
            } else {
                activity.setIsWifiP2pEnabled(false);
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

            // The peer list has changed!  We should probably do something about
            // that.

        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

            // Connection state changed!  We should probably do something about
            // that.

        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                    .findFragmentById(R.id.frag_list);
            fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                    WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

        }
    }
</pre>

<p>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 {@link android.app.Activity#onResume()} and
{@link android.app.Activity#onPause()} methods.

<pre>
    /** register the BroadcastReceiver with the intent values to be matched */
    &#64;Override
    public void onResume() {
        super.onResume();
        receiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
        registerReceiver(receiver, intentFilter);
    }

    &#64;Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
</pre>


<h2 id="discover">Initiate Peer Discovery</h2>
<p>To start searching for nearby devices with Wi-Fi Direct, call {@link
android.net.wifi.p2p.WifiP2pManager#discoverPeers(WifiP2pManager.Channel,
WifiP2pManager.ActionListener) discoverPeers()}.  This method takes the
following arguments:</p>
<ul>
  <li>The {@link android.net.wifi.p2p.WifiP2pManager.Channel} you
  received back when you initialized the peer-to-peer mManager</li>
  <li>An implementation of {@link android.net.wifi.p2p.WifiP2pManager.ActionListener} with methods
  the system invokes for successful and unsuccessful discovery.</li>
</ul>

<pre>
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

        &#64;Override
        public void onSuccess() {
            // Code for when the discovery initiation is successful goes here.
            // No services have actually been discovered yet, so this method
            // can often be left blank.  Code for peer discovery goes in the
            // onReceive method, detailed below.
        }

        &#64;Override
        public void onFailure(int reasonCode) {
            // Code for when the discovery initiation fails goes here.
            // Alert the user that something went wrong.
        }
});
</pre>

<p>Keep in mind that this only <em>initiates</em> peer discovery.  The
{@link android.net.wifi.p2p.WifiP2pManager#discoverPeers(WifiP2pManager.Channel,
WifiP2pManager.ActionListener) discoverPeers()} method starts the discovery process and then
immediately returns.  The system notifies you if the peer discovery process is
successfully initiated by calling methods in the provided action listener.
Also, discovery will remain active until a connection is initiated or a P2P group is
formed.</p>

<h2 id="fetch">Fetch the List of Peers</h2>
<p>Now write the code that fetches and processes the list of peers.  First
implement the {@link android.net.wifi.p2p.WifiP2pManager.PeerListListener}
interface, which provides information about the peers that Wi-Fi Direct has
detected.  The following code snippet illustrates this.</p>

<pre>
    private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
    ...

    private PeerListListener peerListListener = new PeerListListener() {
        &#64;Override
        public void onPeersAvailable(WifiP2pDeviceList peerList) {

            // Out with the old, in with the new.
            peers.clear();
            peers.addAll(peerList.getDeviceList());

            // If an AdapterView is backed by this data, notify it
            // of the change.  For instance, if you have a ListView of available
            // peers, trigger an update.
            ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
            if (peers.size() == 0) {
                Log.d(WiFiDirectActivity.TAG, "No devices found");
                return;
            }
        }
    }
</pre>

<p>Now modify your broadcast receiver's {@link
android.content.BroadcastReceiver#onReceive(Context, Intent) onReceive()}
method to call {@link android.net.wifi.p2p.WifiP2pManager#requestPeers
requestPeers()} when an intent with the action {@link
android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_PEERS_CHANGED_ACTION} is received.  You
need to pass this listener into the receiver somehow.  One way is to send it
as an argument to the broadcast receiver's constructor.
</p>

<pre>
public void onReceive(Context context, Intent intent) {
    ...
    else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

        // Request available peers from the wifi p2p manager. This is an
        // asynchronous call and the calling activity is notified with a
        // callback on PeerListListener.onPeersAvailable()
        if (mManager != null) {
            mManager.requestPeers(mChannel, peerListener);
        }
        Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
    }...
}
</pre>

<p>Now, an intent with the action {@link
android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_PEERS_CHANGED_ACTION} intent will
trigger a request for an updated peer list. </p>

<h2 id="connect">Connect to a Peer</h2>
<p>In order to connect to a peer, create a new {@link
android.net.wifi.p2p.WifiP2pConfig} object, and copy data into it from the
{@link android.net.wifi.p2p.WifiP2pDevice} representing the device you want to
connect to.  Then call the {@link
android.net.wifi.p2p.WifiP2pManager#connect(WifiP2pManager.Channel,
WifiP2pConfig, WifiP2pManager.ActionListener) connect()}
method.</p>

<pre>
    &#64;Override
    public void connect() {
        // Picking the first device found on the network.
        WifiP2pDevice device = peers.get(0);

        WifiP2pConfig config = new WifiP2pConfig();
        config.deviceAddress = device.deviceAddress;
        config.wps.setup = WpsInfo.PBC;

        mManager.connect(mChannel, config, new ActionListener() {

            &#64;Override
            public void onSuccess() {
                // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
            }

            &#64;Override
            public void onFailure(int reason) {
                Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
</pre>

<p>The {@link android.net.wifi.p2p.WifiP2pManager.ActionListener} implemented in
this snippet only notifies you when the <em>initiation</em> succeeds or fails.
To listen for <em>changes</em> in connection state, implement the {@link
android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener} interface. Its {@link
android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener#onConnectionInfoAvailable(WifiP2pInfo)
onConnectionInfoAvailable()}
callback will notify you when the state of the connection changes.  In cases
where multiple devices are going to be connected to a single device (like a game with
3 or more players, or a chat app), one device will be designated the "group
owner".</p>

<pre>
    &#64;Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {

        // InetAddress from WifiP2pInfo struct.
        InetAddress groupOwnerAddress = info.groupOwnerAddress.getHostAddress());

        // After the group negotiation, we can determine the group owner.
        if (info.groupFormed && info.isGroupOwner) {
            // Do whatever tasks are specific to the group owner.
            // One common case is creating a server thread and accepting
            // incoming connections.
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case,
            // you'll want to create a client thread that connects to the group
            // owner.
        }
    }
</pre>

<p>Now go back to the {@link
android.content.BroadcastReceiver#onReceive(Context, Intent) onReceive()} method of the broadcast receiver, and modify the section
that listens for a {@link
android.net.wifi.p2p.WifiP2pManager#WIFI_P2P_CONNECTION_CHANGED_ACTION} intent.
When this intent is received, call {@link
android.net.wifi.p2p.WifiP2pManager#requestConnectionInfo(WifiP2pManager.Channel,
WifiP2pManager.ConnectionInfoListener) requestConnectionInfo()}.  This is an
asynchronous call, so results will be received by the connection info listener
you provide as a parameter.

<pre>
        ...
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

            if (mManager == null) {
                return;
            }

            NetworkInfo networkInfo = (NetworkInfo) intent
                    .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

            if (networkInfo.isConnected()) {

                // We are connected with the other device, request connection
                // info to find group owner IP

                mManager.requestConnectionInfo(mChannel, connectionListener);
            }
            ...
</pre>