get Broadcast Address from DhcpInfo - Android Wifi

Android examples for Wifi:Wifi Connection

Description

get Broadcast Address from DhcpInfo

Demo Code


//package com.java2s;

import android.content.Context;

import android.net.DhcpInfo;

import android.net.wifi.WifiManager;

import java.net.InetAddress;

public class Main {
    public static String getBroadcastAddress(Context ctx) {
        WifiManager cm = (WifiManager) ctx
                .getSystemService(Context.WIFI_SERVICE);
        DhcpInfo myDhcpInfo = cm.getDhcpInfo();
        if (myDhcpInfo == null) {
            return "255.255.255.255";
        }/*ww  w  .  ja v a  2  s  .  com*/
        // int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)
        // | ~myDhcpInfo.netmask;
        int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)
                | ~myDhcpInfo.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k < 4; k++)
            quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
        try {
            return InetAddress.getByAddress(quads).getHostAddress();
        } catch (Exception e) {
            return "255.255.255.255";
        }
    }
}

Related Tutorials