Hello there
so I am trying to translate this SerialPortClass
which uses JNI but I really cant get the grasp of how to correctly translate the bottom part
the lines
// JNI private native static FileDescriptor open(String path, int baudrate, int flags); public native void close(); static { System.loadLibrary("serial_port"); }
heres my port so far
public class SerialPort {
private static string TAG = "SerialPort";
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate, int flags)
{
/* Check access permission */
if (!device.CanRead() || !device.CanWrite()) {
try
{
Log.Debug(TAG, "1============================");
// Missing read/write permission, trying to chmod the file
Process su;
su = Runtime.GetRuntime().Exec("/system/xbin/su");
//String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
//+ "exit\n";
String cmd = "chmod 666 " + device.AbsolutePath;
Log.Debug(TAG, "cmd=====================" + cmd);
Runtime.GetRuntime().Exec(cmd);
//su.getOutputStream().write(cmd.getBytes());
if (/*(su.waitFor() != 0) || */!device.CanRead() || !device.CanWrite())
{
Log.Debug(TAG, "2=====================");
throw new SecurityException();
}
}
catch (Exception ex)
{
Log.Debug(TAG, "3=================");
Log.Debug(TAG, ex.Message);
throw new SecurityException();
}
//do_exec("su root\n");
//Log.e(TAG, "=============cmd : "+device.getAbsolutePath());
//do_exec("chmod 755 " + device.getAbsolutePath() + "\n");
//do_exec("rm /sdcard/123");
//Runtime.getRuntime().exec("/system/xbin/su");
//Runtime.getRuntime().exec("chmod 755 /dev/ttyS1");
}
try
{
mFd = open(device.AbsolutePath, baudrate, flags);
if (mFd == null)
{
Log.Debug(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
catch (Exception ex)
{
}
}
public InputStream getInputStream()
{
return mFileInputStream;
}
public OutputStream getOutputStream()
{
return mFileOutputStream;
}
[DllImport("serial_port")]
private extern static FileDescriptor open(string path, int baudrate, int flags);
[DllImport("serial_port")]
public extern static void close();
}
I probably have the DLLImport name wrong thanks in advance :D