android wifi开发

之前查阅的一些总结,和大家分享一下。 

有关于AP热点开发(wifi发热点开发)的总结请看: http://blog.csdn.net/error/404.html?from=http%3a%2f%2fblog.csdn.net%2fguang_liang_%2farticle%2fdetails%2f55224675

 

public class Wifi_WifiManager {
    private Context mContext;
    private android.net.wifi.WifiManager wifiManager ;
    public static boolean isInOpenWifi=false;

    public Wifi_WifiManager(Context context){
        this.mContext=context;
        wifiManager=(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    /**
     * 判断wifi是否打开
     * @return
     */
    public boolean isWifiActive(){
        ConnectivityManager mConnectivity=(ConnectivityManager)
                mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mConnectivity!=null){
            NetworkInfo[] infos=mConnectivity.getAllNetworkInfo();
            if (infos !=null){
                for (NetworkInfo ni:infos){
                    if ("WIFI".equals(ni.getTypeName())&&ni.isConnected())
                        return true;
                }
            }
        }
        return false;
    }

    /**
     * 打开wifi
     */
    public void openWifi(){
        if (!isWifiActive())
            wifiManager.setWifiEnabled(true);
    }

    /**
     * 关闭wifi
     */
    public void closeWifi(){
        if (isWifiActive())
            wifiManager.setWifiEnabled(false);
    }

    /**
     * 记录进入软件时的wifi,
     * 若开启,离开软件时开启wifi
     */
    public void InApp_isOpenWifi(){
        if (isWifiActive())
            isInOpenWifi = true;
    }

    /**
     * 获得热点手机IP地址
     * @return ip
     */
    public String getIp(){
        DhcpInfo info=wifiManager.getDhcpInfo();
        int iii=info.serverAddress;
        String ip=intToIp(iii);
        return ip;
    }

    /**
     * 获得ip地址算法
     * @param i
     * @return IP
     */
    private String intToIp(int i){
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF);
    }

}


 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页