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//www.java2s.com
*
* 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.voiceremote2;
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;
publicclass Discovery extends AsyncTask<Integer, Integer, InetAddress> {
publicfinalstatic InetAddress MULTICAST_ADDRESS;
publicfinalstaticshort DEFAULT_PORT = 1900;
privatestaticfinalint DEFAULT_TIMEOUT = 10000;
privatestaticfinal String MEDIA_RENDERER_UUID = "SamsungMRDesc.xml";
privatestaticfinal String PERSONAL_MESSAGE_RECEIVER_UUID = "PersonalMessageReceiver.xml";
privatestaticfinal String REMOTE_CONTROL_RECEIVER_UUID = "RemoteControlReceiver.xml";
privatestaticfinal String REMOCON_SN = "urn:samsung.com:device:RemoteControlReceiver:1";
static {
try {
MULTICAST_ADDRESS = InetAddress.getByName("239.255.255.250");
} catch (UnknownHostException e) {
thrownew RuntimeException(e);
}
}
privatefinalstatic 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";
privateint timeout = DEFAULT_TIMEOUT;
protectedboolean 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 = newbyte[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;
} elseif (data.contains(PERSONAL_MESSAGE_RECEIVER_UUID)
|| data.contains(MEDIA_RENDERER_UUID)) {
// B-Series
addr = packet.getAddress();
}
buffer = newbyte[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;
}
}