在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android wifi 直连 文件传输例子源码下载

android wifi 直连 文件传输例子源码下载

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.46M
  • 下载次数:92
  • 浏览次数:3794
  • 发布时间:2015-01-06
  • 实例类别:Android平台开发
  • 发 布 人:wangxunlogin
  • 文件格式:.zip
  • 所需积分:2
 相关标签: wifi

实例介绍

【实例简介】

【实例截图】

【核心代码】

实现了简单的wifi文件共享


/*
 WiFi Direct File Transfer is an open source application that will enable sharing 
 of data between Android devices running Android 4.0 or higher using a WiFi direct
 connection without the use of a separate WiFi access point.This will enable data 
 transfer between devices without relying on any existing network infrastructure. 
 This application is intended to provide a much higher speed alternative to Bluetooth
 file transfers. 

 Copyright (C) 2012  Teja R. Pitla
 Contact: teja.pitla@gmail.com

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package edu.pdx.cs410.wifi.direct.file.transfer;

import java.io.File;

import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	public final int fileRequestID = 55;
	public final int port = 7950;

	
	private WifiP2pManager wifiManager;
	private Channel wifichannel;
	private BroadcastReceiver wifiServerReceiver;

	private IntentFilter wifiServerReceiverIntentFilter;
	
	private String path;
	private File downloadTarget;
	
	private Intent serverServiceIntent; 
	
	private boolean serverThreadActive;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //Block auto opening keyboard
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
               
        wifiManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        wifichannel = wifiManager.initialize(this, getMainLooper(), null);
        wifiServerReceiver = new WiFiServerBroadcastReceiver(wifiManager, wifichannel, this);
              
        wifiServerReceiverIntentFilter = new IntentFilter();;
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        wifiServerReceiverIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        
    	//set status to stopped
    	TextView serverServiceStatus = (TextView) findViewById(R.id.server_status_text);
    	serverServiceStatus.setText(R.string.server_stopped);
        
    	path = "/";
    	downloadTarget = new File(path);
    	
    	serverServiceIntent = null; 
    	serverThreadActive = false;
    	
    	setServerFileTransferStatus("No File being transfered");
    	
        registerReceiver(wifiServerReceiver, wifiServerReceiverIntentFilter);

/*        
        wifiManager.createGroup(wifichannel,  new WifiP2pManager.ActionListener()  {
    	    public void onSuccess() {
    	    	setServerFileTransferStatus("WiFi Group creation successful");

    	    	//Group creation successful
    	    }

    	    public void onFailure(int reason) {
    	    	setServerFileTransferStatus("WiFi Group creation failed");

    	    	//Group creation failed

    	    }
    	});    	
    	*/
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
       
    public void startFileBrowseActivity(View view) {
    	
        Intent clientStartIntent = new Intent(this, FileBrowser.class);
        startActivityForResult(clientStartIntent, fileRequestID);  
        //Path returned to onActivityResult
              
    }
      
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    	if (resultCode == Activity.RESULT_OK && requestCode == fileRequestID) {
    		//Fetch result
    		File targetDir = (File) data.getExtras().get("file");
    		
    		if(targetDir.isDirectory())
    		{
    			if(targetDir.canWrite())
    			{
    				downloadTarget = targetDir;
	    	    	TextView filePath = (TextView) findViewById(R.id.server_file_path);
	    	    	filePath.setText(targetDir.getPath());
	    			setServerFileTransferStatus("Download directory set to "   targetDir.getName());
	    			
    			}
    			else
    			{
	    			setServerFileTransferStatus("You do not have permission to write to "   targetDir.getName());
    			}

    		}
    		else
    		{
    			setServerFileTransferStatus("The selected file is not a directory. Please select a valid download directory.");
    		}

        }
    }
    
    public void startServer(View view) {
    	
    	//If server is already listening on port or transfering data, do not attempt to start server service 
    	if(!serverThreadActive)
    	{
	    	//Create new thread, open socket, wait for connection, and transfer file 
	
	    	serverServiceIntent = new Intent(this, ServerService.class);
	    	serverServiceIntent.putExtra("saveLocation", downloadTarget);
	    	serverServiceIntent.putExtra("port", new Integer(port));
	    	serverServiceIntent.putExtra("serverResult", new ResultReceiver(null) {
	    	    @Override
	    	    protected void onReceiveResult(int resultCode, final Bundle resultData) {
	    	    	
	    	    	if(resultCode == port )
	    	    	{
		    	        if (resultData == null) {
		    	           //Server service has shut down. Download may or may not have completed properly. 
		    	        	serverThreadActive = false;	
		    	        	
		    	        			    	        	
		    	        	final TextView server_status_text = (TextView) findViewById(R.id.server_status_text);
		    	        	server_status_text.post(new Runnable() {
		    	                public void run() {
				    	        	server_status_text.setText(R.string.server_stopped);
		    	                }
		    	        	});	
		    	        	
		    	        			    	        			    	        	
		    	        }
		    	        else
		    	        {    	        	
		    	        	final TextView server_file_status_text = (TextView) findViewById(R.id.server_file_transfer_status);

		    	        	server_file_status_text.post(new Runnable() {
		    	                public void run() {
		    	                	server_file_status_text.setText((String)resultData.get("message"));
		    	                }
		    	        	});		    	   		    	        	
		    	        }
	    	    	}
	    	           	        
	    	    }
	    	});
	    		    		
	    	serverThreadActive = true;
	        startService(serverServiceIntent);
	
	    	//Set status to running
	    	TextView serverServiceStatus = (TextView) findViewById(R.id.server_status_text);
	    	serverServiceStatus.setText(R.string.server_running);
	    	
	    }
    	else
    	{
	    	//Set status to already running
	    	TextView serverServiceStatus = (TextView) findViewById(R.id.server_status_text);
	    	serverServiceStatus.setText("The server is already running");
    		
    	}
    }
    
    public void stopServer(View view) {
    		
    	
    	//stop download thread 
    	if(serverServiceIntent != null)
    	{
    		stopService(serverServiceIntent);
    	
    	}
       	
    }
     
    
    public void startClientActivity(View view) {
    	
    	stopServer(null);
        Intent clientStartIntent = new Intent(this, ClientActivity.class);
        startActivity(clientStartIntent);    		
    }   
    
    
    @Override
    protected void onResume() {
        super.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        //stopServer(null);
        //unregisterReceiver(wifiServerReceiver);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        stopServer(null);
        
        //stopService(serverServiceIntent);
        
        //Unregister broadcast receiver		
		try {
			unregisterReceiver(wifiServerReceiver);
		} catch (IllegalArgumentException e) {
			// This will happen if the server was never running and the stop
			// button was pressed.
			// Do nothing in this case.
		}      
    }
    
    
    
    public void setServerWifiStatus(String message)
    {
    	TextView server_wifi_status_text = (TextView) findViewById(R.id.server_wifi_status_text);
    	server_wifi_status_text.setText(message);	
    }
    
    public void setServerStatus(String message)
    {
    	TextView server_status_text = (TextView) findViewById(R.id.server_status_text_2);
    	server_status_text.setText(message);	
    }
    
    
    public void setServerFileTransferStatus(String message)
    {
    	TextView server_status_text = (TextView) findViewById(R.id.server_file_transfer_status);
    	server_status_text.setText(message);	
    }
    
         
}

标签: wifi

实例下载地址

android wifi 直连 文件传输例子源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 javahand 发表于: 2015-11-26 16:09 02
有参考的价值!

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警