javaandroidandroid-studionetwork-programmingnsd

NSD discovering services android


Task description: I intend to build an app that can discover services given by ESP32 in my Network using NSD android, so I implemented the code below Code:

import android.net.DhcpInfo;
import  android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.DiscoveryListener;
import android.util.Log;
import android.net.nsd.NsdServiceInfo;
import android.text.format.Formatter;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.InetAddress;
import  java.io.IOException;
import  java.net.UnknownHostException;
//import android.app.Activity;


import java.lang.reflect.Method;

 public  class MusicList extends org.qtproject.qt5.android.bindings.QtActivity {


     public static String getWifiName(Context context) throws IOException {
             WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
             final DhcpInfo dhcp = manager.getDhcpInfo();
             InetAddress localhost = null;
             //byte[] localHostIp = InetAddress.getLocalHost().getAddress();
             //String string = new String(localHostIp);
             //InetAddress in= null;
             //in = InetAddress.getByName(Formatter.formatIpAddress(dhcp.gateway));

             if (manager.isWifiEnabled()) {
                WifiInfo wifiInfo = manager.getConnectionInfo();
                if (wifiInfo != null) {
                   DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                   if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {



                                              System.out.println(Formatter.formatIpAddress(dhcp.gateway));
                                              System.out.println(Formatter.formatIpAddress(dhcp.netmask));
                                              
                                              



                       return wifiInfo.getSSID()+"/"+Formatter.formatIpAddress(dhcp.gateway)+"/"+Formatter.formatIpAddress(dhcp.netmask);

                   }
                }
             }
            else {
                manager.setWifiEnabled(true);

                }
          return null;
         }

     public void initializeDiscoveryListener() {

         // Instantiate a new DiscoveryListener

         discoveryListener = 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(serviceName)) {
                     // 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: " + serviceName);
                 } else if (service.getServiceName().contains("NsdChat")){
                     nsdManager.resolveService(service, resolveListener);
                 }
             }

             @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);
                 nsdManager.stopServiceDiscovery(this);
             }

             @Override
             public void onStopDiscoveryFailed(String serviceType, int errorCode) {
                 Log.e(TAG, "Discovery failed: Error code:" + errorCode);
                 nsdManager.stopServiceDiscovery(this);
             }
         };
     }

 public void discoverServices(){
     nsdManager = Context.getSystemService(Context.NSD_SERVICE);
     nsdManager.discoverServices("_http._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener);



     }


}
 

But I got the following error:

D:\ProgQt\build-app-Android_for_armeabi_v7a_arm64_v8a_x86_x86_64_Clang_Qt_5_15_0_for_Android-Debug\android-build\src\org\qtproject\example\musiclist\MusicList.java:127: error: cannot find symbol discoveryListener = new NsdManager.DiscoveryListener() { ^

                 ^

Could you help here please?


Solution

  • You forgot to put the type of the variable. If should be NsdManager.DiscoveryListener discoveryListener = new NsdManager.DiscoveryListener() {

    You're also going to have the same problem with nsdManager. And in your last function because you use discoveryListener without defining it. When you copy pasted this code from somewhere did you forget to copy paste the instance variables of the class?