使用网络服务发现时注册失败,错误代码为0



每当我使用registerService函数时,它都会给出一个异常,代码错误为0。1) 我正在创建一个本地聊天服务应用程序,在那里我需要本地服务广播,在这样做的时候,我一直被卡住,因为无论我做什么,注册总是失败,错误代码为0。我在onResume()或onPause()块中没有多个注册实例。2) Logcat只显示

试图完成输入事件,但输入事件接收器已被释放。

每当我点击右上角的注册服务按钮时。

这是我的代码:

public String mServiceName = "nearByDevices";
NsdServiceInfo mService;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
   public void registerService(int port) {
    // Create the NsdServiceInfo object, and populate it.
         // Cancel any previous registration request
       initializeRegistrationListener();
       NsdServiceInfo serviceInfo  = new NsdServiceInfo();
       serviceInfo.setServiceName(mServiceName);
       serviceInfo.setServiceType(SERVICE_TYPE);
    serviceInfo.setPort(port);
       NsdHelper(this);
       mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
       mNsdManager.registerService(serviceInfo,NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);

}
public void NsdHelper(Context context) {
    mContext = context;
    mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}

public void initializeRegistrationListener() {
    mRegistrationListener = new NsdManager.RegistrationListener() {
        @Override
        public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
            mServiceName = NsdServiceInfo.getServiceName();
            Toast.makeText(MainActivity.this, "Service registered:" + mServiceName, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
            Toast.makeText(MainActivity.this, "Service registration failed:" + arg1, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onServiceUnregistered(NsdServiceInfo arg0) {
            Toast.makeText(MainActivity.this, "Service unregistered:" + arg0.getServiceName(), Toast.LENGTH_LONG).show();
        }
        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Toast.makeText(MainActivity.this, "Service unregistration failed:" + errorCode, Toast.LENGTH_LONG).show();
        }
    };
}
public void initializeDiscoveryListener() {
    // Instantiate a new DiscoveryListener
    mDiscoveryListener = new NsdManager.DiscoveryListener() {
        //  Called as soon as service discovery begins.
        @Override
        public void onDiscoveryStarted(String regType) {
            Toast.makeText(MainActivity.this, "Service Discovery Started", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onServiceFound(NsdServiceInfo service) {
            // A service was found!  Do something with it.
            Toast.makeText(MainActivity.this, "Service Discovery Success:" + service.getServiceName(), Toast.LENGTH_LONG).show();
            if (!service.getServiceType().equals(SERVICE_TYPE)) {
                // Service type is the string containing the protocol and
                // transport layer for this service.
                Toast.makeText(MainActivity.this, "Unknown Service Type" + service.getServiceType(), Toast.LENGTH_LONG).show();
            } else if (service.getServiceName().equals(mServiceName)) {
                Toast.makeText(MainActivity.this, "Same Machine" + mServiceName, Toast.LENGTH_LONG).show();
            } else {
                devices.add((service.getServiceName()).toString());                }
        }
        @Override
        public void onServiceLost(NsdServiceInfo service) {
            Toast.makeText(MainActivity.this, "Service Lost:" + service.getServiceName(), Toast.LENGTH_LONG).show();
            if (mService == service) {
                mService = null;
            }
        }
        @Override
        public void onDiscoveryStopped(String serviceType) {
            Toast.makeText(MainActivity.this, "Discovery Stopped" + serviceType, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            Toast.makeText(MainActivity.this, "Discovery failed. Error Code" + errorCode, Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Toast.makeText(MainActivity.this, "Discovery failed. Error Code" + errorCode, Toast.LENGTH_LONG).show();
        }
    };
}
        public void initializeResolveListener() {
            mResolveListener = new NsdManager.ResolveListener() {
                @Override
                public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
                    // Called when the resolve fails.  Use the error code to debug.
                    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 discoverServices() {
    stopDiscovery();  // Cancel any existing discovery request
    initializeDiscoveryListener();
    mNsdManager.discoverServices(
            SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
public void stopDiscovery() {
    if (mDiscoveryListener != null) {
        try {
            mNsdManager.stopServiceDiscovery(mDiscoveryListener);
        } finally {
        }
        mDiscoveryListener = null;
    }
} public NsdServiceInfo getChosenServiceInfo() {
    return mService;
}
public void tearDown() {
    if (mRegistrationListener != null) {
        try {
            mNsdManager.unregisterService(mRegistrationListener);
        } finally {
        }
        mRegistrationListener = null;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //initializeServerSocket();
    devices = new ArrayList<String>();

    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.main_listview, devices);
    ListView listView = (ListView) findViewById(R.id.device_list);
    if(devices.isEmpty()) {
    devices.add("No Devices Nearby");
        listView.setAdapter(adapter);
    }
    else
    listView.setAdapter(adapter);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    switch (id) {
        case R.id.register:
           // initializeServerSocket();
            registerService(65010);
            return true;
        case R.id.discover:
            discoverServices();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}
@Override
public void onStart() {
    super.onStart();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.devesh1.mynewapp/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
    super.onStop();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.devesh1.mynewapp/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}
}

我在尝试使用NsdManager注册服务时也遇到了这个问题。最后,我发现服务类型必须遵循NsdManager文档中示例所述的格式。为了澄清,格式为:"_name_communicationprotocol",例如:"_ipp._tcp"one_answers"_http._tcp"。如果没有,将导致回调到onRegistrationFailed,错误代码=0

顺便说一句。NsdManager的调试日志不会输出到Logcat,因为它来自您的应用程序。您必须查看带有"无过滤器"的日志,才能查看NSD服务的调试信息

更新:刚刚再次遇到此错误。正在搜索_name._tcp.local,但我应该只搜索_name._tcp

相关内容

最新更新