【问题标题】:Android Wifi DirectAndroid Wifi 直连
【发布时间】:2016-08-02 11:14:08
【问题描述】:

我一直在运行 GitHub 上的这个 WifiDirectDemo,几乎没有修改。我面临的问题是在创建套接字连接期间..我想是的..所以当我在一部手机上选择图像以将其传输到另一部手机时,我的应用程序正在其他手机上关闭,但出现此异常:

java.net.SocketException sendto ECONNRESET 失败(对等方重置连接)。

我已经通过wifi直接连接了两部手机。

这是我收到错误的代码:

这是调用 FileTransferService :

    String localIP = Utils.getLocalIPAddress();
    // Trick to find the ip in the file /proc/net/arp
    String client_mac_fixed = new     String(this.device.deviceAddress).replace("99", "19");
    String clientIP = Utils.getIPFromMac(client_mac_fixed);


    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());

    if(localIP.equals(IP_SERVER)){
        serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, clientIP);
    }else{
        serviceIntent.putExtra(FileTransferService.EXTRAS_ADDRESS, IP_SERVER);
    }

    serviceIntent.putExtra(FileTransferService.EXTRAS_PORT, PORT);
    getActivity().startService(serviceIntent);

后台任务打开服务器套接字:

    protected String doInBackground(Void... params) {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            Log.d(WifiDirectActivity.TAG, "Server: Socket opened");
            Socket client = serverSocket.accept();
            Log.d(WifiDirectActivity.TAG, "Server: connection done");
            final File f = new File("amankush.txt");

            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();

            Log.d(WifiDirectActivity.TAG, "server: copying files " + f.toString());
            InputStream str = client.getInputStream();
            copyFile(str, new FileOutputStream(f));
            //DataInputStream inputstream =new DataInputStream( client.getInputStream());
            //String str  = inputstream.readUTF();
            serverSocket.close();
            server_running = false;
            return f.getAbsolutePath();
        } catch (IOException e) {
            Log.e(WifiDirectActivity.TAG, e.getMessage());
            return null;
        }
    }


    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            statusText.setText("File copied - " + result);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            context.startActivity(intent);
        }

    }

    @Override
    protected void onPreExecute() {
        statusText.setText("Opening a server socket");
    }

最后是文件传输服务:

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_ADDRESS = "go_host";
public static final String EXTRAS_PORT = "go_port";

public FileTransferService(String name) {
    super(name);
}

public FileTransferService() {
    super("FileTransferService");
}


@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_ADDRESS);
        Socket socket = new Socket();
        int port = intent.getExtras().getInt(EXTRAS_PORT);

        try {
            Log.d(WifiDirectActivity.TAG, "Opening client socket - ");
            socket.bind(null);
            socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);
            if(socket.isBound() && socket.isConnected() && socket.isClosed())
                Log.d(WifiDirectActivity.TAG, "Socket not bound");
            Log.d(WifiDirectActivity.TAG, "Client socket - " + socket.isConnected());
            OutputStream os = socket.getOutputStream();
            ContentResolver cr = context.getContentResolver();
            InputStream stream = null;
            try {
                stream = cr.openInputStream(Uri.parse(fileUri));
                //stream = new DataOutputStream(socket.getOutputStream());
                //stream.writeUTF("a string");
            } catch (FileNotFoundException e) {
                Log.d(WifiDirectActivity.TAG, e.toString());
            }
            DeviceDetailFragment.copyFile(stream, os);
            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();
                    }
                }
            }
        }
    }

谁能告诉我为什么会这样?

【问题讨论】:

    标签: android wifi-direct


    【解决方案1】:

    我遇到了同样的问题...

    看看这个:

    String localIP = Utils.getLocalIPAddress();
    

    转到Utils 类,它总是返回null。更改要返回的值。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多