在使用示例和NSDManager实现NSDManager的第一个实现过程中,应用程序成功地启动了发现并找到了设备。
,但是现在它似乎坏了.
程序启动后,在进行一些初始化之后,代码将输入以下方法并成功运行:
public void discoverServices() {
Log.d(TAG, "Initializing discovery on NSD");
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
} 接收到日志消息。一段时间后(比如说大约5分钟)这是从程序中输出的:
05-21 11:08:32.518: E/NsdCamera(12236): Discovery failed: Error code:0
05-21 11:08:32.518: W/dalvikvm(12236): threadid=12: thread exiting with uncaught exception (group=0x40c9c930)
05-21 11:08:32.518: E/AndroidRuntime(12236): FATAL EXCEPTION: NsdManager
05-21 11:08:32.518: E/AndroidRuntime(12236): java.lang.NullPointerException
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.net.nsd.NsdManager$ServiceHandler.handleMessage(NsdManager.java:338)
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Looper.loop(Looper.java:137)
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.HandlerThread.run(HandlerThread.java:60)也来自服务:
05-21 11:50:49.108: E/NativeDaemonConnector.ResponseQueue(8858): Timeout waiting for response
05-21 11:50:49.108: E/mDnsConnector(8858): timed-out waiting for response to 10 mdnssd discover 6 _http._tcp.
05-21 11:50:49.108: E/NsdService(8858): Failed to discoverServices com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '10 mdnssd discover 6 _http._tcp.' failed with 'null'错误代码"0“在NSDManager类中描述为内部错误。我所做的主要更新是访问名为NsdCamera的助手类中的上下文。下面是一些可能是邪恶的代码片段:
帮助类构造函数:
public NsdCamera(CameraChooseActivity context) {
mContext = context;
updateUI = new UpdateUI();
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
mServiceName = new Vector<NsdServiceInfo>();帮助类NSD初始化:
public void initializeNsd() {
initializeDiscoveryListener();
}
public void initializeDiscoveryListener() {
mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
/**
* A name check to see if the DNS discovery was correct. Checks if it contains
* AXIS and has the desired MAC address-space
* @param hostname ,the inputted hostname from the discovery cycle
* @return true if it's an Axis camera.
*/
public boolean nameCheck(String hostname){
return (hostname.contains("AXIS") && hostname.contains("00408C"));
}
@Override
public void onServiceFound(NsdServiceInfo service) {
Log.d(TAG, "Service discovery success: " + service.getServiceName());
if (!service.getServiceType().equals(SERVICE_TYPE)) {
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (nameCheck(service.getServiceName())){
mServiceName.add(service);
// updateUI.execute(new BundleUI(mContext,service, null));
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost" + service);
if(mServiceName.remove(service)){
//TODO
Log.e(TAG, "remove the view, service is lost");
}
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
//Necessary??
mServiceName.clear();
}
@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);
}
};
}CameraChooseActivity -> onCreate正在调用助手类。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camerachoose);
//Setup the animation for the text in the Relativelayout
mDescription = (TextSwitcher) findViewById(R.id.camera_add);
mDescription.setFactory(this);
mDescription.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mDescription.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
mDescription.setText(getText(R.string.camera_add));
//Building alert dialog
mBuilder = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_DARK);
mBuilder.setMessage(R.string.dialog_about).setTitle(R.string.action_about);
mBuilder.setIcon(android.R.drawable.ic_dialog_info);
mLayout = (RelativeLayout) findViewById(R.id.layout_camerachoose);
//Initialize the NSD
mNSDHelper = new NsdCamera(this);
mNSDHelper.initializeNsd();发布于 2013-09-26 16:00:25
根据我的经验,我认为这是一个听众终生的问题。
因为您向系统NSD服务提供了两个侦听器,一个用于startServiceDiscovery(),另一个用于stopServiceDiscovery()。当系统访问这些监听器时,您需要确保这些监听器仍然处于活动状态。
一个事实是,onStartDiscoveryFailed()在调用startServiceDiscovery()后2分钟被调用,与侦听器的生命周期相比,应该是很长的时间。
因此,如果侦听器是一个本地对象,并且在调用startServiceDiscovery()之后释放,它可能会导致NSD服务崩溃。
公共空stopServiceDiscovery (NsdManager.DiscoveryListener侦听器) 停止使用discoverServices()启动的服务发现。使用onDiscoveryStarted(String)通知应用程序活动服务发现,它将保持活动状态,直到应用程序调用停止服务发现。通过调用onDiscoveryStopped(String)通知成功停止。 如果未能停止服务发现,应用程序将通过onStopDiscoveryFailed(String,int)发出通知。 参数侦听器这应该是传递给discoverServices(String,int,NsdManager.DiscoveryListener)的监听器对象。它标识应该停止的发现,并通知成功的停止。
下面的代码片段确保不调用任何NsdManager api。
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.i(TAG, "onStartDiscoveryFailed : Error code:" + errorCode);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.i(TAG, "onStopDiscoveryFailed : Error code:" + errorCode);
}祝好运。
发布于 2013-05-21 12:57:29
DUT的简单重新启动被证明是解决方案。必须说这个错误很奇怪。我认为守护进程崩溃了,没有重新启动。
(如果有人可以发布分析或者有更好的解决方案,请张贴)
https://stackoverflow.com/questions/16666604
复制相似问题