Transfer data between 2 android devices

866 Views Asked by At

I'd like write an app to transfer data between 2 android devices on the same wifi network, like as there is a share folder.

How can i do this?

Thanks

EDIT (My solution):

My Server wait for request

private boolean startServer() {
    try {
        server = new ServerSocket(port);
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
    return true;
}

public void runServer() {
    while (this.go) {
        try {
            Log.d("BurgerClub", "Server in attesa di richieste");
            Socket s1 = server.accept();

            OutputStream s1out = s1.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    s1out));

            BufferedReader br = new BufferedReader(new FileReader(this.path));
            String counter = br.readLine();
            counter = counter != null ? counter : "000";

            br.close();
            bw.write(counter);

            bw.close();
            s1.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

My Client (Runnable object)

public void run() {

    try {

        this.openConnection();

        // Se il socket è connesso
        if( !this.s1.isClosed() ) {

            InputStream is = this.s1.getInputStream();
            BufferedReader dis = new BufferedReader(new InputStreamReader(is));

            line = dis.readLine();

            if( !this.previousCounter.equals(line.trim()) ) {
                ((BurgerClub_MonitorActivity) counterContext).runOnUiThread(new Runnable() {  
                    @Override
                    public void run() {
                        TextView edit = (TextView)(((BurgerClub_MonitorActivity) counterContext).findViewById(R.id.textActionCounter));
                        edit.setText(line);
                    }
                });

                this.previousCounter = line.trim();
            }

            dis.close();
        }
    } catch (ConnectException connExc) {
        connExc.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
2

There are 2 best solutions below

0
On

One device needs to serve as a server and the other one will be the client.

The basic flow needs to be something of this sort:

  1. Server device opens a socket and listens on it.
  2. Server device broadcasts the local IP and port it's listening on.
  3. Client device receives broadcast and initiates a connection.
  4. Perform data transfer.
2
On