0

我再次有这个关于 NSD 的问题,这次我想问一下是否有人知道如何只显示服务名称而不显示端口、主机、服务类型等所有详细信息。到目前为止,我设法使用 ArrayList 和 NsdServiceInfo 对象显示所有服务它显示了我之前提到的有关服务的所有详细信息,但就我而言,我只想显示服务名称。

如果您有任何想法,非常欢迎您与我分享

private String SERVICE_TYPE = "_lala._tcp.";

private InetAddress hostAddress;
private int hostPort;
private NsdManager mNsdManager;
NsdServiceInfo mService;

private WebView mWebView;
ArrayList<NsdServiceInfo> services;
ArrayAdapter<NsdServiceInfo> adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Getting toolbar by id
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //disabling default title text
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    //NSD stuff

    mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
    mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);


    //Creating the arrayList and arrayAdapter to store services
    services = new ArrayList<>();
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, services);
    ListView listView = findViewById(R.id.ListViewServices);
    listView.setAdapter(adapter);


    //Creating onClick listener for the chosen service and trying to resolve it

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Object serviceObj = adapterView.getItemAtPosition(i);
            NsdServiceInfo selectedService = (NsdServiceInfo) serviceObj;
            //mNsdManager.stopServiceDiscovery(mDiscoveryListener);
            mNsdManager.resolveService(selectedService, mResolveListener);

        }
    });


}

// Each time create new discovery listener for discovery methods onServicefound.

NsdManager.DiscoveryListener mDiscoveryListener = new NsdManager.DiscoveryListener() {
    @Override
    public void onStartDiscoveryFailed(String serviceType, int errorCode) {
        Log.e("TAG", "DiscoveryFailed: Error code: " + errorCode);
        mNsdManager.stopServiceDiscovery(this);

    }

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

    @Override
    public void onDiscoveryStarted(String regType) {
        Log.d("TAG", "Service discovery started");

    }

    @Override
    public void onDiscoveryStopped(String serviceType) {
        Log.i("TAG", "Discovery stopped: " + serviceType);

    }

    // When service is found add it to the array. Log d is for debugging purposes.
    @Override
    public void onServiceFound(NsdServiceInfo serviceInfo) {




        Log.d("TAG", "Service discovery success : " + serviceInfo);
        Log.d("TAG", "Host = " + serviceInfo.getServiceName());
        Log.d("TAG", "Port = " + serviceInfo.getPort());
        services.add(serviceInfo);


        //Notifying adapter about change in UI thread because it informs view about data change.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
            adapter.notifyDataSetChanged();


            }
        });

    }

    //When service connection is lost to avoid duplicating we create new serviceToRemove object and check if that object is equals to current service,
    // if it yes wre remove the service from an array and inform adapter about the change.

    @Override
    public void onServiceLost(NsdServiceInfo nsdServiceInfo) {
        Log.d("TAG", "Service lost " + nsdServiceInfo);
        NsdServiceInfo serviceToRemove = new NsdServiceInfo();
        for (NsdServiceInfo currentService : services) {
            if (currentService.getHost() == nsdServiceInfo.getHost() && currentService.getPort() == currentService.getPort() && currentService.getServiceName() == currentService.getServiceName()) {
                serviceToRemove = currentService;
            }
        }
        if (serviceToRemove != null) {
            services.remove(serviceToRemove);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
        }
        Log.d("TAG", "Xd" + services);
    }
    //}
};

//Creating new Resolve listener each time when user chooses the service. If the connection was made we launch web view activity with the webpage.

NsdManager.ResolveListener mResolveListener = new NsdManager.ResolveListener() {


    @Override
    public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int errorCode) {
        Log.e("TAG", "Resolved failed " + errorCode);
        Log.e("TAG", "Service = " + nsdServiceInfo);
    }

    @Override
    public void onServiceResolved(NsdServiceInfo nsdServiceInfo) {


        Log.d("TAG", "bbz" + nsdServiceInfo);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
                startActivity(intent);
            }
        });


        Log.d("TAG", "Resolve Succeeded " + nsdServiceInfo);

        if (nsdServiceInfo.getServiceType().equals(SERVICE_TYPE)) {
            Log.d("TAG", "Same IP");
            return;
        }


        hostPort = nsdServiceInfo.getPort();
        hostAddress = nsdServiceInfo.getHost();


    }
};


// NsdHelper's tearDown method
public void tearDown() {

    mNsdManager.stopServiceDiscovery(mDiscoveryListener);

}


}
4

0 回答 0