Android SSDP服务发现

时间:2015-12-11 07:02:04

标签: java ssdp

我正在实现一个java程序,它使用SSDP发现扫描我拥有的一些智能设备。我使用的代码仅检测连接到本地网络的10个设备中的2个设备。我无法理解原因。

我的问题是,为什么我无法获取本地网络中的所有设备。

这是我正在使用的代码

static final String HOST = "Host:" + SSDP.ADDRESS + ":" + SSDP.PORT;
static final String MAN = "Man: \"ssdp:discover\"";
 static final String NEWLINE = "\r\n";

int mMX = 10;    /* seconds to delay response */
String mST;     /* Search target */

public SSDPSearchMsg(String ST) {
    mST = ST;
}

public int getmMX() {
    return mMX;
}

public void setmMX(int mMX) {
    this.mMX = mMX;
}

public String getmST() {
    return mST;
}

public void setmST(String mST) {
    this.mST = mST;
}

@Override
public String toString() {
    StringBuilder content = new StringBuilder();

    content.append(SSDP.SL_MSEARCH).append(NEWLINE);
    content.append(HOST).append(NEWLINE);
    content.append(MAN).append(NEWLINE);
    content.append(mST).append(NEWLINE);
    content.append("MX:" + mMX).append(NEWLINE);
    content.append(NEWLINE);

    return content.toString();
}

下面是几个常量

 /* New line definition */
public static final String NEWLINE = "\r\n";

public static final String ADDRESS = "239.255.255.250";
public static final int PORT = 1900;

/* Definitions of start line */
public static final String SL_NOTIFY = "NOTIFY * HTTP/1.1";
public static final String SL_MSEARCH = "M-SEARCH * HTTP/1.1";
public static final String SL_OK = "HTTP/1.1 200 OK";

/* Definitions of search targets */
public static final String ST_RootDevice = "ST: upnp:rootdevice";
public static final String ST_ContentDirectory = "ST: urn:schemas-upnp-org:service:ContentDirectory:1";
public static final String ST_ALL="ST: ssdp:all";
public static final String ST_Media="ST:urn:schemas-upnp-org:device:MediaRenderer:1";


/* Definitions of notification sub type */
public static final String NTS_ALIVE = "NTS:ssdp:alive";
public static final String NTS_BYE = "NTS:ssdp:byebye";
public static final String NTS_UPDATE = "NTS:ssdp:update";

主要课程

public class SSDPSocket {
SocketAddress mSSDPMulticastGroup;
MulticastSocket mSSDPSocket;

public SSDPSocket() throws IOException {
    InetAddress localInAddress = InetAddress.getLocalHost();
    System.out.println("Local address: " + localInAddress.getHostAddress());

    mSSDPMulticastGroup = new InetSocketAddress(SSDP.ADDRESS, SSDP.PORT);
    mSSDPSocket = new MulticastSocket(new InetSocketAddress(localInAddress,
            0));

    NetworkInterface netIf = NetworkInterface
            .getByInetAddress(localInAddress);
    mSSDPSocket.joinGroup(mSSDPMulticastGroup, netIf);
}

/* Used to send SSDP packet */
public void send(String data) throws IOException {
    DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(),
            mSSDPMulticastGroup);

    mSSDPSocket.send(dp);
}

/* Used to receive SSDP packet */
public DatagramPacket receive() throws IOException {
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, buf.length);

    mSSDPSocket.receive(dp);

    return dp;
}

public void close() {
    if (mSSDPSocket != null) {
        mSSDPSocket.close();
    }
}

/* For test purpose */
public static void main(String[] args) {

    SSDPSearchMsg search = new SSDPSearchMsg(SSDP.ST_Media);
    System.out.println(search.toString());

    SSDPSocket sock;
    try {
        sock = new SSDPSocket();
        sock.send(search.toString());

        while (true) {
            DatagramPacket dp = sock.receive();
            String c = new String(dp.getData());
            System.out.println(c);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

有人可以为这个问题提出解决方案吗?

1 个答案:

答案 0 :(得分:0)

我刚刚在SSDP Java客户端上工作并仔细研究了规范。

您缺少“MX”标头。如果未设置标题MX,则SSDP服务“应该”不响应您的请求。 因此,请尝试添加“MX:1”标题,看看它的位置。

Schneider database对此问题有很好的帮助。

此外,如果您仍在尝试使用它,可以查看ssdp client