0

我使用网络发现服务来查找网络中的特定设备服务。我设法获得了带有 IP 地址、端口、服务类型和名称的设备,我想把这些信息放在列表视图中,有人能推荐我怎么做吗?

MainActivity.java

公共类 MainActivity 扩展 AppCompatActivity {

private String SERVICE_NAME = "app";
private String SERVICE_TYPE = "_app_tcp.";


private InetAddress hostAddress;
private int hostPort;
private NsdManager mNsdManager;
NsdServiceInfo mService;
private ArrayAdapter 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);

    //ListView listView = findViewById(R.id.listView);
    //adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);


    //NSD stuff

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


}


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

    }

    @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());

        if (serviceInfo.getServiceType().equals(SERVICE_TYPE)) {
            Log.d("TAG", "Unknown Service Type: " + serviceInfo.getServiceType());
            mNsdManager.resolveService(serviceInfo, mResolveListener);

        } else if (serviceInfo.getServiceName().equals(SERVICE_NAME)) {
            Log.d("TAG", "Same machine " + SERVICE_NAME);

        } else {
            Log.d("TAG", "Diff Machine : " + serviceInfo.getServiceName());

        }

    }

    @Override
    public void onServiceLost(NsdServiceInfo nsdServiceInfo) {
        Log.d("TAG", "Service lost " + nsdServiceInfo);


    }
};

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", "Resolve Succeeded " + nsdServiceInfo);

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

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

    }
};


}

activity_main.xml

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.app.MainActivity">


<include
    android:id="@+id/toolbar"
    layout="@layout/toolbarmain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</include>


<ListView

    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

</ListView>

4

0 回答 0