title: How to publish device name (Variable) over WiFi, as some File transfer applications do?
body: <p>I want to publish or advertise my device name over WiFi, which is variable and can be changed by user.</p>
  
  <p>For example, Take a file transfer application <code>Xender</code>. We can see the device name set by users on screen when we select <code>receive</code> option in the app. Here is screen shot.</p>
  
  <p><a href="https://i.stack.imgur.com/gOKXQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gOKXQ.png" alt="enter image description here"></a></p>
  
  <p>You can see in the image that name <code>shah.kaushal</code> is appearing. </p>
  
  <p>I've searched many results on Internet, but can't figure out what the exact thing it is. </p>
  
  <p>I know about host name but I think generally it is not changed by such apps and I think it need some special privileges on android to do so. So I'm sure it is not the host name, Which we can get easily from IP address.</p>
  
  <p>Please note I'm not copying any other apps features. I want this in my music player application to share songs. </p>
  
  <p>To do so, I've used TCP connection between devices. And I can successfully send songs from one device to another. But It requires IP address of the device. Which is not user friendly. </p>
  
  <p>Here is the screenshot of my basic music sharing activity, Which lists the available IP addresses and user have to select one IP from the list. </p>
  
  <p><a href="https://i.stack.imgur.com/jM2zM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jM2zM.png" alt="enter image description here"></a></p>
  
  <p>Here instead of IP addresses, I want to display device names. </p>
  
  <p>My code for sending file is:</p>
  
  <pre><code> @Override
  protected Void doInBackground(Void... voids) {
  
  
      System.out.println("array list");
      ArrayList&lt;File&gt; files = new ArrayList&lt;&gt;();
      System.out.println("about to create.");
  
      files.add(new File(wholePath));
      System.out.println("file created..");
      try {
  
  
          //Receiving IP addresses which are available to send our files(Music)!!
          a = getClientList();
  
  
          //update the UI to display the received IP addresses!!
          publishProgress();
  
  
          //busy waiting for user to select appropriate IP address to send files!
          while (destinationAddress.equals("-1")){
  
          }
  
          //User has selected something, It's time to send files there!
          socket = new Socket(destinationAddress,5004);
  
          System.out.println("Connecting...");
          DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
          DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
          System.out.println(files.size());
          //write the number of files to the server
          dos.writeInt(files.size());
          dos.flush();
  
  
          //write file size
          for(int i = 0;i&lt; files.size();i++){
              int file_size = Integer.parseInt(String.valueOf(files.get(i).length()));
              dos.writeLong(file_size);
              dos.flush();
          }
  
          //write file names
          for(int i = 0 ; i &lt; files.size();i++){
              dos.writeUTF(files.get(i).getName());
              dos.flush();
          }
  
          //buffer for file writing, to declare inside or outside loop?
          int n = 0;
          byte[]buf = new byte[4092];
          //outer loop, executes one for each file
          for(int i =0; i &lt; files.size(); i++){
  
              System.out.println(files.get(i).getName());
              //create new fileinputstream for each file
              FileInputStream fis = new FileInputStream(files.get(i));
  
              //write file to dos
              while((n =fis.read(buf)) != -1){
                  dos.write(buf,0,n);
                  dos.flush();
  
              }
  
          }
  
          dos.close();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          xceptionFlag = true;
          e.printStackTrace();
      }
  
      Log.i("===end of start ====", "==");
      try{
          if(!socket.isClosed()){
              socket.close();
          }
      }
      catch (Exception e){
          xceptionFlag = true;
          e.printStackTrace();
      }
  
      return null;
  }
  </code></pre>
  
  <p>And code for receiving file is:</p>
  
  <pre><code>    @Override
  protected Void doInBackground(Void... voids) {
  
  
      try {
  
  
          //this is done isntead of above line because it was givind error of address is already in use.
          ss = new ServerSocket();
          ss.setReuseAddress(true);
          ss.bind(new InetSocketAddress(5004));
  
          System.out.println("waiting");
  
          Socket socket = ss.accept();
          System.out.println("Accepted!");
          DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
          DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
          //read the number of files from the client
          int number = dis.readInt();
          ArrayList&lt;File&gt;files = new ArrayList&lt;File&gt;(number);
          System.out.println("Number of Files to be received: " +number);
  
          ArrayList&lt;Long&gt; fileSize = new ArrayList&lt;&gt;(number);
  
  
          for(int i = 0; i &lt; number ;i++){
              long size = dis.readLong();
              System.out.println(size);
              fileSize.add(size);
          }
  
          //read file names, add files to arraylist
          for(int i = 0; i&lt; number;i++){
              File file = new File(dis.readUTF());
              files.add(file);
          }
          int n = 0;
          byte[]buf = new byte[4092];
  
          //outer loop, executes one for each file
          for(int i = 0; i &lt; files.size();i++){
  
              System.out.println("Receiving file: " + files.get(i).getName());
  
              //Create new Folder for our app, if it is not there and store received files there in our separate folder.
              File folder = new File(Environment.getExternalStorageDirectory() +
                      File.separator + "File");
              boolean success = true;
              if (!folder.exists()) {
                  success = folder.mkdirs();
              }
              if (success) {
                  // Do something on success
              } else {
                  // Do something else on failure
              }
  
  
              //create a new fileoutputstream for each new file
              FileOutputStream fos = new FileOutputStream("mnt/sdcard/File/" +files.get(i).getName());
              //read file
  
              while (fileSize.get(i) &gt; 0 &amp;&amp; (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize.get(i)))) != -1)
              {
                  fos.write(buf,0,n);
                  long x = fileSize.get(i);
                  x = x-n;
                  fileSize.set(i,x);
              }
              fos.close();
          }
      } catch (IOException e) {
          // TODO Auto-generated catch block
          xceptionFlag = true;
          e.printStackTrace();
  
      }
      ////////////////////
      Log.i("== the end of read ====", "==");
      try{
          if(!ss.isClosed()){
              ss.close();
          }
      }
      catch (Exception e){
          xceptionFlag = true;
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>I've included the code for reference. Thanks.</p>