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;
ServerSocket _serverSocket;
Thread _serviceThread;
private void serviceConnection(Socket socket) throws IOException
@@ -86,7 +85,6 @@ public class MonitorActivity extends Activity
finally
{
audioRecord.stop();
socket.close();
}
}
@@ -100,57 +98,70 @@ public class MonitorActivity extends Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monitor);
try
_serviceThread = new Thread(new Runnable()
{
// 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()
@Override
public void run()
{
@Override
public void run()
while(Thread.currentThread().isInterrupted() == false)
{
try
{
Socket socket = _serverSocket.accept();
serviceConnection(socket);
}
catch (IOException e)
{
Log.e(TAG, "Failed when serving connection", e);
}
ServerSocket serverSocket = null;
try
{
_serverSocket.close();
}
catch (IOException e)
{
// Initialize a server socket on the next available port.
serverSocket = new ServerSocket(0);
}
// Store the chosen port.
int localPort = serverSocket.getLocalPort();
MonitorActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
// 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
{
final TextView statusText = (TextView) findViewById(R.id.textStatus);
statusText.setText(R.string.stopped);
serviceConnection(socket);
}
});
finally
{
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
{
serverSocket.close();
}
catch (IOException e)
{
Log.e(TAG, "Failed to close stray connection", e);
}
serverSocket = null;
}
}
});
_serviceThread.start();
}
catch (IOException e)
{
Log.e(TAG, "Failed to create server socket", e);
}
}
});
_serviceThread.start();
}
@Override