//操作 盒子WiFi 开关的方法 false 表示关闭 true 表示打开 public static boolean wifiSwitch(Context context,boolean open){ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager.isWifiEnabled() && !open) { wifiManager.setWifiEnabled(false); } else if (!wifiManager.isWifiEnabled() && open){ wifiManager.setWifiEnabled(true); } return true; } //设置盒子 上网路由方式 static/DHCP public static void setStaticIP(Context context,String type,String ip) { EthernetManager mEthManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); switch (type) { case "Static": StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration(); String[] split = getDns(HwContext.getContext()).split(" / "); for (int i = 0; i < split.length && !TextUtils.isEmpty(split[i]); i++) { InetAddress inetAddress=android.net.NetworkUtils.numericToInetAddress(split[i]); mStaticIpConfiguration.dnsServers.add(inetAddress); } Inet4Address inetAddr = getIPv4Address(ip); int prefixLength = NetUtils.maskStr2InetMask(NetworkUtils.getLanMask(HwContext.getContext())); InetAddress gatewayAddr = getIPv4Address(NetworkUtils.getDefaultGateway(HwContext.getContext())); if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 ) { return ; } mStaticIpConfiguration.ipAddress = new LinkAddress(ip+"/"+prefixLength); mStaticIpConfiguration.gateway=gatewayAddr; switch (NetworkUtils.getActiveNetworkType(getContext())) { case "Ethernet": LogUtils.e("Ethernet"); IpConfiguration mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null); mEthManager.setConfiguration("eth0",mIpConfiguration); break; case "WiFi": LogUtils.e("\"WiFi\""); IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null); WifiConfiguration wifiConfiguration = getCurrentConfig(); wifiConfiguration.setIpConfiguration(wifiIpConfiguration); wifiManager.setWifiApConfiguration(wifiConfiguration); wifiManager.updateNetwork(wifiConfiguration); new Thread(){// 这里比较耗时 所以开启新线程进行处理 @Override public void run() { super.run(); int netId = wifiManager.addNetwork(wifiConfiguration); wifiManager.disableNetwork(netId); boolean b = wifiManager.enableNetwork(netId, true); } }.start(); break; } break; case "DHCP": LogUtils.e("DHCP"); switch (NetworkUtils.getActiveNetworkType(getContext())){ case "Ethernet": LogUtils.e("Ethernet"); IpConfiguration ipConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null); mEthManager.setConfiguration("eth0",ipConfiguration); break; case "WiFi": LogUtils.e("DHCP"); IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null); WifiConfiguration wifiConfiguration = getCurrentConfig(); wifiConfiguration.setIpConfiguration(wifiIpConfiguration); wifiManager.setWifiApConfiguration(wifiConfiguration); wifiManager.updateNetwork(wifiConfiguration); new Thread(){// 这里比较耗时 所以开启新线程进行处理 @Override public void run() { super.run(); int netId = wifiManager.addNetwork(wifiConfiguration); wifiManager.disableNetwork(netId); boolean b = wifiManager.enableNetwork(netId, true); } }.start(); break; } break; } }
public static int maskStr2InetMask(String maskStr) { StringBuffer sb ; String str; int inetmask = 0; int count = 0; /* * check the subMask format */ Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$"); if (pattern.matcher(maskStr).matches() == false) { Log.e("maskStr2InetMask","subMask is error"); return 0; } String[] ipSegment = maskStr.split("\\."); for(int n =0; n<ipSegment.length;n++) { sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n]))); str = sb.reverse().toString(); count=0; for(int i=0; i<str.length();i++) { i=str.indexOf("1",i); if(i==-1) break; count++; } inetmask+=count; } return inetmask; } public static Inet4Address getIPv4Address(String text) { try { return (Inet4Address) NetworkUtils.numericToInetAddress(text); } catch (IllegalArgumentException|ClassCastException e) { return null; } }
AndroidMenifest文件里添加一下 配置
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL"/> <uses-permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"/> <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
前提条件 导入 classes.jar 参考