Android Open Source - Android-Apps Discovery






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  Copyright (C) 2011  Tom Quist/*  w w  w  . j  a  va2s .c om*/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You can get the GNU General Public License at
 *  http://www.gnu.org/licenses/gpl.html
 */
package com.kniezrec.voiceremotefree;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;

public class Discovery extends AsyncTask<Integer, Integer, InetAddress> {

  public final static InetAddress MULTICAST_ADDRESS;
  public final static short DEFAULT_PORT = 1900;
  private static final int DEFAULT_TIMEOUT = 10000;
  private static final String MEDIA_RENDERER_UUID = "SamsungMRDesc.xml";
  private static final String PERSONAL_MESSAGE_RECEIVER_UUID = "PersonalMessageReceiver.xml";
  private static final String REMOTE_CONTROL_RECEIVER_UUID = "RemoteControlReceiver.xml";
  private static final String REMOCON_SN = "urn:samsung.com:device:RemoteControlReceiver:1";

  static {
    try {
      MULTICAST_ADDRESS = InetAddress.getByName("239.255.255.250");
    } catch (UnknownHostException e) {
      throw new RuntimeException(e);
    }
  }

  private final static String SEARCH_TEAMPLTE = "M-SEARCH * HTTP/1.1\r\n"
      + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n"
      + "USER-AGENT: " + System.getProperty("os.name") + "/"
      + System.getProperty("os.version") + " UPnP/1.1 Voice Remote\r\n"
      + "ST: %s\r\n" + "MX: %d\r\n" + "\r\n";
  private int timeout = DEFAULT_TIMEOUT;
  protected boolean isCSeries = false;

  @Override
  public InetAddress doInBackground(Integer... params) {
    if (params.length > 0) {
      this.timeout = params[0];
    }
    this.isCSeries = false;
    return sendSearchMessage();
  }

  private InetAddress sendSearchMessage() {
    try {
      MulticastSocket socket = new MulticastSocket();
      socket.setReuseAddress(true);
      socket.joinGroup(MULTICAST_ADDRESS);

      // First send a search message
      // String searchMessage = String.format(SEARCH_TEAMPLTE,
      // "upnp:rootdevice", 3);
      String searchMessage = String
          .format(SEARCH_TEAMPLTE, REMOCON_SN, 3);
      byte[] buffer = searchMessage.getBytes("UTF-8");
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
          MULTICAST_ADDRESS, DEFAULT_PORT);
      socket.send(packet);
      searchMessage = String.format(SEARCH_TEAMPLTE,
          "urn:schemas-upnp-org:device:MediaRenderer:1", 3);
      buffer = searchMessage.getBytes("UTF-8");
      packet = new DatagramPacket(buffer, buffer.length,
          MULTICAST_ADDRESS, DEFAULT_PORT);
      socket.send(packet);

      // Now listen for replies
      buffer = new byte[8192];
      packet = new DatagramPacket(buffer, buffer.length);
      long start = SystemClock.uptimeMillis();
      InetAddress addr = null;
      while (SystemClock.uptimeMillis() - start < timeout) {
        try {
          int soTimeout = (int) (timeout - (SystemClock
              .uptimeMillis() - start));
          if (soTimeout < 0)
            break;
          socket.setSoTimeout(soTimeout);
          socket.receive(packet);
          String data = new String(packet.getData(), 0,
              packet.getLength());
          Log.d("TESTSDASDSADA", data);
          if (data.contains(REMOTE_CONTROL_RECEIVER_UUID)) {
            // We have a C- or D-Series TV
            addr = packet.getAddress();
            this.isCSeries = true;
            socket.close();
            return addr;
          } else if (data.contains(PERSONAL_MESSAGE_RECEIVER_UUID)
              || data.contains(MEDIA_RENDERER_UUID)) {
            // B-Series
            addr = packet.getAddress();
          }

          buffer = new byte[8192];
          packet = new DatagramPacket(buffer, buffer.length);
        } catch (SocketTimeoutException e) {
          continue;
        }
      }
      socket.close();
      return addr;
    } catch (UnsupportedEncodingException e) {
      // Should never happen
    } catch (IOException e) {

    }
    return null;
  }

}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java