我有一个任务,我使用 NsdServiceInfo 类获取有关服务的信息,例如服务名称、主机、端口、IP 地址并在自定义数组适配器中显示一些,但是我从 NsdServiceInfo 获得的信息还不够,我需要显示我正在连接的设备的型号类型和状态,因为我正在使用个人协议连接 http 服务器。
到目前为止,我只在屏幕上显示服务名称和 IP 地址,因为我只能使用 NsdServiceInfo 类访问这些信息。对于模型类型和状态,我想创建单独的类并将 NsdServiceInfo 中的所有信息存储到该自定义类中,但我不确定如何做到这一点,或者它是否是正确的方法。到目前为止,我已经创建了自定义类并修改了自定义适配器,但是当涉及到更改主要活动时,我迷失了。
如果您有任何想法,请与我分享:)
PrinterNew.java 自定义类
package com.example.app;
public class PrinterNew {
String printerName;
String printerModel;
int printerState;
public PrinterNew() {
}
public String getPrinterName() {
return printerName;
}
public void setPrinterName(String printerName) {
this.printerName = printerName;
}
public String getPrinterModel() {
return printerModel;
}
public void setPrinterModel(String printerModel) {
this.printerName = printerModel;
}
public int getPrinterState() {
return this.printerState;
}
public void setPrinterState(int printerState) {
this.printerState = printerState;
}
}
NsdServiceInfoAdapter.java 我的自定义数组适配器(对不起,我只是在玩代码,还不想重命名类)
public class NsdServiceInfoAdapter extends ArrayAdapter<PrinterNew> {
private Context mContext;
private List<PrinterNew> services;
private InetAddress hostAddress;
//Creating new constructor with parameters such as this class(context), layout id (list item layout Id) and data model.
public NsdServiceInfoAdapter(@NonNull Context context, int layoutId, List<PrinterNew> list) {
super(context, layoutId, list);
mContext = context;
services = list;
}
@NonNull
@Override
// Creating method get(), which is called when listItem needs to be populated with data.
//Get a View that displays the data at the specified position in the data set.
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem = convertView;
//Checking if view is empty then we inflate our list layout.
if (listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
//Getting data's position in the data set.
PrinterNew currentService = services.get(position);
//We asign the the view to the item layout as a TextView
TextView t = listItem.findViewById(R.id.TextView_serviceName);
TextView r = listItem.findViewById(R.id.TextView_serviceIP);
//hostAddress = currentService.getHost();
r.setText("IP: " + currentService.getPrinterModel());
t.setText("name: " + currentService.getPrinterName());
Log.d("tag", "model" + currentService.getPrinterModel());
Log.d("hello2", String.valueOf(currentService.getPrinterName()));
return listItem;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String SERVICE_TYPE = "_lala._tcp."; // change to normal
private InetAddress hostAddress;
private int hostPort;
private NsdManager mNsdManager;
ArrayList<PrinterNew> services;
private NsdServiceInfoAdapter mAdapter;
@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);
services = new ArrayList<>();
mAdapter = new NsdServiceInfoAdapter(this, R.id.TextView_serviceName, services);
ListView listView = findViewById(R.id.ListViewServices);
listView.setAdapter(mAdapter); // we add custom adapter to the listview to display data from adapter.
//disabling default title text
getSupportActionBar().setDisplayShowTitleEnabled(false);
//NSD stuff
mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
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;
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
startActivity(intent);
}
});
//mNsdManager.stopServiceDiscovery(mDiscoveryListener);
//mNsdManager.resolveService(selectedService, mResolveListener);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://10.0.0.100/SettingGetPrinterName";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Model", "Printer mode" + response.toString());
String temp = response.toString().substring(2, response.length() - 3);
byte array[] = temp.getBytes();
Log.d("Model", "Printer mode" + temp);
//Assigning printer model to the PrinterNew class , printer model attribute.
PrinterNew myPrinterDetails = new PrinterNew();
myPrinterDetails.printerModel = temp;
Log.d("Model", "zdrv77 " + myPrinterDetails.printerModel);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Model", "Nope ");
}
});
queue.add(stringRequest);
}
public NsdManager.ResolveListener initializeResolveListener() {
return new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int errorCode) {
Log.e("TAG", "Resolved failed " + errorCode);
Log.e("TAG", "Service = " + nsdServiceInfo);
mNsdManager.resolveService(nsdServiceInfo, initializeResolveListener());
}
@Override
public void onServiceResolved(final NsdServiceInfo nsdServiceInfo) {
final PrinterNew myPrinterDetails = new PrinterNew();
myPrinterDetails.printerName = nsdServiceInfo.getServiceName();
myPrinterDetails.printerModel = myPrinterDetails.getPrinterModel();
Log.d("TAG", "Resolve Succeeded " + nsdServiceInfo);
runOnUiThread(new Runnable() {
@Override
public void run() {
services.add(myPrinterDetails);
mAdapter.notifyDataSetChanged();
}
});
}
};
}
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(final NsdServiceInfo serviceInfo) {
Log.d("TAG", "Service discovery success : " + serviceInfo);
Log.d("TAG", "Host = " + serviceInfo.getServiceName());
Log.d("TAG", "Port = " + serviceInfo.getPort());
if (!services.contains(serviceInfo)) {
if (serviceInfo.getServiceType().equals(SERVICE_TYPE)) {
mNsdManager.resolveService(serviceInfo, initializeResolveListener());
}
}
}
@Override
public void onServiceLost(NsdServiceInfo nsdServiceInfo) {
Log.d("TAG", "Service lost " + nsdServiceInfo);
PrinterNew serviceToRemove = new PrinterNew();
for (PrinterNew currentService : services) {
if (currentService.getPrinterName() == currentService.getPrinterName() && currentService.getPrinterModel() == currentService.getPrinterModel()) {
serviceToRemove = currentService;
}
}
final PrinterNew finalServiceToRemove = serviceToRemove;
if (serviceToRemove != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
services.remove(finalServiceToRemove);
mAdapter.notifyDataSetChanged();
}
});
}
Log.d("TAG", "Xd" + services);
}
};
/*
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);
}
*/
}