当Android设备连接到wifi AP时,它用如下名称标识自己:
android_cc1dec12345e6054
如何从Android应用程序中获得该字符串?不是为了改变它,只是为了读出。
编辑:
这是我的路由器网络界面的截图,显示了所有连接设备的列表。注意列表中的两个Android设备--如何从运行在设备上的Java代码中读取该字符串?

发布于 2014-02-20 06:14:20
在@Merlevede的回答的基础上,这里有一个快速而肮脏的方法来获得财产。它是一个私有API,所以它可能会被修改,但是这段代码至少从Android1.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;
}
}发布于 2014-02-20 05:37:53
我不知道这是否有帮助,但我要走了。
从unix (可以下载Google中的任何终端应用程序),您可以通过键入
getprop net.hostname当然这不是你想要的..。但是..。另一方面,here是关于如何从java执行unix命令的信息。也许把这两者结合起来,你就能得到你想要的东西。
发布于 2014-12-24 08:48:27
使用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()),你可以忽略它,因为它不是一个“真实”的名字。
https://stackoverflow.com/questions/21898456
复制相似问题