如何在Android上实现像“Printer Discover”这样的服务发现?

时间:2010-10-12 11:36:42

标签: android printing service discovery

我想知道android支持的'服务发现'机制 - 特别是Printer Discovery。

android是否提供了这样的发现选项?例如:支持snmp广播?

我尝试了一个应用程序“PrinterShare”链接:http://www.printeranywhere.com/mobile.sdf通过ipp实现打印机发现。

感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

Roy,我遇到了和你一样的问题,甚至在实际设备上运行代码片段时也会遇到相同的行为(同时独立运行代码,而不是在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);
}

希望有所帮助:)

答案 1 :(得分:3)

  

android是否提供了这样的发现选项?

不是我知道,抱歉。

答案 2 :(得分:2)

此代码段在J2SE上运行正常。但是,在Android模拟器上,我得到一个'Time Out Exception',其中response ='null'

`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(); `