NSD Android - 网络发现
NSD Android - Network discovery
您好,我是 Android 开发的新手。我创建了一个 NSD 实用程序 class。目前它无法调用 DiscoveryListner 方法。任何帮助都会很棒
主要活动
package com.example.android.implicitintents;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.android.implicitintents.Utils.NsdUtils;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "NsdChat";
public static String URL_WEB;
NsdUtils mNsdUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NsdUtils mNsdUtils = new NsdUtils(this);
mNsdUtils.initializeNsd();
}
public void onClickOpenWebpageButton(View view) {
String urlAsString = "http://192.168.10.1";
openWebPage(urlAsString);
}
private void openWebPage(String url) {
Intent intent = new Intent(this, ConfigActivity.class);
intent.putExtra(URL_WEB, url);
Log.d(TAG, "Button Pressed");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
NSD 实用工具 Class
package com.example.android.implicitintents.Utils;
import android.content.Context;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.NsdManager;
import android.util.Log;
public class NsdUtils {
Context mContext;
NsdManager mNsdManager;
NsdManager.ResolveListener mResolveListener;
NsdManager.DiscoveryListener mDiscoveryListener;
public static final String SERVICE_TYPE = "http";
public static final String TAG = "NsdHelper";
public String mServiceName = "Plug_Service";
NsdServiceInfo mService;
public NsdUtils(Context context) {
mContext = context;
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void initializeNsd() {
initializeResolveListener();
initializeDiscoveryListener();
}
public void initializeDiscoveryListener() {
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" + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same machine: " + mServiceName);
} else if (service.getServiceName().contains(mServiceName)){
mNsdManager.resolveService(service, mResolveListener);
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost" + service);
if (mService == service) {
mService = null;
}
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: 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);
}
};
}
public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "Resolve failed" + errorCode);
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mService = serviceInfo;
}
};
}
public void discoverServices() {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
}
我尝试在 onclick 按钮事件中调用 dicoverService 失败。我们需要创建异步任务还是新线程?
没有必要 运行 在另一个线程中使用此功能。
经过一些调查,当将 String SERVICE_TYPE = "http";
更改为 public static final String SERVICE_TYPE = "_name._tcp";
时,当前功能会停止崩溃并开始发现
您可以发现 tcp 服务,然后当您获得 IP 时,只需使用这些 IP 地址通过 http 连接到 Web 服务器即可。
首先指定要查找的TCP网络类型。然后制作class的对象,调用Intilization,然后调用NSD的discovery函数。
它应该工作得很好。
您好,我是 Android 开发的新手。我创建了一个 NSD 实用程序 class。目前它无法调用 DiscoveryListner 方法。任何帮助都会很棒
主要活动
package com.example.android.implicitintents;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.example.android.implicitintents.Utils.NsdUtils;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "NsdChat";
public static String URL_WEB;
NsdUtils mNsdUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NsdUtils mNsdUtils = new NsdUtils(this);
mNsdUtils.initializeNsd();
}
public void onClickOpenWebpageButton(View view) {
String urlAsString = "http://192.168.10.1";
openWebPage(urlAsString);
}
private void openWebPage(String url) {
Intent intent = new Intent(this, ConfigActivity.class);
intent.putExtra(URL_WEB, url);
Log.d(TAG, "Button Pressed");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
NSD 实用工具 Class
package com.example.android.implicitintents.Utils;
import android.content.Context;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.NsdManager;
import android.util.Log;
public class NsdUtils {
Context mContext;
NsdManager mNsdManager;
NsdManager.ResolveListener mResolveListener;
NsdManager.DiscoveryListener mDiscoveryListener;
public static final String SERVICE_TYPE = "http";
public static final String TAG = "NsdHelper";
public String mServiceName = "Plug_Service";
NsdServiceInfo mService;
public NsdUtils(Context context) {
mContext = context;
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void initializeNsd() {
initializeResolveListener();
initializeDiscoveryListener();
}
public void initializeDiscoveryListener() {
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" + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same machine: " + mServiceName);
} else if (service.getServiceName().contains(mServiceName)){
mNsdManager.resolveService(service, mResolveListener);
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost" + service);
if (mService == service) {
mService = null;
}
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: 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);
}
};
}
public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "Resolve failed" + errorCode);
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mService = serviceInfo;
}
};
}
public void discoverServices() {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
}
我尝试在 onclick 按钮事件中调用 dicoverService 失败。我们需要创建异步任务还是新线程?
没有必要 运行 在另一个线程中使用此功能。
经过一些调查,当将 String SERVICE_TYPE = "http";
更改为 public static final String SERVICE_TYPE = "_name._tcp";
您可以发现 tcp 服务,然后当您获得 IP 时,只需使用这些 IP 地址通过 http 连接到 Web 服务器即可。
首先指定要查找的TCP网络类型。然后制作class的对象,调用Intilization,然后调用NSD的discovery函数。
它应该工作得很好。