WiFiDirectDemo
Change-Id: I9ea5020a39f02fd2ba4934397fa9a2ea79f781b4
This commit is contained in:
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.wifidirect;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.wifi.WpsInfo;
|
||||
import android.net.wifi.p2p.WifiP2pConfig;
|
||||
import android.net.wifi.p2p.WifiP2pDevice;
|
||||
import android.net.wifi.p2p.WifiP2pInfo;
|
||||
import android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.android.wifidirect.DeviceListFragment.DeviceActionListener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* A fragment that manages a particular peer and allows interaction with device
|
||||
* i.e. setting up network connection and transferring data.
|
||||
*/
|
||||
public class DeviceDetailFragment extends Fragment implements ConnectionInfoListener {
|
||||
|
||||
protected static final int CHOOSE_FILE_RESULT_CODE = 20;
|
||||
private View mContentView = null;
|
||||
private WifiP2pDevice device;
|
||||
private WifiP2pInfo info;
|
||||
ProgressDialog progressDialog = null;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
|
||||
mContentView = inflater.inflate(R.layout.device_detail, null);
|
||||
mContentView.findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
WifiP2pConfig config = new WifiP2pConfig();
|
||||
config.deviceAddress = device.deviceAddress;
|
||||
config.wps.setup = WpsInfo.PBC;
|
||||
if (progressDialog != null && progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel",
|
||||
"Connecting to :" + device.deviceAddress, true, true
|
||||
// new DialogInterface.OnCancelListener() {
|
||||
//
|
||||
// @Override
|
||||
// public void onCancel(DialogInterface dialog) {
|
||||
// ((DeviceActionListener) getActivity()).cancelDisconnect();
|
||||
// }
|
||||
// }
|
||||
);
|
||||
((DeviceActionListener) getActivity()).connect(config);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
mContentView.findViewById(R.id.btn_disconnect).setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
((DeviceActionListener) getActivity()).disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
mContentView.findViewById(R.id.btn_start_client).setOnClickListener(
|
||||
new View.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// Allow user to pick an image from Gallery or other
|
||||
// registered apps
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.setType("image/*");
|
||||
startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
|
||||
}
|
||||
});
|
||||
|
||||
return mContentView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
|
||||
// User has picked an image. Transfer it to group owner i.e peer using
|
||||
// FileTransferService.
|
||||
Uri uri = data.getData();
|
||||
TextView statusText = (TextView) mContentView.findViewById(R.id.status_text);
|
||||
statusText.setText("Sending: " + uri);
|
||||
Log.d(WiFiDirectActivity.TAG, "Intent----------- " + uri);
|
||||
Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);
|
||||
serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);
|
||||
serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, uri.toString());
|
||||
serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS,
|
||||
info.groupOwnerAddress.getHostAddress());
|
||||
serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);
|
||||
getActivity().startService(serviceIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
|
||||
if (progressDialog != null && progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
this.info = info;
|
||||
this.getView().setVisibility(View.VISIBLE);
|
||||
|
||||
// The owner IP is now known.
|
||||
TextView view = (TextView) mContentView.findViewById(R.id.group_owner);
|
||||
view.setText(getResources().getString(R.string.group_owner_text)
|
||||
+ ((info.isGroupOwner == true) ? getResources().getString(R.string.yes)
|
||||
: getResources().getString(R.string.no)));
|
||||
|
||||
// InetAddress from WifiP2pInfo struct.
|
||||
view = (TextView) mContentView.findViewById(R.id.device_info);
|
||||
view.setText("Group Owner IP - " + info.groupOwnerAddress.getHostAddress());
|
||||
|
||||
// After the group negotiation, we assign the group owner as the file
|
||||
// server. The file server is single threaded, single connection server
|
||||
// socket.
|
||||
if (info.groupFormed && info.isGroupOwner) {
|
||||
new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text))
|
||||
.execute();
|
||||
} else if (info.groupFormed) {
|
||||
// The other device acts as the client. In this case, we enable the
|
||||
// get file button.
|
||||
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
|
||||
((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources()
|
||||
.getString(R.string.client_text));
|
||||
}
|
||||
|
||||
// hide the connect button
|
||||
mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the UI with device data
|
||||
*
|
||||
* @param device the device to be displayed
|
||||
*/
|
||||
public void showDetails(WifiP2pDevice device) {
|
||||
this.device = device;
|
||||
this.getView().setVisibility(View.VISIBLE);
|
||||
TextView view = (TextView) mContentView.findViewById(R.id.device_address);
|
||||
view.setText(device.deviceAddress);
|
||||
view = (TextView) mContentView.findViewById(R.id.device_info);
|
||||
view.setText(device.toString());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the UI fields after a disconnect or direct mode disable operation.
|
||||
*/
|
||||
public void resetViews() {
|
||||
mContentView.findViewById(R.id.btn_connect).setVisibility(View.VISIBLE);
|
||||
TextView view = (TextView) mContentView.findViewById(R.id.device_address);
|
||||
view.setText(R.string.empty);
|
||||
view = (TextView) mContentView.findViewById(R.id.device_info);
|
||||
view.setText(R.string.empty);
|
||||
view = (TextView) mContentView.findViewById(R.id.group_owner);
|
||||
view.setText(R.string.empty);
|
||||
view = (TextView) mContentView.findViewById(R.id.status_text);
|
||||
view.setText(R.string.empty);
|
||||
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.GONE);
|
||||
this.getView().setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple server socket that accepts connection and writes some data on
|
||||
* the stream.
|
||||
*/
|
||||
public static class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
|
||||
|
||||
private Context context;
|
||||
private TextView statusText;
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @param statusText
|
||||
*/
|
||||
public FileServerAsyncTask(Context context, View statusText) {
|
||||
this.context = context;
|
||||
this.statusText = (TextView) statusText;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(8988);
|
||||
Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
|
||||
Socket client = serverSocket.accept();
|
||||
Log.d(WiFiDirectActivity.TAG, "Server: connection done");
|
||||
final File f = new File(Environment.getExternalStorageDirectory() + "/"
|
||||
+ context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
|
||||
+ ".jpg");
|
||||
|
||||
File dirs = new File(f.getParent());
|
||||
if (!dirs.exists())
|
||||
dirs.mkdirs();
|
||||
f.createNewFile();
|
||||
|
||||
Log.d(WiFiDirectActivity.TAG, "server: copying files " + f.toString());
|
||||
InputStream inputstream = client.getInputStream();
|
||||
copyFile(inputstream, new FileOutputStream(f));
|
||||
serverSocket.close();
|
||||
return f.getAbsolutePath();
|
||||
} catch (IOException e) {
|
||||
Log.e(WiFiDirectActivity.TAG, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
if (result != null) {
|
||||
statusText.setText("File copied - " + result);
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(android.content.Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.parse("file://" + result), "image/*");
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see android.os.AsyncTask#onPreExecute()
|
||||
*/
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
statusText.setText("Opening a server socket");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean copyFile(InputStream inputStream, OutputStream out) {
|
||||
byte buf[] = new byte[1024];
|
||||
int len;
|
||||
try {
|
||||
while ((len = inputStream.read(buf)) != -1) {
|
||||
out.write(buf, 0, len);
|
||||
|
||||
}
|
||||
out.close();
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
Log.d(WiFiDirectActivity.TAG, e.toString());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.wifidirect;
|
||||
|
||||
import android.app.ListFragment;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.net.wifi.p2p.WifiP2pConfig;
|
||||
import android.net.wifi.p2p.WifiP2pDevice;
|
||||
import android.net.wifi.p2p.WifiP2pDeviceList;
|
||||
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A ListFragment that displays available peers on discovery and requests the
|
||||
* parent activity to handle user interaction events
|
||||
*/
|
||||
public class DeviceListFragment extends ListFragment implements PeerListListener {
|
||||
|
||||
private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
|
||||
ProgressDialog progressDialog = null;
|
||||
View mContentView = null;
|
||||
private WifiP2pDevice device;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
this.setListAdapter(new WiFiPeerListAdapter(getActivity(), R.layout.row_devices, peers));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
mContentView = inflater.inflate(R.layout.device_list, null);
|
||||
return mContentView;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return this device
|
||||
*/
|
||||
public WifiP2pDevice getDevice() {
|
||||
return device;
|
||||
}
|
||||
|
||||
private static String getDeviceStatus(int deviceStatus) {
|
||||
Log.d(WiFiDirectActivity.TAG, "Peer status :" + deviceStatus);
|
||||
switch (deviceStatus) {
|
||||
case WifiP2pDevice.AVAILABLE:
|
||||
return "Available";
|
||||
case WifiP2pDevice.INVITED:
|
||||
return "Invited";
|
||||
case WifiP2pDevice.CONNECTED:
|
||||
return "Connected";
|
||||
case WifiP2pDevice.FAILED:
|
||||
return "Failed";
|
||||
case WifiP2pDevice.UNAVAILABLE:
|
||||
return "Unavailable";
|
||||
default:
|
||||
return "Unknown";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate a connection with the peer.
|
||||
*/
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
WifiP2pDevice device = (WifiP2pDevice) getListAdapter().getItem(position);
|
||||
((DeviceActionListener) getActivity()).showDetails(device);
|
||||
}
|
||||
|
||||
/**
|
||||
* Array adapter for ListFragment that maintains WifiP2pDevice list.
|
||||
*/
|
||||
private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
|
||||
|
||||
private List<WifiP2pDevice> items;
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @param textViewResourceId
|
||||
* @param objects
|
||||
*/
|
||||
public WiFiPeerListAdapter(Context context, int textViewResourceId,
|
||||
List<WifiP2pDevice> objects) {
|
||||
super(context, textViewResourceId, objects);
|
||||
items = objects;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
View v = convertView;
|
||||
if (v == null) {
|
||||
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
|
||||
Context.LAYOUT_INFLATER_SERVICE);
|
||||
v = vi.inflate(R.layout.row_devices, null);
|
||||
}
|
||||
WifiP2pDevice device = items.get(position);
|
||||
if (device != null) {
|
||||
TextView top = (TextView) v.findViewById(R.id.device_name);
|
||||
TextView bottom = (TextView) v.findViewById(R.id.device_details);
|
||||
if (top != null) {
|
||||
top.setText(device.deviceName);
|
||||
}
|
||||
if (bottom != null) {
|
||||
bottom.setText(getDeviceStatus(device.status));
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update UI for this device.
|
||||
*
|
||||
* @param device WifiP2pDevice object
|
||||
*/
|
||||
public void updateThisDevice(WifiP2pDevice device) {
|
||||
this.device = device;
|
||||
TextView view = (TextView) mContentView.findViewById(R.id.my_name);
|
||||
view.setText(device.deviceName);
|
||||
view = (TextView) mContentView.findViewById(R.id.my_status);
|
||||
view.setText(getDeviceStatus(device.status));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPeersAvailable(WifiP2pDeviceList peerList) {
|
||||
if (progressDialog != null && progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
peers.clear();
|
||||
peers.addAll(peerList.getDeviceList());
|
||||
((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
|
||||
if (peers.size() == 0) {
|
||||
Log.d(WiFiDirectActivity.TAG, "No devices found");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void clearPeers() {
|
||||
peers.clear();
|
||||
((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void onInitiateDiscovery() {
|
||||
if (progressDialog != null && progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
progressDialog = ProgressDialog.show(getActivity(), "Press back to cancel", "finding peers", true,
|
||||
true, new DialogInterface.OnCancelListener() {
|
||||
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface-callback for the activity to listen to fragment interaction
|
||||
* events.
|
||||
*/
|
||||
public interface DeviceActionListener {
|
||||
|
||||
void showDetails(WifiP2pDevice device);
|
||||
|
||||
void cancelDisconnect();
|
||||
|
||||
void connect(WifiP2pConfig config);
|
||||
|
||||
void disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
|
||||
package com.example.android.wifidirect;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* A service that process each file transfer request i.e Intent by opening a
|
||||
* socket connection with the WiFi Direct Group Owner and writing the file
|
||||
*/
|
||||
public class FileTransferService extends IntentService {
|
||||
|
||||
private static final int SOCKET_TIMEOUT = 5000;
|
||||
public static final String ACTION_SEND_FILE = "com.example.android.wifidirect.SEND_FILE";
|
||||
public static final String EXTRAS_FILE_PATH = "file_url";
|
||||
public static final String EXTRAS_GROUP_OWNER_ADDRESS = "go_host";
|
||||
public static final String EXTRAS_GROUP_OWNER_PORT = "go_port";
|
||||
|
||||
public FileTransferService(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public FileTransferService() {
|
||||
super("FileTransferService");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see android.app.IntentService#onHandleIntent(android.content.Intent)
|
||||
*/
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
|
||||
Context context = getApplicationContext();
|
||||
if (intent.getAction().equals(ACTION_SEND_FILE)) {
|
||||
String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);
|
||||
String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);
|
||||
Socket socket = new Socket();
|
||||
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
|
||||
|
||||
try {
|
||||
Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");
|
||||
socket.bind(null);
|
||||
socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
|
||||
|
||||
Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
|
||||
OutputStream stream = socket.getOutputStream();
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = cr.openInputStream(Uri.parse(fileUri));
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.d(WiFiDirectActivity.TAG, e.toString());
|
||||
}
|
||||
DeviceDetailFragment.copyFile(is, stream);
|
||||
Log.d(WiFiDirectActivity.TAG, "Client: Data written");
|
||||
} catch (IOException e) {
|
||||
Log.e(WiFiDirectActivity.TAG, e.getMessage());
|
||||
} finally {
|
||||
if (socket != null) {
|
||||
if (socket.isConnected()) {
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
// Give up
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.wifidirect;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.wifi.p2p.WifiP2pConfig;
|
||||
import android.net.wifi.p2p.WifiP2pDevice;
|
||||
import android.net.wifi.p2p.WifiP2pManager;
|
||||
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
|
||||
import android.net.wifi.p2p.WifiP2pManager.Channel;
|
||||
import android.net.wifi.p2p.WifiP2pManager.ChannelListener;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.android.wifidirect.DeviceListFragment.DeviceActionListener;
|
||||
|
||||
/**
|
||||
* An activity that uses WiFi Direct APIs to discover and connect with available
|
||||
* devices. WiFi Direct APIs are asynchronous and rely on callback mechanism
|
||||
* using interfaces to notify the application of operation success or failure.
|
||||
* The application should also register a BroadcastReceiver for notification of
|
||||
* WiFi state related events.
|
||||
*/
|
||||
public class WiFiDirectActivity extends Activity implements ChannelListener, DeviceActionListener {
|
||||
|
||||
public static final String TAG = "wifidirectdemo";
|
||||
private WifiP2pManager manager;
|
||||
private boolean isWifiP2pEnabled = false;
|
||||
private boolean retryChannel = false;
|
||||
|
||||
private final IntentFilter intentFilter = new IntentFilter();
|
||||
private Channel channel;
|
||||
private BroadcastReceiver receiver = null;
|
||||
|
||||
/**
|
||||
* @param isWifiP2pEnabled the isWifiP2pEnabled to set
|
||||
*/
|
||||
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
|
||||
this.isWifiP2pEnabled = isWifiP2pEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main);
|
||||
|
||||
// add necessary intent values to be matched.
|
||||
|
||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
|
||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
|
||||
|
||||
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
|
||||
channel = manager.initialize(this, getMainLooper(), null);
|
||||
}
|
||||
|
||||
/** register the BroadcastReceiver with the intent values to be matched */
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
|
||||
registerReceiver(receiver, intentFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(receiver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all peers and clear all fields. This is called on
|
||||
* BroadcastReceiver receiving a state change event.
|
||||
*/
|
||||
public void resetData() {
|
||||
DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_list);
|
||||
DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_detail);
|
||||
if (fragmentList != null) {
|
||||
fragmentList.clearPeers();
|
||||
}
|
||||
if (fragmentDetails != null) {
|
||||
fragmentDetails.resetViews();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.action_items, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
|
||||
*/
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.atn_direct_enable:
|
||||
if (manager != null && channel != null) {
|
||||
|
||||
// Since this is the system wireless settings activity, it's
|
||||
// not going to send us a result. We will be notified by
|
||||
// WiFiDeviceBroadcastReceiver instead.
|
||||
|
||||
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
|
||||
} else {
|
||||
Log.e(TAG, "channel or manager is null");
|
||||
}
|
||||
return true;
|
||||
|
||||
case R.id.atn_direct_discover:
|
||||
if (!isWifiP2pEnabled) {
|
||||
Toast.makeText(WiFiDirectActivity.this, R.string.p2p_off_warning,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_list);
|
||||
fragment.onInitiateDiscovery();
|
||||
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Toast.makeText(WiFiDirectActivity.this, "Discovery Initiated",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int reasonCode) {
|
||||
Toast.makeText(WiFiDirectActivity.this, "Discovery Failed : " + reasonCode,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showDetails(WifiP2pDevice device) {
|
||||
DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_detail);
|
||||
fragment.showDetails(device);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect(WifiP2pConfig config) {
|
||||
manager.connect(channel, config, new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int reason) {
|
||||
Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_detail);
|
||||
fragment.resetViews();
|
||||
manager.removeGroup(channel, new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void onFailure(int reasonCode) {
|
||||
Log.d(TAG, "Disconnect failed. Reason :" + reasonCode);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
fragment.getView().setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChannelDisconnected() {
|
||||
// we will try once more
|
||||
if (manager != null && !retryChannel) {
|
||||
Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show();
|
||||
resetData();
|
||||
retryChannel = true;
|
||||
manager.initialize(this, getMainLooper(), this);
|
||||
} else {
|
||||
Toast.makeText(this,
|
||||
"Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelDisconnect() {
|
||||
|
||||
/*
|
||||
* A cancel abort request by user. Disconnect i.e. removeGroup if
|
||||
* already connected. Else, request WifiP2pManager to abort the ongoing
|
||||
* request
|
||||
*/
|
||||
if (manager != null) {
|
||||
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
|
||||
.findFragmentById(R.id.frag_list);
|
||||
if (fragment.getDevice() == null
|
||||
|| fragment.getDevice().status == WifiP2pDevice.CONNECTED) {
|
||||
disconnect();
|
||||
} else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE
|
||||
|| fragment.getDevice().status == WifiP2pDevice.INVITED) {
|
||||
|
||||
manager.cancelConnect(channel, new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Toast.makeText(WiFiDirectActivity.this, "Aborting connection",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(int reasonCode) {
|
||||
Toast.makeText(WiFiDirectActivity.this,
|
||||
"Connect abort request failed. Reason Code: " + reasonCode,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.wifidirect;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.p2p.WifiP2pDevice;
|
||||
import android.net.wifi.p2p.WifiP2pManager;
|
||||
import android.net.wifi.p2p.WifiP2pManager.Channel;
|
||||
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* A BroadcastReceiver that notifies of important wifi p2p events.
|
||||
*/
|
||||
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
private WifiP2pManager manager;
|
||||
private Channel channel;
|
||||
private WiFiDirectActivity activity;
|
||||
|
||||
/**
|
||||
* @param manager WifiP2pManager system service
|
||||
* @param channel Wifi p2p channel
|
||||
* @param activity activity associated with the receiver
|
||||
*/
|
||||
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
|
||||
WiFiDirectActivity activity) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.channel = channel;
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
|
||||
* android.content.Intent)
|
||||
*/
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
|
||||
|
||||
// UI update to indicate wifi p2p status.
|
||||
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
|
||||
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
||||
// Wifi Direct mode is enabled
|
||||
activity.setIsWifiP2pEnabled(true);
|
||||
} else {
|
||||
activity.setIsWifiP2pEnabled(false);
|
||||
activity.resetData();
|
||||
|
||||
}
|
||||
Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
|
||||
} 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 (manager != null) {
|
||||
manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
|
||||
.findFragmentById(R.id.frag_list));
|
||||
}
|
||||
Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
|
||||
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
|
||||
|
||||
if (manager == 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
|
||||
|
||||
DeviceDetailFragment fragment = (DeviceDetailFragment) activity
|
||||
.getFragmentManager().findFragmentById(R.id.frag_detail);
|
||||
manager.requestConnectionInfo(channel, fragment);
|
||||
} else {
|
||||
// It's a disconnect
|
||||
activity.resetData();
|
||||
}
|
||||
} 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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user