NSD发现服务android

问题描述

任务说明: 我打算构建一个应用程序,它可以使用 NSD android 在我的网络中发现 ESP32 提供的服务,所以我实现了下面的代码 代码

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,"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);



     }


}
 

但我收到以下错误

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:错误:找不到符号 discoveryListener = new NsdManager.discoveryListener() { ^

                 ^

你能帮忙吗?

解决方法

你忘记输入变量的类型了。如果应该是

您在使用 nsdManager 时也会遇到同样的问题。在您的最后一个函数中,因为您使用了 discoveryListener 而不定义它。当您从某处复制粘贴此代码时,您是否忘记复制粘贴类的实例变量?

,

总是在同一条路上,我遇到了这个问题

:\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:188: error: not a static discovery context variable nsdManager.discoverServices("_http._tcp.",NsdManager.PROTOCOL_DNS_SD,discoveryListener);

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,"Discovery failed: Error code:" + errorCode);
                 nsdManager.stopServiceDiscovery(this);
             }
         };
     }

 public static void discover(){
     System.out.println("discoverServices");

     nsdManager.discoverServices("_http._tcp.",discoveryListener);


 }
     //nsdManager = (NsdManager)c.getSystemService(Context.NSD_SERVICE);






}