我是一个新手试图在我正在构建的应用程序中使用NSD,以找到在同一个wifi网络上运行相同服务的对等端并连接到它。
[请参阅代码]我有一台运行 HostActivity.java 的设备和一台运行 GuestActivity.java 的设备。单击主机中的浮动按钮在端口9000上注册服务(现在预设),然后在Guest中单击它,开始运行服务发现。两者都连接到同一个wifi网络。
在我的logcat中,我发现 HostActivity.java 的注册工作正常 - 我从 NsdHelper.java 获取必要的日志消息。但是,运行 GuestActivity.java 只记录“服务发现已启动”,并且永远不会解析为网络上存在的服务。
我做错了什么?请帮忙。
相关代码 -
HostActivity.java
NsdHelper mNsdHelper;
public static final String TAG = "ABC";
private final int ABC_PORT = 9000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mNsdHelper = new NsdHelper(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Started Registration", Snackbar.LENGTH_LONG)
.setAction("CLOSE", null).show();
mNsdHelper.initializeNsd();
mNsdHelper.registerService(ABC_PORT);
Snackbar.make(view, "Completed Registration", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (mNsdHelper != null) {
mNsdHelper.initializeNsd();
mNsdHelper.registerService(ABC_PORT);
}
}
@Override
protected void onDestroy() {
mNsdHelper.tearDown();
super.onDestroy();
}
GuestActivity.java
NsdHelper mNsdHelper;
public static final String TAG = "ABC";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mNsdHelper = new NsdHelper(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Searching for available services", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mNsdHelper.initializeNsd();
mNsdHelper.discoverServices();
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if(mNsdHelper != null) {
mNsdHelper.initializeNsd();
mNsdHelper.discoverServices();
}
}
@Override
protected void onDestroy() {
mNsdHelper.tearDown();
super.onDestroy();
}
NsdHelper.java - 这包含所有NSD实现细节
public class NsdHelper {
Context mContext;
NsdManager mNsdManager;
NsdManager.ResolveListener mResolveListener;
NsdManager.DiscoveryListener mDiscoveryListener;
NsdManager.RegistrationListener mRegistrationListener;
private static final String SERVICE_TYPE = "_http._tcp.";
private static final String TAG = "NsdHelper";
private String mServiceName = "ABC";
// First step : register a NsdServiceInfo object to advertise your service
NsdServiceInfo mService;
public NsdHelper(Context context) {
mContext = context;
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void initializeNsd() {
initializeRegistrationListener();
initializeDiscoveryListener();
initializeResolveListener();
}
// Check success of service registration
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
mServiceName = nsdServiceInfo.getServiceName();
Log.d(TAG, "Registered name : " + mServiceName);
}
@Override
public void onRegistrationFailed(NsdServiceInfo nsdServiceInfo, int arg1) {
Log.d(TAG, "Registration Failed");
}
@Override
public void onServiceUnregistered(NsdServiceInfo nsdServiceInfo) {
Log.d(TAG, "Unregistered");
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
}
};
}
public void registerService(int port) {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setPort(port);
serviceInfo.setServiceName(mServiceName);
serviceInfo.setServiceType(SERVICE_TYPE);
mNsdManager.registerService(
serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
// To discover services of the type you're looking for on the network
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);
Log.d(TAG, "Host = "+ service.getServiceName());
Log.d(TAG, "port = " + String.valueOf(service.getPort()));
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;
Log.e(TAG, "service lost" + service);
}
}
@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 discoverServices() {
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
// Connection handshake
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;
int port = mService.getPort();
InetAddress host = mService.getHost();
}
};
}
public void stopDiscovery() {
if (mNsdManager != null) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
}
public NsdServiceInfo getChosenServiceInfo() {
return mService;
}
public void tearDown() {
if (mNsdManager != null) {
if (mRegistrationListener != null) {
mNsdManager.unregisterService(mRegistrationListener);
}
if (mDiscoveryListener != null) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
}
}
}
答案 0 :(得分:0)
您似乎在Android开发者网站上关注了代码示例,并且有两个错误阻止了我们:
public void onServiceFound(NsdServiceInfo service)
方法中,检查服务名称的代码不正确。根据我的理解,同一网络中的其他设备也可以使用相同的服务命名,因此请删除if (service.getServiceName().equals(mServiceName))
代码块。public void onServiceResolved(NsdServiceInfo serviceInfo)
代码块中。请删除if (serviceInfo.getServiceName().equals(mServiceName))
代码块。