如何从Android的IP地址获取设备名称?

时间:2018-08-31 03:02:27

标签: android

我想问一个问题,我试图通过热点连接获取IP地址,但现在我仍然不明白如何从Android的IP地址获取设备名称。

我刚刚尝试使用以下代码:

InetAddress address = InetAddress.getByName ("192.168.43.81");
String hostname = address.getCanonicalHostName ();

但是在尝试使用上面的代码后,仍然获得ip:192.168.43.81,我无法通过热点或wifi获得设备名称。有谁可以帮助我吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用以下命令获取所连接的WiFi网络的名称:

WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
String name = wifiInfo.getSSID();

答案 1 :(得分:0)

要获取设备IP。

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);String ipAddress = BigInteger.valueOf(wm.getDhcpInfo().netmask).toString();

获取设备信息(名称,制造年份等)

StringBuffer infoBuffer = new StringBuffer();
infoBuffer.append("-------------------------------------\n");
infoBuffer.append("Model :" + Build.MODEL + "\n");//The end-user-visible name for the end product.
infoBuffer.append("Device: " + Build.DEVICE + "\n");//The name of the industrial design.
infoBuffer.append("Manufacturer: " + Build.MANUFACTURER + "\n");//The manufacturer of the product/hardware.
infoBuffer.append("Board: " + Build.BOARD + "\n");//The name of the underlying board, like "goldfish".
infoBuffer.append("Brand: " + Build.BRAND + "\n");//The consumer-visible brand with which the product/hardware will be associated, if any.
infoBuffer.append("Serial: " + Build.SERIAL + "\n");infoBuffer.append("-------------------------------------\n");

Android文档:此“序列”字段在API级别 O 中已弃用。请改用getSerial()。硬件序列号(如果有)。仅字母数字,不区分大小写。对于将SDK定位为高于N_MR1的应用,此字段设置为未知。

我只是使用AlertDialog显示设备信息AlertDialog.Builder dialog = new

AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(true);
dialog.setTitle("Device information:");
dialog.setMessage(infoBuffer);//StringBuffer which we appended the device informations.
dialog.show();