I have a Google TV Box. And i have two mobile phones.
On the GoogleTV Box i run a server.
The Server has one ServerSocket on port 6001.
Also the Server has two sockets for two clients
The first device connects to server and using socket number one
the second the other....
I can simultanous post diffrent messages from 2 devices to the google tv box socket and show them on TV.
I using the following solution:
FOR THE MOBILE CLIENT (2 devices)
create a new android project with a blank activity and copy this code into.
create a layout for the client containing a edittext and a button.
MAKE SURE TO SET INTERNET AND ACCESS NETWORK PERMISSIONS IN ANDROIDMANIFEST.XML!!!
And edit serverIpAddress in this file to your servers accessible IP.
package de.android.googletv.gameclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
private Socket socket;
private String serverIpAddress = "192.168.22.253"; // GOOGLE TV IP
private static final int PLAYER_1_SERVERPORT = 6001;
private Button bt;
private TextView tv;
String DEVICE_NAME;
private class ConnectToServer extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
System.out.println("connecting to server...");
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, PLAYER_1_SERVERPORT);
} catch (UnknownHostException e1) {
System.out.println("ERROR REACHING SERVER! 1");
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("ERROR REACHING SERVER! 2");
e1.printStackTrace();
}
System.out.println("Done!");
return params;
}
protected void onPostExecute() {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
DEVICE_NAME = android.os.Build.MODEL;
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(1);
}
});
new ConnectToServer().execute("");
tv = (TextView) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Random rnd = new Random();
EditText et = (EditText) findViewById(R.id.editText1);
String str = DEVICE_NAME + " ID" + rnd.nextInt() + " says: " + et.getText().toString();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true
);
out.println(str);
Log.d("Client", "Client sent message");
}
catch (UnknownHostException e) {
tv.setText("UnknownHostException");
e.printStackTrace();
}
catch (IOException e) {
tv.setText("IOException");
e.printStackTrace();
}
catch (Exception e) {
tv.setText("Exception");
e.printStackTrace();
}
}
});
}
}
FOR THE SERVER (google tv box)
create a new android project with blank activity and copy this code into.
create a layout with only a textfield
MAKE SURE TO SET INTERNET AND ACCESS NETWORK PERMISSIONS IN ANDROIDMANIFEST.XML!!!
package de.android.googletv.gameserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
// server socket
ServerSocket ss_plr1 = null;
public static final int SERVERPORT_1 = 6001;
int nr_connections = 0;
// socket for player1
Socket s1;
// socket for player2
Socket s2;
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
String mClientMsg = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(1);
}
});
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText("Nothing from client yet");
myCommsThread = new Thread(new CommsThread());
myCommsThread.start();
}
@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss_plr1.close();
}
catch (IOException e) {e.printStackTrace(); }
}
Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
System.out.println("handleMessage("+msg+")");
switch (msg.what) {
case MSG_ID:
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText((String)msg.obj);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
System.out.println("creating new sockets...");
try {
ss_plr1 = new ServerSocket(SERVERPORT_1 );
if (s1 == null)
s1 = ss_plr1.accept();
if (s2 == null)
s2 = ss_plr1.accept();
}
catch (IOException e) {e.printStackTrace();}
new Thread(new ConnectionHandler(s1, myUpdateHandler)).start();
new Thread(new ConnectionHandler(s2, myUpdateHandler)).start();
}
}
}
... and required by the server is the connection handler for threaded messaging ...
create a additional class in server project called: "ConnectionHandler.java" and copy this code into. it handles the async connections.
package de.android.googletv.gameserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.os.Handler;
import android.os.Message;
public class ConnectionHandler implements Runnable {
Socket m_socket;
Handler m_updateHandler;
public ConnectionHandler(Socket socket, Handler updateHandler) {
m_socket = socket;
m_updateHandler = updateHandler;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
String st = null;
st = input.readLine();
Message m = new Message();
m.what = 0x1337;
m.obj = st;
m_updateHandler.sendMessage(m);
}
catch (IOException e) { e.printStackTrace();}
}
}
}
This is not the nicest solution. Multiple "not nice" codings. System.exit(1) for example. And it is only has two devices support. But it works for more than one device, and im pretty sure you will modify it for you purposes. Its based on three web sources and additional attemps from myself to make it work. Its only my prototype....
I CANNOT LINK TO THEM :( ... to less reputation.
If you build and run everthing it should look like this:
https://plus.google.com/u/0/109268217221135029753/posts/3iz6SF1hiJa