获取Android WiFi"net.hostname"从代码 [英] Get Android WiFi "net.hostname" from code
问题描述
当Android设备连接到wifi AP时,它会使用以下名称标识自己:
When an Android device connects to a wifi AP, it identifies itself with a name like:
android_cc1dec12345e6054
android_cc1dec12345e6054
如何从Android应用程序中获取该字符串?并非出于更改目的,而只是出于读出目的.
How can that string be obtained from within an Android app? Not for the purpose of changing it, just for readout.
这是路由器的Web界面的屏幕截图,显示了所有已连接设备的列表.请注意列表中的两个Android设备-如何从设备上运行的Java代码读取该字符串?
This is a screenshot of my router's web interface, showing a list of all connected devices. Note the two Android devices on the list -- How can that string be read from Java code running on the device?
推荐答案
以@Merlevede的答案为基础,这是一种获取属性的快捷方法.这是一个私有API,因此可能会发生变化,但是至少从Android 1.5开始,此代码就没有进行过修改,因此使用起来可能很安全.
Building off of @Merlevede's answer, here's a quick and dirty way to get the property. It's a private API, so it's subject to change, but this code hasn't been modified since at least Android 1.5 so it's probably safe to use.
import android.os.Build;
import java.lang.reflect.Method;
/**
* Retrieves the net.hostname system property
* @param defValue the value to be returned if the hostname could
* not be resolved
*/
public static String getHostName(String defValue) {
try {
Method getString = Build.class.getDeclaredMethod("getString", String.class);
getString.setAccessible(true);
return getString.invoke(null, "net.hostname").toString();
} catch (Exception ex) {
return defValue;
}
}
这篇关于获取Android WiFi"net.hostname"从代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!