我遇到了这个问题,无法解决。我正在使用NSD Android API查找网络上的多个服务。问题是,如果网络中只有一台具有该服务类型的设备,我可以检测到它并解决它,但是如果有不止一次,我可以检测到它,但是没有一个可以解决。
我在互联网上读到一些人也遇到过同样的问题,他们建议每次尝试解析服务时都创建新的onResolveListener。我也为DiscoveryListener做过,但似乎无法解决多个服务问题。
如果您有任何建议或示例,请提前谢谢!
public class MainActivity extends AppCompatActivity {
private String SERVICE_NAME = "printer";
private String SERVICE_TYPE = "_print._tcp.";
private InetAddress hostAddress;
private int hostPort;
private NsdManager mNsdManager;
NsdServiceInfo mService;
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting toolbar by id
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//disabling default title text
getSupportActionBar().setDisplayShowTitleEnabled(false);
mWebView = findViewById(R.id.activity_main_webview);
//Enable JavasScript for webView
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
//mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
if (Build.VERSION.SDK_INT >= 19) {
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
//NSD stuff
mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack() && mWebView.isFocused()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
NsdManager.DiscoveryListener mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e("TAG", "DiscoveryFailed: Error code: " + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e("TAG", "Discovery failed : Error code: " + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
@Override
public void onDiscoveryStarted(String regType) {
Log.d("TAG", "Service discovery started");
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i("TAG", "Discovery stopped: " + serviceType);
}
@Override
public void onServiceFound(final NsdServiceInfo serviceInfo) {
Log.d("TAG", "Service discovery success : " + serviceInfo);
Log.d("TAG", "Host = " + serviceInfo.getServiceName());
Log.d("TAG", "Port = " + serviceInfo.getPort());
if (serviceInfo.getServiceType().equals(SERVICE_TYPE)) {
Log.d("TAG", "Unknown Service Type: " + serviceInfo.getServiceType());
mNsdManager.resolveService(serviceInfo, new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int errorCode) {
Log.e("TAG", "Resolve failed: " + nsdServiceInfo);
}
@Override
public void onServiceResolved(NsdServiceInfo nsdServiceInfo) {
final String nsdServiceInfoName = nsdServiceInfo.getServiceName();
final TextView myTextView = findViewById(R.id.serviceName);
/*
CountDownLatch latch = new CountDownLatch(1);
mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD,mDiscoveryListener);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
mNsdManager.discoverServices(SERVICE_TYPE,NsdManager.PROTOCOL_DNS_SD,mDiscoveryListener);
latch.countDown();
*/
runOnUiThread(new Runnable() {
@Override
public void run() {
myTextView.setText(nsdServiceInfoName);
myTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(MainActivity.this, "im printer",Toast.LENGTH_SHORT).show();
mWebView.loadUrl("http://10.0.0.115");
}
});
}
});
Log.d("TAG", "Resolve Succeeded " + nsdServiceInfo);
if (nsdServiceInfo.getServiceType().equals(SERVICE_TYPE)) {
Log.d("TAG", "Same IP");
return;
}
hostPort = nsdServiceInfo.getPort();
hostAddress = nsdServiceInfo.getHost();
}
});
}
}
@Override
public void onServiceLost(NsdServiceInfo nsdServiceInfo) {
Log.d("TAG", "Service lost " + nsdServiceInfo);
}
};
}
我得到的错误是“解决失败,服务名称”