├── bin
├── classes.dex
├── resources.ap_
├── WiFiDirectDemo.apk
├── res
│ ├── drawable
│ │ └── machine.png
│ ├── drawable-hdpi
│ │ ├── icon.png
│ │ ├── ic_launcher.png
│ │ ├── ic_action_on_off.png
│ │ └── ic_action_discover.png
│ ├── drawable-ldpi
│ │ ├── icon.png
│ │ ├── ic_launcher.png
│ │ ├── ic_action_on_off.png
│ │ └── ic_action_discover.png
│ ├── drawable-mdpi
│ │ ├── icon.png
│ │ ├── ic_launcher.png
│ │ ├── ic_action_on_off.png
│ │ └── ic_action_discover.png
│ └── drawable-xhdpi
│ │ ├── ic_launcher.png
│ │ ├── ic_action_on_off.png
│ │ └── ic_action_discover.png
├── classes
│ └── com
│ │ └── example
│ │ └── android
│ │ └── wifidirect
│ │ ├── R.class
│ │ ├── R$id.class
│ │ ├── R$attr.class
│ │ ├── R$menu.class
│ │ ├── Utils.class
│ │ ├── R$color.class
│ │ ├── R$dimen.class
│ │ ├── R$layout.class
│ │ ├── R$string.class
│ │ ├── BuildConfig.class
│ │ ├── R$drawable.class
│ │ ├── DeviceListFragment.class
│ │ ├── FileTransferService.class
│ │ ├── WiFiDirectActivity.class
│ │ ├── DeviceDetailFragment.class
│ │ ├── DeviceListFragment$1.class
│ │ ├── WiFiDirectActivity$1.class
│ │ ├── WiFiDirectActivity$2.class
│ │ ├── WiFiDirectActivity$3.class
│ │ ├── WiFiDirectActivity$4.class
│ │ ├── DeviceDetailFragment$1.class
│ │ ├── DeviceDetailFragment$2.class
│ │ ├── DeviceDetailFragment$3.class
│ │ ├── WiFiDirectBroadcastReceiver.class
│ │ ├── DeviceDetailFragment$ServerAsyncTask.class
│ │ ├── DeviceListFragment$DeviceActionListener.class
│ │ └── DeviceListFragment$WiFiPeerListAdapter.class
└── AndroidManifest.xml
├── res
├── drawable
│ ├── machine.png
│ ├── section_header.xml
│ └── details_view.xml
├── drawable-hdpi
│ ├── icon.png
│ ├── ic_launcher.png
│ ├── ic_action_on_off.png
│ └── ic_action_discover.png
├── drawable-ldpi
│ ├── icon.png
│ ├── ic_launcher.png
│ ├── ic_action_on_off.png
│ └── ic_action_discover.png
├── drawable-mdpi
│ ├── icon.png
│ ├── ic_launcher.png
│ ├── ic_action_on_off.png
│ └── ic_action_discover.png
├── drawable-xhdpi
│ ├── ic_launcher.png
│ ├── ic_action_on_off.png
│ └── ic_action_discover.png
├── values
│ ├── colors.xml
│ ├── dimens.xml
│ └── strings.xml
├── menu
│ └── action_items.xml
├── layout-large
│ └── main.xml
├── layout
│ ├── main.xml
│ ├── row_devices.xml
│ ├── device_detail.xml
│ └── device_list.xml
└── layout-land
│ └── main.xml
├── assets
└── sample_file.txt
├── gen
└── com
│ └── example
│ └── android
│ └── wifidirect
│ ├── BuildConfig.java
│ └── R.java
├── .classpath
├── README
├── project.properties
├── .project
├── AndroidManifest.xml
└── src
└── com
└── example
└── android
└── wifidirect
├── FileTransferService.java
├── Utils.java
├── WiFiDirectBroadcastReceiver.java
├── DeviceListFragment.java
├── WiFiDirectActivity.java
└── DeviceDetailFragment.java
/bin/classes.dex:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes.dex
--------------------------------------------------------------------------------
/bin/resources.ap_:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/resources.ap_
--------------------------------------------------------------------------------
/bin/WiFiDirectDemo.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/WiFiDirectDemo.apk
--------------------------------------------------------------------------------
/res/drawable/machine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable/machine.png
--------------------------------------------------------------------------------
/bin/res/drawable/machine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable/machine.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-hdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-ldpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-mdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-hdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-ldpi/ic_action_discover.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-mdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-xhdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-hdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/bin/res/drawable-ldpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-ldpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-mdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/bin/res/drawable-xhdpi/ic_action_on_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-xhdpi/ic_action_on_off.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/res/drawable-xhdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-hdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/bin/res/drawable-ldpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-ldpi/ic_action_discover.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-mdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/bin/res/drawable-xhdpi/ic_action_discover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/res/drawable-xhdpi/ic_action_discover.png
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$id.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$id.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$attr.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$attr.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$menu.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$menu.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/Utils.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/Utils.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$color.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$color.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$dimen.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$dimen.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$layout.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$layout.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$string.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$string.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/BuildConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/BuildConfig.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/R$drawable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/R$drawable.class
--------------------------------------------------------------------------------
/assets/sample_file.txt:
--------------------------------------------------------------------------------
1 | This is a sample text file for wifi_direct demo.
2 |
3 | Once the devices are connected, the server i.e. groupOwner will listen for incoming connections and write this file.
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceListFragment.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceListFragment.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/FileTransferService.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/FileTransferService.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectActivity.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectActivity.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceDetailFragment.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceDetailFragment.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceListFragment$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceListFragment$1.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$1.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$2.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$3.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectActivity$4.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$1.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$2.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$3.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/WiFiDirectBroadcastReceiver.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/WiFiDirectBroadcastReceiver.class
--------------------------------------------------------------------------------
/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FF000000
4 | #FF111111
5 |
6 |
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$ServerAsyncTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceDetailFragment$ServerAsyncTask.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceListFragment$DeviceActionListener.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceListFragment$DeviceActionListener.class
--------------------------------------------------------------------------------
/bin/classes/com/example/android/wifidirect/DeviceListFragment$WiFiPeerListAdapter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ahmontero/wifi-direct-demo/HEAD/bin/classes/com/example/android/wifidirect/DeviceListFragment$WiFiPeerListAdapter.class
--------------------------------------------------------------------------------
/gen/com/example/android/wifidirect/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.example.android.wifidirect;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/drawable/section_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
14 |
16 |
17 |
--------------------------------------------------------------------------------
/res/drawable/details_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
10 |
15 |
16 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This project is based on the original demo from Google. The original example show us how to send an image file
2 | from the client to the group owner. With this version, I try to show how to send data between two devices (be group owner or not).
3 |
4 | ==========
5 | How To Use
6 | ==========
7 | 1. Import into Eclipse
8 |
9 | 2. Run in your device!
10 |
11 | =====
12 | NOTES
13 | =====
14 | The program has been tested on two Samsung Nexus devices both runnig Android 4.1.1
15 |
16 | The devices were resetted to factory default.
17 |
18 | This code is not appropriate for production or comercial uses.
19 |
--------------------------------------------------------------------------------
/res/menu/action_items.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-16
15 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 | 300dp
19 | 200dp
20 |
21 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | WiFiDirectDemo
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/res/layout-large/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
7 |
10 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | WiFi Direct
4 | P2P On/Off
5 | Discover
6 | Connect
7 | Disconnect
8 | Launch Gallery
9 |
10 | yes
11 | no
12 | This device will act as a client. Click on Gallery button to pick a local(stored) file
13 | No devices found. Turn on P2P and perform discovery from the Action Bar
14 | Enable P2P from action bar button above or system settings
15 | "Am I the Group Owner? "
16 | ME
17 | PEERS
18 |
19 |
--------------------------------------------------------------------------------
/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
17 |
18 |
19 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/res/layout-land/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
17 |
18 |
19 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/res/layout/row_devices.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
14 |
19 |
25 |
32 |
33 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
21 |
24 |
25 |
27 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/bin/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
21 |
24 |
25 |
27 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/FileTransferService.java:
--------------------------------------------------------------------------------
1 | // Copyright 2011 Google Inc. All Rights Reserved.
2 |
3 | package com.example.android.wifidirect;
4 |
5 | import java.io.FileNotFoundException;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.OutputStream;
9 | import java.net.InetSocketAddress;
10 | import java.net.Socket;
11 |
12 | import android.app.IntentService;
13 | import android.content.ContentResolver;
14 | import android.content.Context;
15 | import android.content.Intent;
16 | import android.net.Uri;
17 | import android.util.Log;
18 |
19 | /**
20 | * A service that process each file transfer request i.e Intent by opening a
21 | * socket connection with the WiFi Direct Group Owner and writing the file
22 | */
23 | public class FileTransferService extends IntentService {
24 |
25 | private static final int SOCKET_TIMEOUT = 5000;
26 | public static final String ACTION_SEND_FILE = "com.example.android.wifidirect.SEND_FILE";
27 | public static final String EXTRAS_FILE_PATH = "file_url";
28 | public static final String EXTRAS_ADDRESS = "go_host";
29 | public static final String EXTRAS_PORT = "go_port";
30 |
31 | public FileTransferService(String name) {
32 | super(name);
33 | }
34 |
35 | public FileTransferService() {
36 | super("FileTransferService");
37 | }
38 |
39 | /*
40 | * (non-Javadoc)
41 | * @see android.app.IntentService#onHandleIntent(android.content.Intent)
42 | */
43 | @Override
44 | protected void onHandleIntent(Intent intent) {
45 |
46 | Context context = getApplicationContext();
47 | if (intent.getAction().equals(ACTION_SEND_FILE)) {
48 | String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);
49 | String host = intent.getExtras().getString(EXTRAS_ADDRESS);
50 | Socket socket = new Socket();
51 | int port = intent.getExtras().getInt(EXTRAS_PORT);
52 |
53 | try {
54 | Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");
55 | socket.bind(null);
56 | socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
57 |
58 | Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
59 | OutputStream stream = socket.getOutputStream();
60 | ContentResolver cr = context.getContentResolver();
61 | InputStream is = null;
62 | try {
63 | is = cr.openInputStream(Uri.parse(fileUri));
64 | } catch (FileNotFoundException e) {
65 | Log.d(WiFiDirectActivity.TAG, e.toString());
66 | }
67 | DeviceDetailFragment.copyFile(is, stream);
68 | Log.d(WiFiDirectActivity.TAG, "Client: Data written");
69 | } catch (IOException e) {
70 | Log.e(WiFiDirectActivity.TAG, e.getMessage());
71 | } finally {
72 | if (socket != null) {
73 | if (socket.isConnected()) {
74 | try {
75 | socket.close();
76 | } catch (IOException e) {
77 | // Give up
78 | e.printStackTrace();
79 | }
80 | }
81 | }
82 | }
83 |
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/res/layout/device_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
14 |
19 |
24 |
29 |
35 |
36 |
40 |
44 |
48 |
52 |
53 |
61 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/Utils.java:
--------------------------------------------------------------------------------
1 | package com.example.android.wifidirect;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.FileReader;
5 | import java.io.IOException;
6 | import java.net.Inet4Address;
7 | import java.net.InetAddress;
8 | import java.net.NetworkInterface;
9 | import java.net.SocketException;
10 | import java.util.Enumeration;
11 |
12 | import android.util.Log;
13 |
14 | public class Utils {
15 |
16 | private final static String p2pInt = "p2p-p2p0";
17 |
18 | public static String getIPFromMac(String MAC) {
19 | /*
20 | * method modified from:
21 | *
22 | * http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/
23 | *
24 | * */
25 | BufferedReader br = null;
26 | try {
27 | br = new BufferedReader(new FileReader("/proc/net/arp"));
28 | String line;
29 | while ((line = br.readLine()) != null) {
30 |
31 | String[] splitted = line.split(" +");
32 | if (splitted != null && splitted.length >= 4) {
33 | // Basic sanity check
34 | String device = splitted[5];
35 | if (device.matches(".*" +p2pInt+ ".*")){
36 | String mac = splitted[3];
37 | if (mac.matches(MAC)) {
38 | return splitted[0];
39 | }
40 | }
41 | }
42 | }
43 | } catch (Exception e) {
44 | e.printStackTrace();
45 | } finally {
46 | try {
47 | br.close();
48 | } catch (IOException e) {
49 | e.printStackTrace();
50 | }
51 | }
52 | return null;
53 | }
54 |
55 |
56 | public static String getLocalIPAddress() {
57 | /*
58 | * modified from:
59 | *
60 | * http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
61 | *
62 | * */
63 | try {
64 | for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
65 | NetworkInterface intf = en.nextElement();
66 | for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
67 | InetAddress inetAddress = enumIpAddr.nextElement();
68 |
69 | String iface = intf.getName();
70 | if(iface.matches(".*" +p2pInt+ ".*")){
71 | if (inetAddress instanceof Inet4Address) { // fix for Galaxy Nexus. IPv4 is easy to use :-)
72 | return getDottedDecimalIP(inetAddress.getAddress());
73 | }
74 | }
75 | }
76 | }
77 | } catch (SocketException ex) {
78 | Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
79 | } catch (NullPointerException ex) {
80 | Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
81 | }
82 | return null;
83 | }
84 |
85 | private static String getDottedDecimalIP(byte[] ipAddr) {
86 | /*
87 | * ripped from:
88 | *
89 | * http://stackoverflow.com/questions/10053385/how-to-get-each-devices-ip-address-in-wifi-direct-scenario
90 | *
91 | * */
92 | String ipAddrStr = "";
93 | for (int i=0; i 0) {
95 | ipAddrStr += ".";
96 | }
97 | ipAddrStr += ipAddr[i]&0xFF;
98 | }
99 | return ipAddrStr;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/res/layout/device_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
19 |
20 |
21 |
27 |
33 |
38 |
44 |
51 |
52 |
53 |
54 |
59 |
60 |
65 |
66 |
67 |
73 |
74 |
80 |
81 |
--------------------------------------------------------------------------------
/gen/com/example/android/wifidirect/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.example.android.wifidirect;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class color {
14 | public static final int layout_border_color=0x7f040001;
15 | public static final int status_bar_color=0x7f040000;
16 | }
17 | public static final class dimen {
18 | public static final int phone_list_height=0x7f050001;
19 | public static final int tablet_list_width=0x7f050000;
20 | }
21 | public static final class drawable {
22 | public static final int details_view=0x7f020000;
23 | public static final int ic_action_discover=0x7f020001;
24 | public static final int ic_action_on_off=0x7f020002;
25 | public static final int ic_launcher=0x7f020003;
26 | public static final int icon=0x7f020004;
27 | public static final int machine=0x7f020005;
28 | public static final int section_header=0x7f020006;
29 | }
30 | public static final class id {
31 | public static final int atn_direct_discover=0x7f080012;
32 | public static final int atn_direct_enable=0x7f080011;
33 | public static final int btn_connect=0x7f080000;
34 | public static final int btn_disconnect=0x7f080001;
35 | public static final int btn_start_client=0x7f080002;
36 | public static final int device_address=0x7f080003;
37 | public static final int device_details=0x7f080010;
38 | public static final int device_info=0x7f080004;
39 | public static final int device_name=0x7f08000f;
40 | public static final int frag_detail=0x7f08000e;
41 | public static final int frag_list=0x7f08000d;
42 | public static final int group_ip=0x7f080006;
43 | public static final int group_owner=0x7f080005;
44 | public static final int icon=0x7f080009;
45 | public static final int linearLayout1=0x7f08000c;
46 | public static final int my_name=0x7f08000a;
47 | public static final int my_status=0x7f08000b;
48 | public static final int status_bar=0x7f080007;
49 | public static final int status_text=0x7f080008;
50 | }
51 | public static final class layout {
52 | public static final int device_detail=0x7f030000;
53 | public static final int device_list=0x7f030001;
54 | public static final int main=0x7f030002;
55 | public static final int row_devices=0x7f030003;
56 | }
57 | public static final class menu {
58 | public static final int action_items=0x7f070000;
59 | }
60 | public static final class string {
61 | public static final int app_name=0x7f060000;
62 | public static final int client_text=0x7f060009;
63 | public static final int connect_peer_button=0x7f060003;
64 | public static final int disconnect_peer_button=0x7f060004;
65 | public static final int discover_peers_button=0x7f060002;
66 | public static final int empty=0x7f060006;
67 | public static final int empty_message=0x7f06000a;
68 | public static final int enable_p2p_button=0x7f060001;
69 | public static final int get_file_button=0x7f060005;
70 | public static final int group_owner_text=0x7f06000c;
71 | public static final int label_me=0x7f06000d;
72 | public static final int label_peers=0x7f06000e;
73 | public static final int no=0x7f060008;
74 | public static final int p2p_off_warning=0x7f06000b;
75 | public static final int yes=0x7f060007;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/WiFiDirectBroadcastReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.android.wifidirect;
18 |
19 | import android.content.BroadcastReceiver;
20 | import android.content.Context;
21 | import android.content.Intent;
22 | import android.net.NetworkInfo;
23 | import android.net.wifi.p2p.WifiP2pDevice;
24 | import android.net.wifi.p2p.WifiP2pManager;
25 | import android.net.wifi.p2p.WifiP2pManager.Channel;
26 | import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
27 | import android.util.Log;
28 |
29 | /**
30 | * A BroadcastReceiver that notifies of important wifi p2p events.
31 | */
32 | public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
33 |
34 | private WifiP2pManager manager;
35 | private Channel channel;
36 | private WiFiDirectActivity activity;
37 |
38 | /**
39 | * @param manager WifiP2pManager system service
40 | * @param channel Wifi p2p channel
41 | * @param activity activity associated with the receiver
42 | */
43 | public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
44 | WiFiDirectActivity activity) {
45 | super();
46 | this.manager = manager;
47 | this.channel = channel;
48 | this.activity = activity;
49 | }
50 |
51 | /*
52 | * (non-Javadoc)
53 | * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
54 | * android.content.Intent)
55 | */
56 | @Override
57 | public void onReceive(Context context, Intent intent) {
58 | String action = intent.getAction();
59 | if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
60 |
61 | // UI update to indicate wifi p2p status.
62 | int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
63 | if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
64 | // Wifi Direct mode is enabled
65 | activity.setIsWifiP2pEnabled(true);
66 | } else {
67 | activity.setIsWifiP2pEnabled(false);
68 | activity.resetData();
69 |
70 | }
71 | Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
72 | } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
73 |
74 | // request available peers from the wifi p2p manager. This is an
75 | // asynchronous call and the calling activity is notified with a
76 | // callback on PeerListListener.onPeersAvailable()
77 | if (manager != null) {
78 | manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
79 | .findFragmentById(R.id.frag_list));
80 | }
81 | Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
82 | } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
83 |
84 | if (manager == null) {
85 | return;
86 | }
87 |
88 | NetworkInfo networkInfo = (NetworkInfo) intent
89 | .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
90 |
91 | if (networkInfo.isConnected()) {
92 |
93 | // we are connected with the other device, request connection
94 | // info to find group owner IP
95 |
96 | DeviceDetailFragment fragment = (DeviceDetailFragment) activity
97 | .getFragmentManager().findFragmentById(R.id.frag_detail);
98 | manager.requestConnectionInfo(channel, fragment);
99 | } else {
100 | // It's a disconnect
101 | activity.resetData();
102 | }
103 | } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
104 | DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
105 | .findFragmentById(R.id.frag_list);
106 | fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
107 | WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
108 |
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/DeviceListFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.android.wifidirect;
18 |
19 | import android.app.ListFragment;
20 | import android.app.ProgressDialog;
21 | import android.content.Context;
22 | import android.content.DialogInterface;
23 | import android.net.wifi.p2p.WifiP2pConfig;
24 | import android.net.wifi.p2p.WifiP2pDevice;
25 | import android.net.wifi.p2p.WifiP2pDeviceList;
26 | import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
27 | import android.os.Bundle;
28 | import android.util.Log;
29 | import android.view.LayoutInflater;
30 | import android.view.View;
31 | import android.view.ViewGroup;
32 | import android.widget.ArrayAdapter;
33 | import android.widget.ListView;
34 | import android.widget.TextView;
35 |
36 | import java.util.ArrayList;
37 | import java.util.List;
38 |
39 | /**
40 | * A ListFragment that displays available peers on discovery and requests the
41 | * parent activity to handle user interaction events
42 | */
43 | public class DeviceListFragment extends ListFragment implements PeerListListener {
44 |
45 | private List peers = new ArrayList();
46 | ProgressDialog progressDialog = null;
47 | View mContentView = null;
48 | private WifiP2pDevice device;
49 |
50 | @Override
51 | public void onActivityCreated(Bundle savedInstanceState) {
52 | super.onActivityCreated(savedInstanceState);
53 | this.setListAdapter(new WiFiPeerListAdapter(getActivity(), R.layout.row_devices, peers));
54 |
55 | }
56 |
57 | @Override
58 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
59 | mContentView = inflater.inflate(R.layout.device_list, null);
60 | return mContentView;
61 | }
62 |
63 | /**
64 | * @return this device
65 | */
66 | public WifiP2pDevice getDevice() {
67 | return device;
68 | }
69 |
70 | private static String getDeviceStatus(int deviceStatus) {
71 | Log.d(WiFiDirectActivity.TAG, "Peer status :" + deviceStatus);
72 | switch (deviceStatus) {
73 | case WifiP2pDevice.AVAILABLE:
74 | return "Available";
75 | case WifiP2pDevice.INVITED:
76 | return "Invited";
77 | case WifiP2pDevice.CONNECTED:
78 | return "Connected";
79 | case WifiP2pDevice.FAILED:
80 | return "Failed";
81 | case WifiP2pDevice.UNAVAILABLE:
82 | return "Unavailable";
83 | default:
84 | return "Unknown";
85 |
86 | }
87 | }
88 |
89 | /**
90 | * Initiate a connection with the peer.
91 | */
92 | @Override
93 | public void onListItemClick(ListView l, View v, int position, long id) {
94 | WifiP2pDevice device = (WifiP2pDevice) getListAdapter().getItem(position);
95 | ((DeviceActionListener) getActivity()).showDetails(device);
96 | }
97 |
98 | /**
99 | * Array adapter for ListFragment that maintains WifiP2pDevice list.
100 | */
101 | private class WiFiPeerListAdapter extends ArrayAdapter {
102 |
103 | private List items;
104 |
105 | /**
106 | * @param context
107 | * @param textViewResourceId
108 | * @param objects
109 | */
110 | public WiFiPeerListAdapter(Context context, int textViewResourceId,
111 | List objects) {
112 | super(context, textViewResourceId, objects);
113 | items = objects;
114 |
115 | }
116 |
117 | @Override
118 | public View getView(int position, View convertView, ViewGroup parent) {
119 | View v = convertView;
120 | if (v == null) {
121 | LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
122 | Context.LAYOUT_INFLATER_SERVICE);
123 | v = vi.inflate(R.layout.row_devices, null);
124 | }
125 | WifiP2pDevice device = items.get(position);
126 | if (device != null) {
127 | TextView top = (TextView) v.findViewById(R.id.device_name);
128 | TextView bottom = (TextView) v.findViewById(R.id.device_details);
129 | if (top != null) {
130 | top.setText(device.deviceName);
131 | }
132 | if (bottom != null) {
133 | bottom.setText(getDeviceStatus(device.status));
134 | }
135 | }
136 |
137 | return v;
138 |
139 | }
140 | }
141 |
142 | /**
143 | * Update UI for this device.
144 | *
145 | * @param device WifiP2pDevice object
146 | */
147 | public void updateThisDevice(WifiP2pDevice device) {
148 | this.device = device;
149 | TextView view = (TextView) mContentView.findViewById(R.id.my_name);
150 | view.setText(device.deviceName);
151 | view = (TextView) mContentView.findViewById(R.id.my_status);
152 | view.setText(getDeviceStatus(device.status));
153 | }
154 |
155 | @Override
156 | public void onPeersAvailable(WifiP2pDeviceList peerList) {
157 | if (progressDialog != null && progressDialog.isShowing()) {
158 | progressDialog.dismiss();
159 | }
160 | peers.clear();
161 | peers.addAll(peerList.getDeviceList());
162 | ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
163 | if (peers.size() == 0) {
164 | Log.d(WiFiDirectActivity.TAG, "No devices found");
165 | return;
166 | }
167 |
168 | }
169 |
170 | public void clearPeers() {
171 | peers.clear();
172 | ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
173 | }
174 |
175 | /**
176 | *
177 | */
178 | public void onInitiateDiscovery() {
179 | if (progressDialog != null && progressDialog.isShowing()) {
180 | progressDialog.dismiss();
181 | }
182 | progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel", "finding peers", true,
183 | true, new DialogInterface.OnCancelListener() {
184 |
185 | @Override
186 | public void onCancel(DialogInterface dialog) {
187 |
188 | }
189 | });
190 | }
191 |
192 | /**
193 | * An interface-callback for the activity to listen to fragment interaction
194 | * events.
195 | */
196 | public interface DeviceActionListener {
197 |
198 | void showDetails(WifiP2pDevice device);
199 |
200 | void cancelDisconnect();
201 |
202 | void connect(WifiP2pConfig config);
203 |
204 | void disconnect();
205 | }
206 |
207 | }
208 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/WiFiDirectActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.android.wifidirect;
18 |
19 | import android.app.Activity;
20 | import android.content.BroadcastReceiver;
21 | import android.content.Context;
22 | import android.content.Intent;
23 | import android.content.IntentFilter;
24 | import android.net.wifi.p2p.WifiP2pConfig;
25 | import android.net.wifi.p2p.WifiP2pDevice;
26 | import android.net.wifi.p2p.WifiP2pManager;
27 | import android.net.wifi.p2p.WifiP2pManager.ActionListener;
28 | import android.net.wifi.p2p.WifiP2pManager.Channel;
29 | import android.net.wifi.p2p.WifiP2pManager.ChannelListener;
30 | import android.os.Bundle;
31 | import android.provider.Settings;
32 | import android.util.Log;
33 | import android.view.Menu;
34 | import android.view.MenuInflater;
35 | import android.view.MenuItem;
36 | import android.view.View;
37 | import android.widget.Toast;
38 |
39 | import com.example.android.wifidirect.DeviceListFragment.DeviceActionListener;
40 |
41 | /**
42 | * An activity that uses WiFi Direct APIs to discover and connect with available
43 | * devices. WiFi Direct APIs are asynchronous and rely on callback mechanism
44 | * using interfaces to notify the application of operation success or failure.
45 | * The application should also register a BroadcastReceiver for notification of
46 | * WiFi state related events.
47 | */
48 | public class WiFiDirectActivity extends Activity implements ChannelListener, DeviceActionListener {
49 |
50 | public static final String TAG = "wifidirectdemo";
51 | private WifiP2pManager manager;
52 | private boolean isWifiP2pEnabled = false;
53 | private boolean retryChannel = false;
54 |
55 | private final IntentFilter intentFilter = new IntentFilter();
56 | private Channel channel;
57 | private BroadcastReceiver receiver = null;
58 |
59 | /**
60 | * @param isWifiP2pEnabled the isWifiP2pEnabled to set
61 | */
62 | public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
63 | this.isWifiP2pEnabled = isWifiP2pEnabled;
64 | }
65 |
66 | @Override
67 | public void onCreate(Bundle savedInstanceState) {
68 | super.onCreate(savedInstanceState);
69 | setContentView(R.layout.main);
70 |
71 | // add necessary intent values to be matched.
72 |
73 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
74 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
75 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
76 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
77 |
78 | manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
79 | channel = manager.initialize(this, getMainLooper(), null);
80 | }
81 |
82 | /** register the BroadcastReceiver with the intent values to be matched */
83 | @Override
84 | public void onResume() {
85 | super.onResume();
86 | receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
87 | registerReceiver(receiver, intentFilter);
88 | }
89 |
90 | @Override
91 | public void onPause() {
92 | super.onPause();
93 | unregisterReceiver(receiver);
94 | }
95 |
96 | /**
97 | * Remove all peers and clear all fields. This is called on
98 | * BroadcastReceiver receiving a state change event.
99 | */
100 | public void resetData() {
101 | DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
102 | .findFragmentById(R.id.frag_list);
103 | DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
104 | .findFragmentById(R.id.frag_detail);
105 | if (fragmentList != null) {
106 | fragmentList.clearPeers();
107 | }
108 | if (fragmentDetails != null) {
109 | fragmentDetails.resetViews();
110 | }
111 | }
112 |
113 | @Override
114 | public boolean onCreateOptionsMenu(Menu menu) {
115 | MenuInflater inflater = getMenuInflater();
116 | inflater.inflate(R.menu.action_items, menu);
117 | return true;
118 | }
119 |
120 | /*
121 | * (non-Javadoc)
122 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
123 | */
124 | @Override
125 | public boolean onOptionsItemSelected(MenuItem item) {
126 | switch (item.getItemId()) {
127 | case R.id.atn_direct_enable:
128 | if (manager != null && channel != null) {
129 |
130 | // Since this is the system wireless settings activity, it's
131 | // not going to send us a result. We will be notified by
132 | // WiFiDeviceBroadcastReceiver instead.
133 |
134 | startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
135 | } else {
136 | Log.e(TAG, "channel or manager is null");
137 | }
138 | return true;
139 |
140 | case R.id.atn_direct_discover:
141 | if (!isWifiP2pEnabled) {
142 | Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
143 | Toast.LENGTH_SHORT).show();
144 | return true;
145 | }
146 | final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
147 | .findFragmentById(R.id.frag_list);
148 | fragment.onInitiateDiscovery();
149 | manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
150 |
151 | @Override
152 | public void onSuccess() {
153 | Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated",
154 | Toast.LENGTH_SHORT).show();
155 | }
156 |
157 | @Override
158 | public void onFailure(int reasonCode) {
159 | Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode,
160 | Toast.LENGTH_SHORT).show();
161 | }
162 | });
163 | return true;
164 | default:
165 | return super.onOptionsItemSelected(item);
166 | }
167 | }
168 |
169 | @Override
170 | public void showDetails(WifiP2pDevice device) {
171 | DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
172 | .findFragmentById(R.id.frag_detail);
173 | fragment.showDetails(device);
174 |
175 | }
176 |
177 | @Override
178 | public void connect(WifiP2pConfig config) {
179 | manager.connect(channel, config, new ActionListener() {
180 |
181 | @Override
182 | public void onSuccess() {
183 | // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
184 | }
185 |
186 | @Override
187 | public void onFailure(int reason) {
188 | Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
189 | Toast.LENGTH_SHORT).show();
190 | }
191 | });
192 | }
193 |
194 | @Override
195 | public void disconnect() {
196 | final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
197 | .findFragmentById(R.id.frag_detail);
198 | fragment.resetViews();
199 | manager.removeGroup(channel, new ActionListener() {
200 |
201 | @Override
202 | public void onFailure(int reasonCode) {
203 | Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);
204 |
205 | }
206 |
207 | @Override
208 | public void onSuccess() {
209 | fragment.getView().setVisibility(View.GONE);
210 | }
211 |
212 | });
213 | }
214 |
215 | @Override
216 | public void onChannelDisconnected() {
217 | // we will try once more
218 | if (manager != null && !retryChannel) {
219 | Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show();
220 | resetData();
221 | retryChannel = true;
222 | manager.initialize(this, getMainLooper(), this);
223 | } else {
224 | Toast.makeText(this,
225 | "Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
226 | Toast.LENGTH_LONG).show();
227 | }
228 | }
229 |
230 | @Override
231 | public void cancelDisconnect() {
232 |
233 | /*
234 | * A cancel abort request by user. Disconnect i.e. removeGroup if
235 | * already connected. Else, request WifiP2pManager to abort the ongoing
236 | * request
237 | */
238 | if (manager != null) {
239 | final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
240 | .findFragmentById(R.id.frag_list);
241 | if (fragment.getDevice() == null
242 | || fragment.getDevice().status == WifiP2pDevice.CONNECTED) {
243 | disconnect();
244 | } else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE
245 | || fragment.getDevice().status == WifiP2pDevice.INVITED) {
246 |
247 | manager.cancelConnect(channel, new ActionListener() {
248 |
249 | @Override
250 | public void onSuccess() {
251 | Toast.makeText(WiFiDirectActivity.this, "Aborting connection",
252 | Toast.LENGTH_SHORT).show();
253 | }
254 |
255 | @Override
256 | public void onFailure(int reasonCode) {
257 | Toast.makeText(WiFiDirectActivity.this,
258 | "Connect abort request failed. Reason Code: " + reasonCode,
259 | Toast.LENGTH_SHORT).show();
260 | }
261 | });
262 | }
263 | }
264 |
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/src/com/example/android/wifidirect/DeviceDetailFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2011 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.example.android.wifidirect;
18 |
19 | import java.io.File;
20 | import java.io.FileOutputStream;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import java.io.OutputStream;
24 | import java.net.ServerSocket;
25 | import java.net.Socket;
26 |
27 | import android.app.Fragment;
28 | import android.app.ProgressDialog;
29 | import android.content.Context;
30 | import android.content.Intent;
31 | import android.net.Uri;
32 | import android.net.wifi.WpsInfo;
33 | import android.net.wifi.p2p.WifiP2pConfig;
34 | import android.net.wifi.p2p.WifiP2pDevice;
35 | import android.net.wifi.p2p.WifiP2pInfo;
36 | import android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener;
37 | import android.os.AsyncTask;
38 | import android.os.Bundle;
39 | import android.os.Environment;
40 | import android.util.Log;
41 | import android.view.LayoutInflater;
42 | import android.view.View;
43 | import android.view.ViewGroup;
44 | import android.widget.TextView;
45 |
46 | import com.example.android.wifidirect.DeviceListFragment.DeviceActionListener;
47 |
48 | /**
49 | * A fragment that manages a particular peer and allows interaction with device
50 | * i.e. setting up network connection and transferring data.
51 | */
52 | public class DeviceDetailFragment extends Fragment implements ConnectionInfoListener {
53 |
54 | public static final String IP_SERVER = "192.168.49.1";
55 | public static int PORT = 8988;
56 | private static boolean server_running = false;
57 |
58 | protected static final int CHOOSE_FILE_RESULT_CODE = 20;
59 | private View mContentView = null;
60 | private WifiP2pDevice device;
61 | private WifiP2pInfo info;
62 | ProgressDialog progressDialog = null;
63 |
64 | @Override
65 | public void onActivityCreated(Bundle savedInstanceState) {
66 | super.onActivityCreated(savedInstanceState);
67 | }
68 |
69 | @Override
70 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
71 |
72 | mContentView = inflater.inflate(R.layout.device_detail, null);
73 | mContentView.findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {
74 |
75 | @Override
76 | public void onClick(View v) {
77 | WifiP2pConfig config = new WifiP2pConfig();
78 | config.deviceAddress = device.deviceAddress;
79 | config.wps.setup = WpsInfo.PBC;
80 | if (progressDialog != null && progressDialog.isShowing()) {
81 | progressDialog.dismiss();
82 | }
83 | progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel",
84 | "Connecting to :" + device.deviceAddress, true, true
85 | // new DialogInterface.OnCancelListener() {
86 | //
87 | // @Override
88 | // public void onCancel(DialogInterface dialog) {
89 | // ((DeviceActionListener) getActivity()).cancelDisconnect();
90 | // }
91 | // }
92 | );
93 | ((DeviceActionListener) getActivity()).connect(config);
94 |
95 | }
96 | });
97 |
98 | mContentView.findViewById(R.id.btn_disconnect).setOnClickListener(
99 | new View.OnClickListener() {
100 |
101 | @Override
102 | public void onClick(View v) {
103 | ((DeviceActionListener) getActivity()).disconnect();
104 | }
105 | });
106 |
107 | mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
108 | new View.OnClickListener() {
109 |
110 | @Override
111 | public void onClick(View v) {
112 | // Allow user to pick an image from Gallery or other
113 | // registered apps
114 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
115 | intent.setType("image/*");
116 | startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
117 | }
118 | });
119 |
120 | return mContentView;
121 | }
122 |
123 | @Override
124 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
125 |
126 | String localIP = Utils.getLocalIPAddress();
127 | // Trick to find the ip in the file /proc/net/arp
128 | String client_mac_fixed = new String(device.deviceAddress).replace("99", "19");
129 | String clientIP = Utils.getIPFromMac(client_mac_fixed);
130 |
131 | // User has picked an image. Transfer it to group owner i.e peer using
132 | // FileTransferService.
133 | Uri uri = data.getData();
134 | TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
135 | statusText.setText("Sending: " + uri);
136 | Log.d(WiFiDirectActivity.TAG, "Intent----------- " + uri);
137 | Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
138 | serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
139 | serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uri.toString());
140 |
141 | if(localIP.equals(IP_SERVER)){
142 | serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, clientIP);
143 | }else{
144 | serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, IP_SERVER);
145 | }
146 |
147 | serviceIntent.putExtra(FileTransferService.EXTRAS_PORT, PORT);
148 | getActivity().startService(serviceIntent);
149 | }
150 |
151 | @Override
152 | public void onConnectionInfoAvailable(final WifiP2pInfo info) {
153 | if (progressDialog != null && progressDialog.isShowing()) {
154 | progressDialog.dismiss();
155 | }
156 | this.info = info;
157 | this.getView().setVisibility(View.VISIBLE);
158 |
159 | // The owner IP is now known.
160 | TextView view = (TextView) mContentView.findViewById(R.id.group_owner);
161 | view.setText(getResources().getString(R.string.group_owner_text)
162 | + ((info.isGroupOwner == true) ? getResources().getString(R.string.yes)
163 | : getResources().getString(R.string.no)));
164 |
165 | // InetAddress from WifiP2pInfo struct.
166 | view = (TextView) mContentView.findViewById(R.id.device_info);
167 | view.setText("Group Owner IP - " + info.groupOwnerAddress.getHostAddress());
168 |
169 | mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
170 |
171 | if (!server_running){
172 | new ServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text)).execute();
173 | server_running = true;
174 | }
175 |
176 | // hide the connect button
177 | mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);
178 | }
179 |
180 | /**
181 | * Updates the UI with device data
182 | *
183 | * @param device the device to be displayed
184 | */
185 | public void showDetails(WifiP2pDevice device) {
186 | this.device = device;
187 | this.getView().setVisibility(View.VISIBLE);
188 | TextView view = (TextView) mContentView.findViewById(R.id.device_address);
189 | view.setText(device.deviceAddress);
190 | view = (TextView) mContentView.findViewById(R.id.device_info);
191 | view.setText(device.toString());
192 |
193 | }
194 |
195 | /**
196 | * Clears the UI fields after a disconnect or direct mode disable operation.
197 | */
198 | public void resetViews() {
199 | mContentView.findViewById(R.id.btn_connect).setVisibility(View.VISIBLE);
200 | TextView view = (TextView) mContentView.findViewById(R.id.device_address);
201 | view.setText(R.string.empty);
202 | view = (TextView) mContentView.findViewById(R.id.device_info);
203 | view.setText(R.string.empty);
204 | view = (TextView) mContentView.findViewById(R.id.group_owner);
205 | view.setText(R.string.empty);
206 | view = (TextView) mContentView.findViewById(R.id.status_text);
207 | view.setText(R.string.empty);
208 | mContentView.findViewById(R.id.btn_start_client).setVisibility(View.GONE);
209 | this.getView().setVisibility(View.GONE);
210 | }
211 |
212 | /**
213 | * A simple server socket that accepts connection and writes some data on
214 | * the stream.
215 | */
216 | public static class ServerAsyncTask extends AsyncTask {
217 |
218 | private final Context context;
219 | private final TextView statusText;
220 |
221 | /**
222 | * @param context
223 | * @param statusText
224 | */
225 | public ServerAsyncTask(Context context, View statusText) {
226 | this.context = context;
227 | this.statusText = (TextView) statusText;
228 | }
229 |
230 | @Override
231 | protected String doInBackground(Void... params) {
232 | try {
233 | ServerSocket serverSocket = new ServerSocket(PORT);
234 | Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
235 | Socket client = serverSocket.accept();
236 | Log.d(WiFiDirectActivity.TAG, "Server: connection done");
237 | final File f = new File(Environment.getExternalStorageDirectory() + "/"
238 | + context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
239 | + ".jpg");
240 |
241 | File dirs = new File(f.getParent());
242 | if (!dirs.exists())
243 | dirs.mkdirs();
244 | f.createNewFile();
245 |
246 | Log.d(WiFiDirectActivity.TAG, "server: copying files " + f.toString());
247 | InputStream inputstream = client.getInputStream();
248 | copyFile(inputstream, new FileOutputStream(f));
249 | serverSocket.close();
250 | server_running = false;
251 | return f.getAbsolutePath();
252 | } catch (IOException e) {
253 | Log.e(WiFiDirectActivity.TAG, e.getMessage());
254 | return null;
255 | }
256 | }
257 |
258 | /*
259 | * (non-Javadoc)
260 | * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
261 | */
262 | @Override
263 | protected void onPostExecute(String result) {
264 | if (result != null) {
265 | statusText.setText("File copied - " + result);
266 | Intent intent = new Intent();
267 | intent.setAction(android.content.Intent.ACTION_VIEW);
268 | intent.setDataAndType(Uri.parse("file://" + result), "image/*");
269 | context.startActivity(intent);
270 | }
271 |
272 | }
273 |
274 | /*
275 | * (non-Javadoc)
276 | * @see android.os.AsyncTask#onPreExecute()
277 | */
278 | @Override
279 | protected void onPreExecute() {
280 | statusText.setText("Opening a server socket");
281 | }
282 |
283 | }
284 |
285 | public static boolean copyFile(InputStream inputStream, OutputStream out) {
286 | byte buf[] = new byte[1024];
287 | int len;
288 | try {
289 | while ((len = inputStream.read(buf)) != -1) {
290 | out.write(buf, 0, len);
291 |
292 | }
293 | out.close();
294 | inputStream.close();
295 | } catch (IOException e) {
296 | Log.d(WiFiDirectActivity.TAG, e.toString());
297 | return false;
298 | }
299 | return true;
300 | }
301 |
302 | }
303 |
--------------------------------------------------------------------------------