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

发布于 2024-09-27 13:45:46 字数 294 浏览 5 评论 0原文

我想了解 android 支持的“服务发现”机制 - 特别是打印机发现。

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

我尝试了应用程序“PrinterShare”链接:http://www.printeranywhere.com/mobile.sdf 其中 Printer Discovery 是通过 ipp 实现的。

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何时共饮酒 2024-10-04 13:45:46

罗伊,我遇到了和你一样的问题,甚至在实际设备上运行该代码片段时也得到了相同的行为(独立运行代码时,而不是在 Android 中,工作得很好)。我找到了此页面并使其正常工作,尽管仅在设备上,通过使用以下是找出广播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);
}

希望有帮助:)

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:)

眼藏柔 2024-10-04 13:45:46

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

据我所知,抱歉。

Does android provide such a discovery option?

Not that I am aware of, sorry.

荒芜了季节 2024-10-04 13:45:46

此代码片段在 J2SE 上运行良好。但是,在 Android 模拟器上,我收到“超时异常”,响应 = '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(); `

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(); `
鲜肉鲜肉永远不皱 2024-10-04 13:45:46

对于在 .NET 中评估 SSDP,此库可能很有用

https://yortw.github.io/RSSDP/< /a>

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文