JMDNS不会更新和/或检测可用服务

时间:2014-10-02 20:19:10

标签: java android jmdns

我正在尝试在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));


    }

    });

到目前为止,我遇到了几个问题:

  1. 其他设备的服务发现不一致。有时发现是有效的,有时则不然。有时候它会起作用,但会在一段时间后停止。
  2. 接收更新的txt记录信息也非常不一致。这是非常经常发生的。更新服务中TXT的过程如下:

    jmDNS.unregisterAllServices();
    jmDNS.removeServiceListener(SERVICE_TYPE, mdnsServiceListener);
    
  3. 然后我重新开始注册过程。问题是其他设备使用serviceResolved检测到新服务,但txt记录仍然过时。

0 个答案:

没有答案