gpt4 book ai didi

android - 在android中获取错误的网络接口(interface)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:13 25 4
gpt4 key购买 nike

我已经将我的手机更新到 Android Lollipop,在更新到 Lollipop 之前它工作正常。现在我面临网络接口(interface)问题。

  if (isWifiConnected()) {
Log.d(TAG,"Wifi is connected");
mNetIf = Utils.getActiveNetworkInterface();
String name = mNetIf.getName();
Log.d(TAG, "network interface in constructor" + NetworkInterface.getByName(name));
//do some multicast Operations.

}

如果 WiFi 已连接,我应该在 WiFi 中进行一些多播操作。

iswifiConnected 方法

 public  boolean isWifiConnected(){
ConnectivityManager connManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
return true;
}
return false;
}

Utils.getActiveNetworkInterface

public static NetworkInterface getActiveNetworkInterface() {

Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
return null;
}

while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();

/* Check if we have a non-local address. If so, this is the active
* interface.
*
* This isn't a perfect heuristic: I have devices which this will
* still detect the wrong interface on, but it will handle the
* common cases of wifi-only and Ethernet-only.
*/
while (inetAddresses.hasMoreElements()) {
InetAddress addr = inetAddresses.nextElement();

if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
return iface;
}
}
}

return null;
}

日志

Wifi  is connected
network interface in constructor**[rmnet0][3]t** [/fe80::32e3:daf0:ba51:f971%rmnet0%3][/27.57.104.11]//3g network interface

我想知道应用程序是如何获得 3g 接口(interface)的。它应该有 wlan 接口(interface),它可以在所有其他手机上工作。是错误吗?

权限:

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

最佳答案

您正在使用 ConnectivityManger,但是要在 Android 中使用 WiFi 网络,最好的选择是使用 WifiManager :它有许多专用于 Wifi 的功能。特别是,如果你想获得一个引用 WiFi 接口(interface)的 NetworkInterface 对象,你可以使用以下方法:

public static NetworkInterface getActiveWifiInterface(Context context) throws SocketException, UnknownHostException {
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//Return dynamic information about the current Wi-Fi connection, if any is active.
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo == null) return null;
InetAddress address = intToInet(wifiInfo.getIpAddress());
return NetworkInterface.getByInetAddress(address);
}

public static byte byteOfInt(int value, int which) {
int shift = which * 8;
return (byte)(value >> shift);
}

public static InetAddress intToInet(int value) {
byte[] bytes = new byte[4];
for(int i = 0; i<4; i++) {
bytes[i] = byteOfInt(value, i);
}
try {
return InetAddress.getByAddress(bytes);
} catch (UnknownHostException e) {
// This only happens if the byte array has a bad length
return null;
}
}

关于android - 在android中获取错误的网络接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28991965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com