android手机开启热点并获得连上手机ip

创建热点的方法

private void stratWifiAp(String mSSID, String mPasswd) {
    Method method1 = null;
    try {
        method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        WifiConfiguration netConfig = new WifiConfiguration();

        netConfig.SSID = mSSID;
        netConfig.preSharedKey = mPasswd;
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        method1.invoke(mWifiManager, netConfig, true);

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

/**
 * 检查是否开启wifi
 *
 * @return
 */
public boolean isWifiApEnabled() {
    try {
        Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(mWifiManager);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}


/**
 * 关闭热点
 */
public void closeWifiAp() {
    WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (isWifiApEnabled()) {
        try {
            Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
            method.setAccessible(true);
            WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
            Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            method2.invoke(wifiManager, config, false);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

判断wifi是否打开

public boolean isWifiActive(){
    ConnectivityManager mConnectivity =(ConnectivityManager)
        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;
}

获得热点手机IP地址

DhcpInfo info = wifiManager.getDhcpInfo();
int iii = info.serverAddress;
String ip=intToIp(iii);

private String intToIp(int i) { 
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF); 
}

开热点的手机获得其他连接手机IP的方法

 private ArrayList getConnectedIP() {
    ArrayList connectedIP = new ArrayList();
    try {
        BufferedReader br = new BufferedReader(new FileReader(
                "/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4) {
                String ip = splitted[0];
                if (!ip.equalsIgnoreCase("IP"))
                   connectedIP.add(ip);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    return connectedIP;
}

你可能感兴趣的:(android手机开启热点并获得连上手机ip)