2010-10-12 205 views

回答

3

Android是否提供了這樣的發現選項?

不是說我知道,對不起。

+0

感謝快速回復......順便說一句......你是怎麼得到這樣一個巨大的聲譽得分? *只是想知道* – 2010-10-13 02:35:56

+2

@羅伊塞繆爾:「你是如何得到像這樣的大聲譽得分??? !!!」 - 一次一個答案。隨着「沒有生命」的副命令...... :-) – CommonsWare 2010-10-13 11:24:57

2

此代碼片段在J2SE上正常工作。但是,在Android模擬器上,我收到'Time Out Exception'(響應='空')

`DatagramSocket clientSocket = new DatagramSocket(8888); clientSocket.setSoTimeout(20000);

/** 
* SSDP is a text-based protocol based on the Hypertext Transfer Protocol (RFC 2616). 
* However, it uses the User Datagram Protocol (UDP) as underlying transport protocol. 
* Services are announced by the hosting system with multicast addressing to a 
* specifically designated IP multicast address at port number 1900. In IPv4, 
* the multicast address is 239.255.255.250. 
*/ 
         //getByName(host) //host the hostName to be resolved to an address or null. 
InetAddress group = InetAddress.getByName("239.255.255.250"); 

//host can be null which means that an address of the loopback interface is returned. 
if(group == null){ 
    Log.d("Discovery","getByName(): returns address of loopback interface."); 
} 
byte[] sendData; 
byte[] receiveData = new byte[128]; 

String sentence = "M-SEARCH * HTTP/1.1\r\n" 
    + "HOST: 239.255.255.250:1900\r\n" 
    + "MAN: \"ssdp:discover\"\r\n" 
    + "MX: 10\r\n" 
    + "ST: ssdp:all\r\n" 
    + "\r\n"; 

sendData = sentence.getBytes(); 
//public DatagramPacket (byte[] data, int length, InetAddress host, int port) 
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, group, 1900); 

try { 
    clientSocket.send(sendPacket); 
} catch (Exception e) { 
    e.getMessage(); 
    e.printStackTrace(); 
} 
Log.d("Discovery","sent packet..."); 
while(true) 
{ 
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
    try 
    { 
     boolean isc = clientSocket.isConnected(); 
     clientSocket.receive(receivePacket); 
    } 
    catch (Exception Ex) 
    { 
     Log.d("Discovery","Time out Exception"); 
    } 
    if (receivePacket.getAddress() == null) 
    { 
     Log.d("Discovery","receivePacket.getAddress() == null"); 
     break; 
    } 
    Log.d("Discovery","Senders Address : " + receivePacket.getAddress().getHostAddress()); 
    String controllerResponse = new String(receivePacket.getData());  
} //end of while() 
clientSocket.close(); ` 
+0

任何想法,爲什麼它不能在android模擬器上工作? – 2010-10-13 10:36:50

+0

Android模擬器不支持廣播/多播 - 需要在真實的Android硬件上運行。 – 2014-03-28 16:21:24

4

羅伊,我碰到了同樣的問題來了,你和運行代碼片段時在實際設備上有人甚至得到同樣的行爲(同時運行的代碼獨立,不會在Android中,工作得很好)。我發現this page,得到它的工作,雖然只是在設備上,通過使用下面的找出廣播IP(而不是239.255.255.250):

InetAddress getBroadcastAddress() throws IOException { 
    WifiManager wifi = mContext.getSystemService(Context.WIFI_SERVICE); 
    DhcpInfo dhcp = wifi.getDhcpInfo(); 
    // handle null somehow 

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; 
    byte[] quads = new byte[4]; 
    for (int k = 0; k < 4; k++) 
     quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); 
    return InetAddress.getByAddress(quads); 
} 

希望幫助:)