Android NSD无法发现所有服务 [英] Android NSD not discovering all services
问题描述
我试图使用Android本机服务发现来运行应用程序,但是有时当我运行该应用程序时,它无法发现我网络中的所有服务.我正在使用四个星系关系从 https://github.com/joeluchoa/nsd 中运行代码,大多数情况下是每次他们中的同一时间发现了不同数量的服务.
I'm trying to run an application using Android Native Service Discovery but sometimes when I run the application, it doesn't discover all services from my network. I'm running the code from https://github.com/joeluchoa/nsd using four galaxy nexus and most of the times each of them discoveries different number of services at the same time.
基本上,我使用ServerSocket运行服务:
Basically I run a service with a ServerSocket:
ServerSocket server = new ServerSocket(0);
Log.i(TAG, "IP " + server.getInetAddress()
+ ", running on port " + server.getLocalPort());
Intent intent = new Intent(MySocket.this,
MyPresence.class);
intent.putExtra("PORT", server.getLocalPort());
startService(intent);
然后我使用NsdManager中的registerService方法发布它:
Then I publish it using the method registerService from NsdManager:
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(Build.SERIAL + "-" + new Date().getTime());
serviceInfo.setServiceType(SERVICE_TYPE);
serviceInfo.setPort(port);
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD,
mRegistrationListener);
要发现服务,我使用NsdManager中的discoverServices方法:
To discover the services I use the method discoverServices from NsdManager:
mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD,
mDiscoveryListener);
使用mDiscoveryListener如下:
With mDiscoveryListener as follows:
mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
@Override
public void onServiceFound(NsdServiceInfo service) {
Log.d(TAG, "Service discovery success");
Log.d(TAG, String.format("%s %s %s %d",
service.getServiceName(), service.getServiceType(),
service.getHost(), service.getPort()));
if (!service.getServiceType().contains(SERVICE_TYPE)) {
Log.d(TAG,
"Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same machine: " + mServiceName);
} else {
mNsdManager.resolveService(service, mResolveListener);
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost" + service);
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, serviceType + " Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, serviceType + " Discovery failed: Error code:"
+ errorCode);
mNsdManager.stopServiceDiscovery(this);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, serviceType + " Discovery failed: Error code:"
+ errorCode);
mNsdManager.stopServiceDiscovery(this);
}
};
我做错什么了吗?有人知道解决方案或解决方法吗?
Am i doing something wrong? Does anybody know a solution or a workaround for this?
推荐答案
要发现已连接网络的所有服务,只需更改要发现的服务类型,
To discover all services of the connected network just change the service type you are discovering,
public static final String SERVICE_TYPE = "_services._dns-sd._udp";
这篇关于Android NSD无法发现所有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!