gpt4 book ai didi

Android NSD onServiceFound() 没有被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:42:33 27 4
gpt4 key购买 nike

第一次尝试在 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);
}
}

最佳答案

来自 NdsManager documentation page :

The API currently supports DNS based service discovery and discovery is currently limited to a local network over Multicast DNS.

从这里Local networking limitations emulator docs page :

Currently, the emulator does not support IGMP or multicast.

希望对你有帮助

关于Android NSD onServiceFound() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36797008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com