Allow child device to establish new connection after parent disconnects

Previously the monitor activity would only support one connection,
after which the activity would need to be restarted. With this change,
if a connection is established with a parent device but is eventually
disconnected the child device will begin advertising again.

Note that because the child device can only support one connection
at a time currently, after the connection is established it will
now stop advertising. When a connection is lost and advertising
starts again, it may end up advertising as another service. E.g.
  ProtectBabyMonitor (2)
instead of
  ProtectBabyMonitor
This commit is contained in:
Branden Archer
2015-12-28 16:12:49 -05:00
parent 375242d6fb
commit c25b0fdeba

View File

@@ -42,7 +42,6 @@ public class MonitorActivity extends Activity
NsdManager.RegistrationListener _registrationListener; NsdManager.RegistrationListener _registrationListener;
ServerSocket _serverSocket;
Thread _serviceThread; Thread _serviceThread;
private void serviceConnection(Socket socket) throws IOException private void serviceConnection(Socket socket) throws IOException
@@ -86,7 +85,6 @@ public class MonitorActivity extends Activity
finally finally
{ {
audioRecord.stop(); audioRecord.stop();
socket.close();
} }
} }
@@ -100,58 +98,71 @@ public class MonitorActivity extends Activity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monitor); setContentView(R.layout.activity_monitor);
try
{
// Initialize a server socket on the next available port.
_serverSocket = new ServerSocket(0);
// Store the chosen port.
int localPort = _serverSocket.getLocalPort();
registerService(localPort);
_serviceThread = new Thread(new Runnable() _serviceThread = new Thread(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
while(Thread.currentThread().isInterrupted() == false)
{
ServerSocket serverSocket = null;
try
{
// Initialize a server socket on the next available port.
serverSocket = new ServerSocket(0);
// Store the chosen port.
int localPort = serverSocket.getLocalPort();
// Register the service so that parent devices can
// locate the child device
registerService(localPort);
// Wait for a parent to find us and connect
Socket socket = serverSocket.accept();
Log.i(TAG, "Connection from parent device received");
// We now have a client connection.
// Unregister so no other clients will
// attempt to connect
serverSocket.close();
serverSocket = null;
unregisterService();
try try
{ {
Socket socket = _serverSocket.accept();
serviceConnection(socket); serviceConnection(socket);
} }
catch (IOException e) finally
{ {
Log.e(TAG, "Failed when serving connection", e); socket.close();
}
}
catch(IOException e)
{
Log.e(TAG, "Connection failed", e);
} }
// If an exception was thrown before the connection
// could be closed, clean it up
if(serverSocket != null)
{
try try
{ {
_serverSocket.close(); serverSocket.close();
} }
catch (IOException e) catch (IOException e)
{ {
Log.e(TAG, "Failed to close stray connection", e);
}
serverSocket = null;
} }
MonitorActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
final TextView statusText = (TextView) findViewById(R.id.textStatus);
statusText.setText(R.string.stopped);
} }
});
} }
}); });
_serviceThread.start(); _serviceThread.start();
} }
catch (IOException e)
{
Log.e(TAG, "Failed to create server socket", e);
}
}
@Override @Override
protected void onDestroy() protected void onDestroy()