Add a pair button to the UI. Fix the hardcoded MAC address string. Pass the device model in the pairing request.

This commit is contained in:
Cameron Gutman 2013-11-04 01:43:35 -05:00
parent 1454ade3d2
commit e50f668aaf
5 changed files with 113 additions and 18 deletions

View File

@ -35,8 +35,8 @@ or to a theme attribute in the form "<code>?[<i>package</i>:][<i>type</i>:]<i>na
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int editText1=0x7f080002;
public static final int hostTextView=0x7f080000;
public static final int hostTextView=0x7f080002;
public static final int pairButton=0x7f080000;
public static final int statusButton=0x7f080001;
public static final int surfaceView=0x7f080003;
}

View File

@ -7,29 +7,31 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Connection" >
<Button
android:id="@+id/pairButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/statusButton"
android:layout_centerHorizontal="true"
android:text="Pair with PC"/>
<EditText
android:id="@+id/hostTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="255.255.255.255"
android:textSize="50dp" >
<requestFocus />
</EditText>
android:hint="IP address of GeForce PC" />
<Button
android:id="@+id/statusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_below="@+id/hostTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="131dp"
android:text="Start Streaming Steam!"
android:textSize="50dp" />
android:text="Start Streaming Steam!" />
</RelativeLayout>

View File

@ -1,9 +1,12 @@
package com.limelight;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.net.SocketException;
import org.xmlpull.v1.XmlPullParserException;
import com.limelight.nvstream.NvConnection;
import com.limelight.nvstream.NvHTTP;
import com.limelight.nvstream.NvmDNS;
import android.os.Bundle;
@ -12,12 +15,13 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
public class Connection extends Activity {
private Button statusButton;
private Button statusButton, pairButton;
private TextView hostText;
private SharedPreferences prefs;
@ -59,6 +63,7 @@ public class Connection extends Activity {
this.statusButton = (Button) findViewById(R.id.statusButton);
this.pairButton = (Button) findViewById(R.id.pairButton);
this.hostText = (TextView) findViewById(R.id.hostTextView);
prefs = getPreferences(0);
@ -72,6 +77,60 @@ public class Connection extends Activity {
Connection.this.startActivity(intent);
}
});
this.pairButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(Connection.this, "Pairing...", Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
String macAddress;
try {
macAddress = NvConnection.getMacAddressString();
} catch (SocketException e) {
e.printStackTrace();
return;
}
if (macAddress == null) {
System.out.println("Couldn't find a MAC address");
return;
}
NvHTTP httpConn = new NvHTTP(hostText.getText().toString(), macAddress);
String message;
try {
if (httpConn.getPairState()) {
message = "Already paired";
}
else {
int session = httpConn.getSessionId();
if (session == 0) {
message = "Pairing was declined by the target";
}
else {
message = "Pairing was successful";
}
}
} catch (IOException e) {
message = e.getMessage();
} catch (XmlPullParserException e) {
message = e.getMessage();
}
final String toastMessage = message;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Connection.this, toastMessage, Toast.LENGTH_LONG).show();
}
});
}
}).start();
}
});
}
}

View File

@ -2,7 +2,11 @@ package com.limelight.nvstream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -33,6 +37,33 @@ public class NvConnection {
this.video = video;
this.threadPool = new ThreadPoolExecutor(1, 1, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>());
}
public static String getMacAddressString() throws SocketException {
Enumeration<NetworkInterface> ifaceList = NetworkInterface.getNetworkInterfaces();
while (ifaceList.hasMoreElements()) {
NetworkInterface iface = ifaceList.nextElement();
/* Look for the first non-loopback interface to use as the MAC address.
* We don't require the interface to be up to avoid having to repair when
* connecting over different interfaces */
if (!iface.isLoopback()) {
byte[] macAddress = iface.getHardwareAddress();
if (macAddress.length == 6) {
StringBuilder addrStr = new StringBuilder();
for (int i = 0; i < macAddress.length; i++) {
addrStr.append(String.format("%02x", macAddress[i]));
if (i != macAddress.length - 1) {
addrStr.append(':');
}
}
return addrStr.toString();
}
}
}
return null;
}
public void start()
{
@ -159,7 +190,7 @@ public class NvConnection {
private void startSteamBigPicture() throws XmlPullParserException, IOException
{
NvHTTP h = new NvHTTP(host, "b0:ee:45:57:5d:5f");
NvHTTP h = new NvHTTP(host, getMacAddressString());
if (!h.getPairState())
{

View File

@ -74,7 +74,10 @@ public class NvHTTP {
public int getSessionId() throws IOException, XmlPullParserException
{
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/pair?mac="+macAddress+"&devicename=ANDROID");
/* Pass the model (minus spaces) as the device name */
String deviceName = android.os.Build.MODEL;
deviceName = deviceName.replace(" ", "");
InputStream in = openHttpConnection("http://"+host+":"+PORT+"/pair?mac="+macAddress+"&devicename="+deviceName);
String sessionId = getXmlString(in, "sessionid");
return Integer.valueOf(sessionId);
}