当Android设备连接到wifi AP时,它会使用以下名称标识自己:
android_cc1dec12345e6054
如何从Android应用中获取该字符串?不是为了改变它,只是为了读出。
编辑:
这是我路由器的Web界面的屏幕截图,显示了所有连接设备的列表。请注意列表中的两个Android设备 - 如何从设备上运行的Java代码中读取该字符串?
答案 0 :(得分:12)
建立@ Merlevede的答案,这是获得房产的快捷方式。它是一个私有API,所以它可能会发生变化,但这个代码至少从Android 1.5开始就没有被修改过,所以它可能是安全的。
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;
}
}
答案 1 :(得分:7)
使用NetworkInterface对象枚举接口并从接口“InetAddress
获取规范主机名。由于您需要wifi名称,因此您可以直接查询wlan0
,如果失败,您可以像这样枚举它们:
import android.test.InstrumentationTestCase;
import android.util.Log;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
public class NetworkInterfaceTest extends InstrumentationTestCase {
private static final String TAG = NetworkInterfaceTest.class.getSimpleName();
public void testNetworkName() throws Exception {
Enumeration<NetworkInterface> it_ni = NetworkInterface.getNetworkInterfaces();
while (it_ni.hasMoreElements()) {
NetworkInterface ni = it_ni.nextElement();
Enumeration<InetAddress> it_ia = ni.getInetAddresses();
if (it_ia.hasMoreElements()) {
Log.i(TAG, "++ NI: " + ni.getDisplayName());
while (it_ia.hasMoreElements()) {
InetAddress ia = it_ia.nextElement();
Log.i(TAG, "-- IA: " + ia.getCanonicalHostName());
Log.i(TAG, "-- host: " + ia.getHostAddress());
}
}
}
}
}
这会给你一个这样的输出:
TestRunner﹕ started: testNetworkName
++ NI: lo
-- IA: ::1%1
-- host: ::1%1
-- IA: localhost
-- host: 127.0.0.1
++ NI: p2p0
-- IA: fe80::1234:1234:1234:1234%p2p0
-- host: fe80::1234:1234:1234:1234%p2p0
++ NI: wlan0
-- IA: fe80::1234:1234:1234:1234%wlan0
-- host: fe80::1234:1234:1234:1234%wlan0
-- IA: android-1234567812345678 <--- right here
-- host: 192.168.1.234
提示:如果InetAddress.getCanonicalHostName().equals(InetAddress.getHostAddress())
您可以忽略它,因为它不是“真正的”名称。
答案 2 :(得分:5)
我不知道这是否有帮助,但我在这里。
从unix shell(您可以在Google Play中下载任何终端应用),您可以输入
来获取主机名getprop net.hostname
当然这不是你想要的......但是......另一方面,here是关于如何从java执行unix命令的信息。也许通过将这两者结合起来,你得到了你正在寻找的东西。
答案 3 :(得分:0)
java
:
您可以通过以下方式获得该物业:
String str = SystemProperties.get("net.hostname");
答案 4 :(得分:0)
我使用了jiangze ren的答案并对其进行了修改。
现在您可以通过获取设备的IP地址来获取主机名:
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
final String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
InetAddress address = InetAddress.getByName(ip);
System.out.println(address.getHostName()); // <-- host name is here and not localhost!
答案 5 :(得分:-1)
试试这个:
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
String serialNumber = (String) get.invoke(c, "net.hostname", "Error");
if (serialNumber.equals("Error")) {
serialNumber = (String) get.invoke(c, "net.hostname", "Error");
}
答案 6 :(得分:-2)
您可以使用以下代码:
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostName()); // <-- host name is here!
实际上,您可以使用此功能获得更多信息。 如... 其他设备主机名在同一网络中。