有人可以解释可以传递给回调的错误消息,即
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode)
我以前见过错误代码3,但不知道它意味着什么.文档似乎很好看......
干杯,
我通过再次调用resolveService来解决这个问题.它可能会连续几次失败,但最终会解决.
@Override public void onServiceFound(NsdServiceInfo serviceInfo) { Log.d(TAG, "Service found: "+ serviceInfo); if (!serviceInfo.getServiceType().equals(SERVICE_TYPE)){ Log.d(TAG, "Unknown service type: " + serviceInfo.getServiceType()); } else if (serviceInfo.getServiceName().equals(mServiceName)){ Log.d(TAG, "Same machine"); } else { startResolveService(serviceInfo); } }
而不是从onServiceFound中调用resolveService,我调用它的单独声明:
private void startResolveService(NsdServiceInfo serviceInfo){ NsdManager.ResolveListener newResolveListener = new NsdManager.ResolveListener() { @Override public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { Log.e(TAG, "Resolve Failed: " + serviceInfo + "\tError Code: " + errorCode); switch (errorCode) { case NsdManager.FAILURE_ALREADY_ACTIVE: Log.e(TAG, "FAILURE_ALREADY_ACTIVE"); // Just try again... startResolveService(serviceInfo); break; case NsdManager.FAILURE_INTERNAL_ERROR: Log.e(TAG, "FAILURE_INTERNAL_ERROR"); break; case NsdManager.FAILURE_MAX_LIMIT: Log.e(TAG, "FAILURE_MAX_LIMIT"); break; } } @Override public void onServiceResolved(NsdServiceInfo serviceInfo) { Log.i(TAG, "Service Resolved: " + serviceInfo); mLocatedServices.add(serviceInfo); reportNewService(); } }; mNsdManager.resolveService(serviceInfo, newResolveListener); }
当几乎立即发现多个服务时,第一个服务得到解决,然后您获得FAILURE_ALREADY_ACTIVE一次或两次,然后下一个服务得到解决,依此类推.