Created
January 25, 2014 23:54
-
-
Save ethernetdan/8625693 to your computer and use it in GitHub Desktop.
it's a gist, get it?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ampvita.bubbleserver; | |
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothServerSocket; | |
import android.bluetooth.BluetoothSocket; | |
import android.os.Handler; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.app.ActionBar; | |
import android.support.v4.app.Fragment; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.os.Build; | |
import android.widget.TextView; | |
import org.apache.http.MethodNotSupportedException; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.lang.reflect.Method; | |
import java.net.InetAddress; | |
import java.net.NetworkInterface; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.net.SocketException; | |
import java.util.Enumeration; | |
import java.util.UUID; | |
public class MainActivity extends ActionBarActivity { | |
TextView serverStatus; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
serverStatus = (TextView)findViewById(R.id.text_holder); | |
Thread fst = new Thread(new ServerThread()); | |
fst.start(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
// default ip | |
public static String SERVERIP = "10.0.2.2"; | |
// designate a port | |
public static final int SERVERPORT = 6002; | |
private Handler handler = new Handler(); | |
private ServerSocket serverSocket; | |
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) { | |
final String data = line; | |
Log.d("ServerActivity", data); | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
/// the fun goes here | |
serverStatus.setText(data); | |
} | |
}); | |
} | |
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) { | |
final Exception ed = e; | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
serverStatus.setText("Error: " + ed.getStackTrace()); | |
} | |
}); | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
try { | |
// make sure you close the socket upon exiting | |
serverSocket.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment