如何在android上实现类似“Printer Discover”的服务发现?
我想知道android支持的'服务发现'机制 - 特别是Printer Discovery。如何在android上实现类似“Printer Discover”的服务发现?
Android提供了这样的发现选项吗?例如:支持snmp广播?
我尝试了一个应用程序“PrinterShare”链接:http://www.printeranywhere.com/mobile.sdf其中打印机发现是通过ipp实现的。
任何帮助表示赞赏。
Android是否提供了这样的发现选项?
不是说我知道,对不起。
此代码片段在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(); `
任何想法,为什么它不能在android模拟器上工作? – 2010-10-13 10:36:50
Android模拟器不支持广播/多播 - 需要在真实的Android硬件上运行。 – 2014-03-28 16:21:24
罗伊,我碰到了同样的问题来了,你和运行代码片段时在实际设备上有人甚至得到同样的行为(同时运行的代码独立,不会在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);
}
希望帮助:)
感谢快速回复......顺便说一句......你是怎么得到这样一个巨大的声誉得分? *只是想知道* – 2010-10-13 02:35:56
@罗伊塞缪尔:“你是如何得到像这样的大声誉得分??? !!!” - 一次一个答案。随着“没有生命”的副命令...... :-) – CommonsWare 2010-10-13 11:24:57