下面列出了怎么用android.net.DhcpInfo的API类实例代码及写法,或者点击链接到github查看源代码。
private static String[] readDnsServersFromWifiManager(Context context) {
LinkedList<String> dnsServers = new LinkedList<>();
try {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null || !wifiManager.isWifiEnabled()) {
return new String[0];
}
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
if (dhcpInfo != null) {
if (dhcpInfo.dns1 != 0) {
dnsServers.add(intToIp(dhcpInfo.dns1));
}
if (dhcpInfo.dns2 != 0) {
dnsServers.add(intToIp(dhcpInfo.dns2));
}
}
} catch (Exception e) {
}
return dnsServers.isEmpty() ? new String[0] : dnsServers.toArray(new String[dnsServers.size()]);
}
private JSONObject serializeDhcpInfo(DhcpInfo data) throws JSONException {
JSONObject result = new JSONObject(mGson.toJson(data));
int ipAddress = data.ipAddress;
byte[] addressBytes = {
(byte) (0xff & ipAddress),
(byte) (0xff & (ipAddress >> 8)),
(byte) (0xff & (ipAddress >> 16)),
(byte) (0xff & (ipAddress >> 24))
};
try {
String addressString = InetAddress.getByAddress(addressBytes).toString();
result.put("IpAddress", addressString);
} catch (UnknownHostException e) {
result.put("IpAddress", ipAddress);
}
return result;
}
/**
* result[0] is self ip,result[1] is host ip,result[2] is isWifiEnable,true or false.
*/
public static ArrayList<String> getWifiHostAndSelfIP(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String isWifiEnable;
if (!wifiManager.isWifiEnabled()) {
isWifiEnable = "false";
} else
isWifiEnable = "true";
ArrayList<String> result = new ArrayList<>();
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String IPAddress = intToIp(wifiInfo.getIpAddress());
result.add(IPAddress);
DhcpInfo dhcpinfo = wifiManager.getDhcpInfo();
String serverAddress = intToIp(dhcpinfo.serverAddress);
result.add(serverAddress);
result.add(isWifiEnable);
return result;
}
public static InetAddress getBroadcastAddress(Context c) throws UnknownHostException {
WifiManager wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
if (dhcp == null) {
return InetAddress.getByAddress(null);
}
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++) {
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return InetAddress.getByAddress(quads);
}
/**
* wifi状态下获取网关
*/
public static String pingGateWayInWifi(Context context) {
String gateWay = null;
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
return "wifiManager not found";
}
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
if (dhcpInfo != null) {
int tmp = dhcpInfo.gateway;
gateWay = String.format("%d.%d.%d.%d", (tmp & 0xff), (tmp >> 8 & 0xff),
(tmp >> 16 & 0xff), (tmp >> 24 & 0xff));
}
return gateWay;
}
private InetAddress getBroadcastAddress()
throws UnknownHostException
{
final WifiManager wifi = (WifiManager) SettingsActivity.getActivity().getSystemService(
Context.WIFI_SERVICE);
final DhcpInfo dhcp = wifi.getDhcpInfo();
if (dhcp == null)
{
return InetAddress.getByName("0.0.0.0");
}
final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
final byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
{
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return InetAddress.getByAddress(quads);
}
public static InetAddress getBroadcastAddress(Context context) throws UnknownHostException {
if(isWifiApEnabled(context)) {
return InetAddress.getByName("192.168.43.255");
}
WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
if(dhcp==null) {
return InetAddress.getByName("255.255.255.255");
}
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++) {
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return InetAddress.getByAddress(quads);
}
private static String getNetwork(DhcpInfo dhcp){
int ip = dhcp.ipAddress;
int mask = dhcp.netmask;
if (ip == 0 || mask == 0) return null;
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ip = Integer.reverseBytes(ip);
mask = Integer.reverseBytes(mask);
}
ip &= mask;
mask = netmaskToCIDR(mask);
if (mask == 0) return null;
int a = (ip >> 24) & 0xFF;
int b = (ip >> 16) & 0xFF;
int c = (ip >> 8) & 0xFF;
int d = ip & 0xFF;
return String.format(Locale.US, "%d.%d.%d.%d/%d", a, b, c, d, mask);
}
public static String getBroadcastAddress(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) (broadcast >> (k * 8));
try {
return InetAddress.getByAddress(quads).getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "255.255.255.255";
}
/**
* 获取热点主机 IP 地址
* @return 热点主机 IP 地址
*/
@SuppressLint("MissingPermission")
public String getHotspotServiceIp() {
try {
// 获取网关信息
DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
// 获取服务器地址
return intToString(dhcpInfo.serverAddress);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getHotspotServiceIp");
}
return null;
}
public static String getRouterIpAddress(@NonNull Context context){
try{
WifiManager wifiManager=(WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo=wifiManager.getDhcpInfo();
return Formatter.formatIpAddress(dhcpInfo.gateway);
}catch (Exception e){
e.printStackTrace();
}
return "192.168.1.1";
}
private String getGatewayIP() {
if (!checkIsWifiOnAndConnected()) return "0.0.0.0";
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
int ip = dhcp.gateway;
return String.format("%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff)
);
}
public JSONObject toJson(Object object) throws JSONException {
if (object instanceof DhcpInfo) {
return serializeDhcpInfo((DhcpInfo) object);
} else if (object instanceof WifiConfiguration) {
return serializeWifiConfiguration((WifiConfiguration) object);
} else if (object instanceof WifiInfo) {
return serializeWifiInfo((WifiInfo) object);
}
return defaultSerialization(object);
}
public static void getGatewayIpAddress(Context c) {
// get wifi ip
final WifiManager manager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);
StringBuilder IFCONFIG = new StringBuilder();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
IFCONFIG.append(inetAddress.getHostAddress() + "\n");
}
}
}
} catch (SocketException ex) {
Log.e("LOG_TAG", ex.toString());
}
MLog.d(TAG, "ifconfig " + IFCONFIG.toString());
MLog.d(TAG, "hotspot address is " + address);
}
public static InetAddress getBroadcastAddress() throws IOException {
WifiManager wifi = (WifiManager)SSJApplication.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
if(dhcp == null)
throw new IOException("dhcp is null");
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
return InetAddress.getByAddress(quads);
}
public static byte[] getBroadcastIPAddressRaw(final Context pContext) throws WifiUtilsException {
final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
final DhcpInfo dhcp = wifiManager.getDhcpInfo();
// TODO handle null somehow...
final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
final byte[] broadcastIP = new byte[IPUtils.IPV4_LENGTH];
for (int k = 0; k < IPUtils.IPV4_LENGTH; k++) {
broadcastIP[k] = (byte) ((broadcast >> (k * 8)) & 0xFF);
}
return broadcastIP;
}
/**
* 判断WIFI是否连接
*
* @param context Context
* @return true:连接, false:未连接
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isWifiConnected(Context context) {
final WifiManager manager = (WifiManager) context.getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);
if (manager == null)
return false;
final DhcpInfo info = manager.getDhcpInfo();
return info != null && info.ipAddress != 0;
}
/**
* 获取WIFI IP地址
*
* @param context Context
* @return IP地址
*/
public static String getWifiIp(Context context) {
final WifiManager manager = (WifiManager) context.getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);
if (manager == null)
return "0.0.0.0";
final DhcpInfo info = manager.getDhcpInfo();
if (info == null)
return "0.0.0.0";
final int ip = info.ipAddress;
return (0xFF & ip) + "." + (0xFF & ip >> 8) + "." + (0xFF & ip >> 16) + "."
+ (0xFF & ip >> 24);
}
public final static byte[] getBroadcastAddress(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
// handle null somehow
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++) {
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return quads;
}
/**
* Get WiFi Router IP from DHCP
* @return
*/
private String getWiFiRouterIP() {
DhcpInfo dhcp = wifiManager.getDhcpInfo();
int ip = dhcp.gateway;
return formatIP(ip);
}
@ReactMethod
public void getDhcpServerAddress(Callback callback) {
DhcpInfo dhcpInfo = wifi.getDhcpInfo();
String ip = longToIP(dhcpInfo.serverAddress);
callback.invoke(ip);
}
public static String getServerIPAddress() {
DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
return intToIp(mDhcpInfo.gateway);
}
@Override
protected String doInBackground(Void... ip) {
DhcpInfo dhcpInfo = ((WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
return intToIp(dhcpInfo.gateway);
}
public String getgateway() {
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
String str1 = FormatIP(dhcpInfo.gateway);
return str1;
}
public String getgateway() {
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
String str1 = FormatIP(dhcpInfo.gateway);
return str1;
}
public static String getServerIPAddress() {
DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
return intToIp(mDhcpInfo.gateway);
}