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:
@@ -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,58 +98,71 @@ public class MonitorActivity extends Activity
|
||||
super.onCreate(savedInstanceState);
|
||||
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()
|
||||
{
|
||||
@Override
|
||||
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
|
||||
{
|
||||
Socket socket = _serverSocket.accept();
|
||||
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
|
||||
{
|
||||
_serverSocket.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
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();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Log.e(TAG, "Failed to create server socket", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy()
|
||||
|
||||
Reference in New Issue
Block a user