-3

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();
    }
Matteo Codogno
  • 1,569
  • 6
  • 21
  • 36
  • 1
    you might want to use socket programming – Rohit Sep 02 '13 at 13:03
  • 1
    here is an application that doing what you want. Study the code and change it according to your idea: http://code.google.com/p/android-fileshare/source/checkout – fecub Sep 02 '13 at 13:09

2 Answers2

3

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.
Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
0

Read about NFC (Near field communication)

http://developer.android.com/guide/topics/connectivity/nfc/index.html

fish40
  • 5,738
  • 17
  • 50
  • 69