Initial attempt at pairing with child device with address/port

On some networks or some mDNS implementations child discovery may not
work well. As an alternative, allow a parent to connect to a device
directly with an IP Address and port.
This commit is contained in:
Branden Archer
2016-01-01 14:03:56 -05:00
parent 7cb2138d13
commit 562a2bb164
4 changed files with 112 additions and 7 deletions

View File

@@ -36,6 +36,7 @@
android:name="protect.babymonitor.DiscoverActivity" android:name="protect.babymonitor.DiscoverActivity"
android:label="@string/app_name" android:label="@string/app_name"
android:configChanges="orientation|screenSize" android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustPan"
android:parentActivityName="protect.babymonitor.StartActivity" android:parentActivityName="protect.babymonitor.StartActivity"
/> />
<activity <activity

View File

@@ -22,6 +22,64 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="15dip" /> android:layout_height="15dip" />
<TextView
android:id="@+id/parentPairInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/parentPairInstructions" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/ipAddressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/addressTitle" />
<EditText
android:id="@+id/ipAddressField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/exampleAddress"
android:inputType="textUri"
android:textSize="20sp"
android:nextFocusForward="@+id/portField" >
</EditText>
<TextView
android:id="@+id/portTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/portTitle" />
<EditText
android:id="@+id/portField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ems="10"
android:inputType="number"
android:maxLength="5"
android:hint="@string/examplePort"
android:nextFocusForward="@+id/connectViaAddressButton"/>
<Button
android:id="@+id/connectViaAddressButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/connect" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView <TextView
android:id="@+id/selectChildDeviceTitle" android:id="@+id/selectChildDeviceTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@@ -24,4 +24,9 @@
<string name="portTitle">Port:</string> <string name="portTitle">Port:</string>
<string name="addressDescription">Address and port that parent must pair to</string> <string name="addressDescription">Address and port that parent must pair to</string>
<string name="wifiNotConnected">Not connected to a Wi-Fi network</string> <string name="wifiNotConnected">Not connected to a Wi-Fi network</string>
<string name="parentPairInstructions">Enter the IP Address and port of a child and click Connect, or choose a child from the list below</string>
<string name="connect">Connect</string>
<string name="exampleAddress">192.168.1.2</string>
<string name="examplePort">10000</string>
<string name="invalidPort">Either no port was entered or it is invalid</string>
</resources> </resources>

View File

@@ -27,7 +27,10 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
public class DiscoverActivity extends Activity public class DiscoverActivity extends Activity
{ {
@@ -47,6 +50,32 @@ public class DiscoverActivity extends Activity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover); setContentView(R.layout.activity_discover);
final Button connectButton = (Button) findViewById(R.id.connectViaAddressButton);
connectButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.i(TAG, "Connecting to child device via address");
final EditText addressField = (EditText) findViewById(R.id.ipAddressField);
final EditText portField = (EditText) findViewById(R.id.portField);
final String addressString = addressField.getText().toString();
final String portString = portField.getText().toString();
try
{
final int port = Integer.parseInt(portString);
connectToChild(addressString, port, addressString);
}
catch(NumberFormatException e)
{
Toast.makeText(DiscoverActivity.this, R.string.invalidPort, Toast.LENGTH_LONG).show();
}
}
});
startServiceDiscovery("_babymonitor._tcp."); startServiceDiscovery("_babymonitor._tcp.");
} }
@@ -83,13 +112,7 @@ public class DiscoverActivity extends Activity
int position, long id) int position, long id)
{ {
final ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position); final ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position);
final Intent i = new Intent(getApplicationContext(), ListenActivity.class); connectToChild(info.getAddress(), info.getPort(), info.getName());
final Bundle b = new Bundle();
b.putString("address", info.getAddress());
b.putInt("port", info.getPort());
b.putString("name", info.getName());
i.putExtras(b);
startActivity(i);
} }
}); });
@@ -181,6 +204,24 @@ public class DiscoverActivity extends Activity
nsdManager.discoverServices( nsdManager.discoverServices(
serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener); serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener);
} }
/**
* Launch the ListenActivity to connect to the given child device
*
* @param address
* @param port
* @param name
*/
private void connectToChild(final String address, final int port, final String name)
{
final Intent i = new Intent(getApplicationContext(), ListenActivity.class);
final Bundle b = new Bundle();
b.putString("address", address);
b.putInt("port", port);
b.putString("name", name);
i.putExtras(b);
startActivity(i);
}
} }
class ServiceInfoWrapper class ServiceInfoWrapper