Android如何实现ssdp alive receiver功能

时间:2013-08-20 09:37:33

标签: android upnp ssdp

我正在尝试实现一个功能来收听门1900并捕获活动消息和设备IP,我尝试使用一些我发现的lib但我的应用程序一直崩溃只是试图启动应用程序。

这是我的主要功能

     public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash_screen_layout);

   //----------SSDP para identificação da STB--------------------------------

        SSDPSocket sock;
        try {
            sock = new SSDPSocket();

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




        //---------------------------------------------------------------------

其余的lib代码

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketAddress;

public class SSDPSocket {
SocketAddress mSSDPMulticastGroup;
MulticastSocket mSSDPSocket;

public SSDPSocket() throws IOException {
    InetAddress localInAddress = InetAddress.getLocalHost(); **//crashes here first**
    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 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();
    }
}

}


public class SSDP {
/* 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:rootdevice";
public static final String ST_ContentDirectory = "ST:urn:schemas-upnp- org:service:ContentDirectory: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 SSDPSearchMsg {

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

int mMX = 3;    /* 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();
}
}

2 个答案:

答案 0 :(得分:1)

似乎没有错。

您是否在Mainifest中添加了多播权限?

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"></uses-permission>

答案 1 :(得分:1)

Android不喜欢你在主线程中执行昂贵的操作,并且当你这样做时会引发异常。尝试使用您创建的新线程执行该代码。然后它应该工作。