1. Download our Official Android App: Forums for Android!

Apps Android Network Service Discovery

Discussion in 'Android Development' started by Wuffle, Mar 20, 2016.

  1. Wuffle

    Wuffle Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Mar 19, 2016

    Mar 19, 2016
    1
    0
    5
    Male
    I am using androids built in Network Service Discovery to find device B from device A however it doesn't discover the service from A but finds everything else printers etc.

    The Register service triggers the call back informing me the service was registered on A but i still cant find it on Device B.

    Code (Text):
    1.  
    2. public void registerService(int port) {
    3.     tearDown();
    4.     NsdServiceInfo serviceInfo  = new NsdServiceInfo();
    5.     serviceInfo.setPort(port);
    6.     serviceInfo.setServiceName(mServiceName);
    7.     serviceInfo.setServiceType(SERVICE_TYPE);
    8.     initializeRegistrationListener();
    9.     manager.registerService(
    10.             serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
    11.  
    12. }
    13.  
    14. @Override
    15. public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
    16.     mServiceName = NsdServiceInfo.getServiceName();
    17.     Log.d("Quiz", "Regsistered name " + NsdServiceInfo.getServiceName());
    18.     Log.d("Quiz", "Regsistered port " + String.valueOf(NsdServiceInfo.getPort()));
    19.     Log.d("Quiz", "Service Type " + NsdServiceInfo.getServiceType());
    20. }
    21.  
    22.  
    23. public void discoverServices() {
    24.     stopDiscovery();  // Cancel any existing discovery request
    25.     initializeDiscoveryListener();
    26.     manager.discoverServices(
    27.             SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
    28. }
    29.  
    30. @Override
    31. public void onServiceFound(NsdServiceInfo service) {
    32.     Log.d("Quiz", "Services " +service.getServiceName());
    33.     Log.d("Quiz", "Port " +String.valueOf(service.getPort()));
    34.     Log.d("Quiz", "Service Type " + service.getServiceType());
    35.     boolean found=false;
    36.     if (service.getServiceType().equals(SERVICE_TYPE)) {
    37.         if(servers.size()==0){
    38.             servers.add(service.getServiceName());
    39.           //  Log.d("Quiz", "Services Added");
    40.         }
    41.         for(int i=0;i<servers.size();i++){
    42.             if(servers.get(i).equals(service.getServiceName()))
    43.             {
    44.                 found=true;
    45.             }
    46.         }
    47.     }
    48.     if(found==false) {
    49.         servers.add(service.getServiceName());
    50.       //  Log.d("Quiz", "Services Added");
    51.     }
    52. }
    53.  
    I am adding the services found to a list from which i update a list view for those wondering what the extra code is for.

    Anyone have any ideas as to why i cant find the service?

    Thanks in advance.
     

    Advertisement

Share This Page

Loading...