diff --git a/src/protect/babymonitor/MonitorActivity.java b/src/protect/babymonitor/MonitorActivity.java index d58ea65..35e5e42 100644 --- a/src/protect/babymonitor/MonitorActivity.java +++ b/src/protect/babymonitor/MonitorActivity.java @@ -16,22 +16,54 @@ */ package protect.babymonitor; +import java.io.IOException; +import java.net.ServerSocket; import android.app.Activity; +import android.content.Context; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.widget.TextView; public class MonitorActivity extends Activity { final String TAG = "BabyMonitor"; + NsdManager _nsdManager; + + NsdManager.RegistrationListener _registrationListener; + + ServerSocket _serverSocket; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "Baby monitor start"); + + _nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE); + super.onCreate(savedInstanceState); setContentView(R.layout.activity_monitor); + + final TextView text = (TextView) findViewById(R.id.textStatus); + text.setText("Loading..."); + + try + { + // Initialize a server socket on the next available port. + _serverSocket = new ServerSocket(0); + + // Store the chosen port. + int localPort = _serverSocket.getLocalPort(); + + registerService(localPort); + } + catch (IOException e) + { + Log.e(TAG, "Failed to create server socket", e); + } } @Override @@ -39,6 +71,14 @@ public class MonitorActivity extends Activity { Log.i(TAG, "Baby monitor stop"); + if(_registrationListener != null) + { + Log.i(TAG, "Unregistering monitoring service"); + + _nsdManager.unregisterService(_registrationListener); + _registrationListener = null; + } + super.onDestroy(); } @@ -62,4 +102,64 @@ public class MonitorActivity extends Activity } return super.onOptionsItemSelected(item); } + + public void registerService(int port) + { + NsdServiceInfo serviceInfo = new NsdServiceInfo(); + serviceInfo.setServiceName("ProtectBabyMonitor"); + serviceInfo.setServiceType("_babymonitor._tcp."); + serviceInfo.setPort(port); + + _registrationListener = new NsdManager.RegistrationListener() + { + @Override + public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) { + // Save the service name. Android may have changed it in order to + // resolve a conflict, so update the name you initially requested + // with the name Android actually used. + final String serviceName = NsdServiceInfo.getServiceName(); + + Log.i(TAG, "Service name: " + serviceName); + + MonitorActivity.this.runOnUiThread(new Runnable() { + @Override + public void run() + { + final TextView statusText = (TextView) findViewById(R.id.textStatus); + statusText.setText("Waiting for connection..."); + + final TextView serviceText = (TextView) findViewById(R.id.textService); + serviceText.setText(serviceName); + } + }); + } + + @Override + public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) + { + // Registration failed! Put debugging code here to determine why. + Log.e(TAG, "Registration failed: " + errorCode); + } + + @Override + public void onServiceUnregistered(NsdServiceInfo arg0) + { + // Service has been unregistered. This only happens when you call + // NsdManager.unregisterService() and pass in this listener. + + Log.i(TAG, "Unregistering service"); + } + + @Override + public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) + { + // Unregistration failed. Put debugging code here to determine why. + + Log.e(TAG, "Unregistration failed: " + errorCode); + } + }; + + _nsdManager.registerService( + serviceInfo, NsdManager.PROTOCOL_DNS_SD, _registrationListener); + } }