Listener already in use (Service Discovery)

后端 未结 3 2062
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 06:07

I can\'t resolve one service while another is being resolved? If that\'s what the error means... What is the way to wait until it is resolved?

@Override
public v         


        
3条回答
  •  孤独总比滥情好
    2021-02-04 06:18

    I had this problem as well, and had been following the Android NsdHelper implementation laid out in NsdChat here. This example shows creating a single NsdManager.ResolveListener mResolveListener in the NsdHelper class, and using that ResolveListener for all calls to NsdManager.resolveService.

    From here I read that "a separate Listener is to be used for each active registration or discovery request".

    So instead of using a class variable mResolveListener, create a new listener every time you call mNsdManager.resolveService:

    @Override
    public void onServiceFound(NsdServiceInfo serviceInfo) {
        Log.d(TAG, "Service found: "+ serviceInfo);
        if (serviceInfo.getServiceType().equals(SERVICE_TYPE)){
            mNsdManager.resolveService(serviceInfo, new NsdManager.ResolveListener() {
                @Override
                public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
                    Log.e(TAG, "Resolve Failed: " + serviceInfo);
                }
                @Override
                public void onServiceResolved(NsdServiceInfo serviceInfo) {
                    Log.i(TAG, "Service Resolved: " + serviceInfo);
                }
            });
        }
    }
    

提交回复
热议问题