我正在尝试在Android应用中实施JMDNS。这是相关的代码:
...
jmDNS = JmDNS.create(myaddr);
HashMap props= new HashMap();
props.put(PROP_USER_NAME,userName);
props.put(PROP_USER_ID,userId);
ServiceInfo serviceInfo = ServiceInfo.create(SERVICE_TYPE,originalServiceName,port,0,0,true,props);
jmDNS.registerService(serviceInfo);
jmDNS.addServiceListener(SERVICE_TYPE, mdnsServiceListener = new ServiceListener() {
public void serviceAdded (ServiceEvent serviceEvent){
jmDNS.requestServiceInfo(serviceEvent.getType(), serviceEvent.getName(), true);
}
public void serviceRemoved (ServiceEvent serviceEvent){
Log.i(TAG, "A service was removed");
//Log.i(TAG,"NAME "+serviceEvent.getInfo().getPropertyString(PROP_USER_ID) + serviceEvent.getInfo().getPropertyString(PROP_USER_NAME));
// Test service is disappeared.
}
public void serviceResolved (ServiceEvent serviceEvent){
// Test service info is resolved.
Log.i(TAG, "A service was resolved");
ServiceInfo serviceInfo = serviceEvent.getInfo();
Log.i(TAG, "NAME" + serviceInfo.getName());
Log.i(TAG, "user_name" + serviceInfo.getPropertyString(PROP_USER_NAME));
}
});
到目前为止,我遇到了几个问题:
接收更新的txt记录信息也非常不一致。这是非常经常发生的。更新服务中TXT的过程如下:
jmDNS.unregisterAllServices();
jmDNS.removeServiceListener(SERVICE_TYPE, mdnsServiceListener);
然后我重新开始注册过程。问题是其他设备使用serviceResolved检测到新服务,但txt记录仍然过时。