public void clickAdvertise(View v) {
   // Register service
   if (mConnection.getLocalPort() > -1) {
     mNsdHelper.registerService(mConnection.getLocalPort());
   } else {
     Log.d(TAG, "ServerSocket isn't bound.");
   }
 }
 public void clickConnect(View v) {
   NsdServiceInfo service = mNsdHelper.getChosenServiceInfo();
   if (service != null) {
     Log.d(TAG, "Connecting.");
     mConnection.connectToServer(service.getHost(), service.getPort());
   } else {
     Log.d(TAG, "No service to connect to!");
   }
 }
 @Override
 protected void onStop() {
   Log.d(TAG, "Being stopped.");
   mNsdHelper.tearDown();
   mConnection.tearDown();
   mNsdHelper = null;
   mConnection = null;
   super.onStop();
 }
 public void clickSend(View v) {
   EditText messageView = (EditText) this.findViewById(R.id.chatInput);
   if (messageView != null) {
     String messageString = messageView.getText().toString();
     if (!messageString.isEmpty()) {
       mConnection.sendMessage(messageString);
     }
     messageView.setText("");
   }
 }