How can a device communicate with another using Android SDK?

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

How can a device communicate with another using Android SDK?

Postby tv_sathish » Wed Jan 09, 2008 12:40 pm

Hi all,
I am just thinking of a scenario where two devices separated by a
network try to communicate with each other through the
network(whatever that means) . The idea is to have devices
communicate (without the requirement to use any "real" network to which your
desktop system running Android app is connected to). Is this possible
in Android, using the SDK?

1. Is there a way we can simulate network connection itself with Android SDK to test
our apps?
2. I am a programmer working on my desktop computer and testing Android apps
and cannot be connected to any network/Internet always. Then how to connect
from one device to another device?
3. How can devices be identified (like the way desktop computers are identified using
IP address) for communication?
4. Somebody in this forum replied to one of my questions that latest plugin version
allows multiple emulator instances to run but I am using plugin version 0.3.3 (which
I hope is latest), but does not even show any error message while trying to launch
another emulator and does not open a new emulator window at all)?

If there is no way to simulate the connection itself, it means I and
probably other contestants working on their home computer can't think
of ideas that involve communication between devices?

Thanks and regards,
Posts: 29
Joined: Wed Jan 09, 2008 12:32 pm


Postby faldureon » Thu Jan 10, 2008 2:42 am

Android devices have their own ip addresses and will likely be able to connect to wi-fi much the way your desktop computer does.

If you want to emulate the phone connecting to another phone you need a local network for them to be on, then they'll each have a unique ip address and you'll be able to use sockets(more on this later) to have them talk to each other.

If you need to emulate the phone connecting to your webserver you can just run Tomcat locally on your machine, you will still need a local network as your machine will have to have an ip address for the android emulator to connect to it. My android contest entry uses a Tomcat webserver, I have a wiki page that explains how to set it up to run in your eclipse.

The presence of the ServerSocket class in the api means that you will be able to listen to incoming http connections and even run a webserver from your phone(if you write one :D). I tried using the ServerSocket class but i get an IOException whenever try code like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 try {
  2.                         ServerSocket serverSocket = ServerSocketFactory.getDefault()
  3.                                         .createServerSocket(80);
  4.                         while (mTrucking) {
  5.                                 Socket socket = serverSocket.accept();
  6.                                 InputStream is = socket.getInputStream();
  8.                                 BufferedReader br = new BufferedReader(
  9.                                                 new InputStreamReader(is));
  10.                                 String text = br.readLine();
  11.                                 text = text + new Date(System.currentTimeMillis());
  13.                                 textView.setText("About to send: " + text + " to "
  14.                                                 + socket.getInetAddress());
  16.                                 OutputStream os = socket.getOutputStream();
  17.                                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
  18.                                                 os));
  19.                                 bw.write(text);
  20.                         }
  22.                 } catch (IOException e) {
  23.                         //BOOOO ServerSocket doesn't work;
  24.                 }
Parsed in 0.035 seconds, using GeSHi
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm


Return to General

Who is online

Users browsing this forum: Google [Bot] and 4 guests