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;
|
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,57 +98,70 @@ public class MonitorActivity extends Activity
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_monitor);
|
setContentView(R.layout.activity_monitor);
|
||||||
|
|
||||||
try
|
_serviceThread = new Thread(new Runnable()
|
||||||
{
|
{
|
||||||
// Initialize a server socket on the next available port.
|
@Override
|
||||||
_serverSocket = new ServerSocket(0);
|
public void run()
|
||||||
|
|
||||||
// Store the chosen port.
|
|
||||||
int localPort = _serverSocket.getLocalPort();
|
|
||||||
|
|
||||||
registerService(localPort);
|
|
||||||
|
|
||||||
_serviceThread = new Thread(new Runnable()
|
|
||||||
{
|
{
|
||||||
@Override
|
while(Thread.currentThread().isInterrupted() == false)
|
||||||
public void run()
|
|
||||||
{
|
{
|
||||||
try
|
ServerSocket serverSocket = null;
|
||||||
{
|
|
||||||
Socket socket = _serverSocket.accept();
|
|
||||||
serviceConnection(socket);
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
Log.e(TAG, "Failed when serving connection", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_serverSocket.close();
|
// Initialize a server socket on the next available port.
|
||||||
}
|
serverSocket = new ServerSocket(0);
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
// Store the chosen port.
|
||||||
|
int localPort = serverSocket.getLocalPort();
|
||||||
|
|
||||||
MonitorActivity.this.runOnUiThread(new Runnable()
|
// Register the service so that parent devices can
|
||||||
{
|
// locate the child device
|
||||||
@Override
|
registerService(localPort);
|
||||||
public void run()
|
|
||||||
|
// 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);
|
serviceConnection(socket);
|
||||||
statusText.setText(R.string.stopped);
|
|
||||||
}
|
}
|
||||||
});
|
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();
|
});
|
||||||
}
|
_serviceThread.start();
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
Log.e(TAG, "Failed to create server socket", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user