13

How can i transfer data between two device? I would like to transfer data (mostly image file) between different platforms (android,iphone). Is it possible to use p2p or should i use client-server?

Any help will be appreciated.

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • android2android is possible see for BTchat demo .But BT in Iphone is not designed for this usage. – Its not blank May 02 '12 at 13:04
  • What mechanism do you want to do such a transfer over? BlueTooth, wifi, internet - any of the above? – halfer May 03 '12 at 17:43
  • I want to transfer data over internet using 3G or wifi. – Parvin Gasimzade May 03 '12 at 18:25
  • you have tagged cross platform in your question, then are you using something like phone gap or any hybrid app development for iOS and Android ? so you want to transfer files between these hybrid mobile apps - correct me if i am wrong? – Futur May 10 '12 at 10:10
  • Ya you are right.I want to transfer files between apps running on android and iphone. The idea is very similar to Bump but i couldn't find any source that explains how it is working technically. – Parvin Gasimzade May 10 '12 at 10:30
  • Oh ok, i read long time back but you can re check again, - the bump works like, all the bumps happening on any bump app installed and active on a device is pushed to the server firstly along with the intensity of bump and the geo tags (high precision lat,longs). these are analyzed with their proprietary algo in the server(their IP). with the predicted results they match two devices close by. then file transfer happens. Bump also says they have a lot of mismatches at times and ask the user to bump again. We won't be able to find it their CTO is a PHD in Quantum physics :). – Futur May 10 '12 at 11:41
  • file is sent to the server from the sender and transmitted back to the receiver from the server. – Futur May 10 '12 at 11:41

5 Answers5

12

Have you looked at Qualcomm's AllJoyn library? It works over Bluetooth or wifi only.

The below code is copied from this Incorporating Socket Programming into your Applications

public class ServerActivity extends Activity {

    private TextView serverStatus;

    // default ip
    public static String SERVERIP = "10.0.2.15";

    // designate a port
    public static final int SERVERPORT = 8080;

    private Handler handler = new Handler();

    private ServerSocket serverSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server);
        serverStatus = (TextView) findViewById(R.id.server_status);

        SERVERIP = getLocalIpAddress();

        Thread fst = new Thread(new ServerThread());
        fst.start();
    }

    public class ServerThread implements Runnable {

        public void run() {
            try {
                if (SERVERIP != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Listening on IP: " + SERVERIP);
                        }
                    });
                    serverSocket = new ServerSocket(SERVERPORT);
                    while (true) {
                        // listen for incoming clients
                        Socket client = serverSocket.accept();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Connected.");
                            }
                        });

                        try {
                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                            String line = null;
                            while ((line = in.readLine()) != null) {
                                Log.d("ServerActivity", line);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        // do whatever you want to the front end
                                        // this is where you can be creative
                                    }
                                });
                            }
                            break;
                        } catch (Exception e) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                                }
                            });
                            e.printStackTrace();
                        }
                    }
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Couldn't detect internet connection.");
                        }
                    });
                }
            } catch (Exception e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Error");
                    }
                });
                e.printStackTrace();
            }
        }
    }

    // gets the ip address of your phone's network
    private String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                }
            }
        } catch (SocketException ex) {
            Log.e("ServerActivity", ex.toString());
        }
        return null;
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
             // make sure you close the socket upon exiting
             serverSocket.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
    }

}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
6

If you are OK with Bump, Then they provide an API for the developers to use and its pretty simple to use. Check this out.

To see a live demo of transferring between iOS and Android @ youtube.

Detailed code snippet and example in Github.

PS: i don't belong to bu.mp :).

Futur
  • 8,444
  • 5
  • 28
  • 34
5

While p2p is possible in some cases, you will achieve the most widespread compatibility with a client-server architecture, at the cost of having to deploy, power, connect, and maintain servers.

  • You will be able to use both wifi access points and mobile (3g, etc) networks
  • You have a ready place in the system to measure (or restrict) usage and/or insert ads
  • You don't have to worry about firewalls blocking inbound connections to the devices (common on wifi, almost always the case on mobile)
  • Relative locations of the devices are irrelevant
  • You can interoperate not only with smartphones and tablets, but with traditional PCs and laptops
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
2

I would use a WebService. That makes it easier to maintain your service, becuase the Service Implementation is independent of your app.

Pro Webservice:

  • Reachable under a pre configured DNS
  • Modifications on the Service do not affect the Application (No updates needed on possible bugfixes)

Contra Webservice:

  • All the traffic goes through your Server
  • Application relies on your Server

The you should ask yourself the question, how much data is transferred in your communication? Is there a real benefit of using direct connections?

Tarion
  • 16,283
  • 13
  • 71
  • 107
0

You can use filehub to transfer files between IOS and android.

there is a new device called fielhub on the market can transfer photos/movies/music between iOS and Android Devices, or between mobile devices and SD Card/USB HDD.. I think it is called ravpower filehub.