第一次尝试在

Android中进行IP发现.我使用了

http://developer.android.com/training/connect-devices-wirelessly/nsd.html#discover并编写了代码.我没有注册设备,只是在网络中发现服务.当我在模拟器或设备中运行项目时,会调用onDiscoveryStarted(),但永远不会调用onServiceFound().请在下面找到我的代码.任何输入都非常感谢.谢谢!

public class MainActivity extends AppCompatActivity {

private Button discoverButton;

Context mContext;

NsdManager mNsdManager;

NsdManager.ResolveListener mResolveListener;

NsdManager.DiscoveryListener mDiscoveryListener;

NsdManager.RegistrationListener mRegistrationListener;

public static final String SERVICE_TYPE = "_http._tcp.";

public static final String TAG = "MyApp_MAIN_CLIENT";

public String mServiceName = "MyApp";

/*

* public static final String SERVICE_TYPE = "_http._tcp.";

public static final String TAG = "NsdHelper";

public String mServiceName = "NsdChat";

* */

NsdServiceInfo mService;

private Handler mUpdateHandler;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mNsdManager = (NsdManager) this.getSystemService(Context.NSD_SERVICE);

discoverButton = (Button) findViewById(R.id.netButton);

discoverButton.setOnClickListener(new View.OnClickListener() {

public void onClick(android.view.View v) {

initializeDiscoveryListener();

initializeResolveListener();

discoverServices();

}

});

}

public void discoverServices() {

mNsdManager.discoverServices(

SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);

}

public void initializeDiscoveryListener() {

// Instantiate a new DiscoveryListener

mDiscoveryListener = new NsdManager.DiscoveryListener() {

// Called as soon as service discovery begins.

@Override

public void onDiscoveryStarted(String regType) {

Log.d(TAG, "Service discovery started");

}

@Override

public void onServiceFound(NsdServiceInfo service) {

// A service was found! Do something with it.

Log.d(TAG, "Service discovery success" + service);

if (!service.getServiceType().equals(SERVICE_TYPE)) {

// Service type is the string containing the protocol and

// transport layer for this service.

Log.d(TAG, "Unknown Service Type: " + service.getServiceType());

} /*else if (service.getServiceName().equals(mServiceName)) {

// The name of the service tells the user what they'd be

// connecting to. It could be "Bob's Chat App".

Log.d(TAG, "Same machine: " + mServiceName);

}

//else if (service.getServiceName().contains("NsdChat")){*/

else{

mNsdManager.resolveService(service, mResolveListener);

}

}

@Override

public void onServiceLost(NsdServiceInfo service) {

// When the network service is no longer available.

// Internal bookkeeping code goes here.

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);

}

};

}// end of initializeListener()

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();

Log.d(TAG,host.toString());

}

};

}//end of initializeResolveListener

@Override

protected void onPause() {

super.onPause();

stopDiscovery();

tearDown();

}

@Override

protected void onResume() {

super.onResume();

discoverServices();

}

@Override

protected void onDestroy() {

tearDown();

super.onDestroy();

}

public void stopDiscovery() {

mNsdManager.stopServiceDiscovery(mDiscoveryListener);

}

public void tearDown() {

mNsdManager.unregisterService(mRegistrationListener);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐