3

I would like to know about the 'service discovery' mechanisms supported by android - particularly, Printer Discovery.

Does android provide such a discovery option? example : support for snmp broadcast?

I tried out an application "PrinterShare" link : http://www.printeranywhere.com/mobile.sdf where Printer Discovery is achieved through ipp.

Any help is appreciated.

Roy Samuel
  • 790
  • 9
  • 24

4 Answers4

4

Roy, I came across the same problem as you, and was even getting the same behavior when running that code snippet on an actual device (while running the code standalone, not in android, worked fine). I found this page and got it working, although only on the device, by using the following to figure out the Broadcast IP (instead of 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);
}

Hope that helps:)

atomjack
  • 41
  • 1
  • 3
3

Does android provide such a discovery option?

Not that I am aware of, sorry.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    thanks for the quick reply... btw... how did you get a massive reputation score like that???!!! *just wondering* – Roy Samuel Oct 13 '10 at 02:35
  • 3
    @Roy Samuel: "how did you get a massive reputation score like that???!!!" -- one answer at a time. With a side order of "having no life"... :-) – CommonsWare Oct 13 '10 at 11:24
2

This code snippet works fine on J2SE. However, on the Android emulator, I get a 'Time Out Exception' with 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(); `
Roy Samuel
  • 790
  • 9
  • 24
0

For evaluation of SSDP in .NET this library may be useful

https://yortw.github.io/RSSDP/

flodis
  • 1,123
  • 13
  • 9