Were working with industrial Android HMI Panels.
In our application we need to set the ethernet settings.
I got a Android studio project from the manufacturer.
But i dont know how to use it in B4A. I tried inline java but getting lots of error due my missing java knowledge.
Can someone help me to convert this?
In our application we need to set the ethernet settings.
I got a Android studio project from the manufacturer.
But i dont know how to use it in B4A. I tried inline java but getting lots of error due my missing java knowledge.
Can someone help me to convert this?
Java:
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.EthernetManager;
import android.net.IpConfiguration;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import android.net.StaticIpConfiguration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.a).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insetNet(MainActivity.this,"192.168.1.111","255.255.255.0","192.168.1.0","192.168.1.0","144.144.144.144");
}
});
findViewById(R.id.b).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insetNet(MainActivity.this,"192.168.1.122","255.255.255.0","192.168.1.0","192.168.1.0","144.144.144.144");
}
});
}
@SuppressLint("WrongConstant")
public void insetNet(Context context, String address, String netmask, String gateway, String dns1, String dns2){
Log.e("insetNet","address="+address+" netmask="+netmask+" gateway="+gateway+" dns1="+dns1+" dns2="+dns2);
mEthManager = (EthernetManager)context.getSystemService("ethernet");
Inet4Address inetAddr = getIPv4Address(address);
int prefixLength = maskStr2InetMask(netmask);
InetAddress gatewayAddr = getIPv4Address(gateway);
InetAddress dnsAddr = getIPv4Address(dns1);
mStaticIpConfiguration = new StaticIpConfiguration();
//mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);
updateLinkAddress(inetAddr, prefixLength);
mStaticIpConfiguration.gateway = gatewayAddr;
mStaticIpConfiguration.dnsServers.add(dnsAddr);
mStaticIpConfiguration.dnsServers.add(getIPv4Address(dns2));
mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);
mEthManager.setConfiguration(mIpConfiguration);
try {
Runtime.getRuntime().exec("sync");
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateLinkAddress(Inet4Address inetAddr, int prefixLength){
Class<?> clazz = null;
try {
clazz = Class.forName("android.net.LinkAddress");
} catch (Exception e) {
// TODO: handle exception
}
Class[] cl = new Class[]{InetAddress.class, int.class};
Constructor cons = null;
//取得所有构造函数
try {
cons = clazz.getConstructor(cl);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//给传入参数赋初值
Object[] x = {inetAddr, prefixLength};
try {
mStaticIpConfiguration.ipAddress = (LinkAddress) cons.newInstance(x);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
EthernetManager mEthManager;
IpConfiguration mIpConfiguration;
StaticIpConfiguration mStaticIpConfiguration;
private Inet4Address getIPv4Address(String text) {
try {
return (Inet4Address) NetworkUtils.numericToInetAddress(text);
} catch (IllegalArgumentException | ClassCastException e) {
return null;
}
}
private 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("ethernet", "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;
}
}