Beispiel #1
0
        public bool estarConectado()
        {
            Runtime runtime = Runtime.GetRuntime();

            try {
                Java.Lang.Process ipProcess = runtime.Exec("/system/bin/ping -c 1 8.8.8.8");
                int exitValue = ipProcess.WaitFor();
                return(exitValue == 0);
            }
            catch (IOException e) {
                e.PrintStackTrace();
            }
            catch (InterruptedException e) {
                e.PrintStackTrace();
            }
            return(false);
        }
Beispiel #2
0
        public static void test_run()
        {
            //var app_path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var path = Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath, "KoromoCopy", "youtube_dl");

            using (var br = new BinaryReader(Application.Context.Assets.Open("youtube_dl")))
            {
                using (var bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
                {
                    byte[] buffer = new byte[2048];
                    int    length = 0;
                    while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        bw.Write(buffer, 0, length);
                    }
                }
            }

            //if (File.Exists(Path.Combine(app_path, "youtube_dl")))
            //    ;

            Java.Lang.Process p = Runtime.GetRuntime().Exec("chmod u+x " + path);
            //Java.Lang.Process p = Runtime.GetRuntime().Exec(new string[] { "/system/bin/chmod", "744", Path.Combine(app_path, "youtube_dl") });

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.InputStream));
            int            read;

            char[]       bbuffer = new char[4096];
            StringBuffer output  = new StringBuffer();

            while ((read = reader.Read(bbuffer)) > 0)
            {
                output.Append(bbuffer, 0, read);
            }
            reader.Close();


            p.WaitFor();

            var st = output.ToString();
        }
Beispiel #3
0
        public Task <bool> PingHost(string host = null, int timeout = 3)
        {
            bool pingable = false;

            if (host == null)
            {
                host = Static.Secrets.ServerIp;
            }

            Runtime runtime = Runtime.GetRuntime();

            Java.Lang.Process process = runtime.Exec($"ping -c 1 -W {timeout} {host}");
            int result = process.WaitFor();

            if (result == 0)
            {
                pingable = true;
            }

            return(Task.FromResult(pingable));
        }
        public SerialPort(File device, int baudrate, int flags)
        {
            //        检查访问权限,如果没有读写权限,进行文件操作,修改文件访问权限
            if (!device.CanRead() || !device.CanWrite())
            {
                try
                {
                    //通过挂在到linux的方式,修改文件的操作权限
                    Java.Lang.Process su = Runtime.GetRuntime().Exec("/system/xbin/su");
                    //一般的都是/system/bin/su路径,有的也是/system/xbin/su
                    var cmd = new Java.Lang.String("chmod 777 " + device.AbsolutePath + "\n" + "exit\n");
                    // Log.e("cmd :", cmd);
                    su.OutputStream.Write(cmd.GetBytes());
                    if ((su.WaitFor() != 0) || !device.CanRead() || !device.CanWrite())
                    {
                        throw new SecurityException();
                    }
                }
                catch (Exception e)
                {
                    //   e.printStackTrace();
                    throw new SecurityException();
                }
            }

            var ptr = Open(Android.Runtime.JNIEnv.Handle, this.Handle, JNIEnv.NewString(device.AbsolutePath), baudrate, flags);

            if (ptr != System.IntPtr.Zero)
            {
                mFd = GetObject <FileDescriptor>(ptr, JniHandleOwnership.DoNotRegister);
            }
            if (mFd == null)
            {
                //  Log.e(TAG, "native open returns null");
                throw new IOException();
            }

            mFileInputStream  = new FileInputStream(mFd);
            mFileOutputStream = new FileOutputStream(mFd);
        }
        public static Process doShellCommand(Java.Lang.Process proc, string[] cmds, FFMpegCallbacks sc, bool runAsRoot, bool waitFor)
        {
            var r = Runtime.GetRuntime();

            if (proc == null)
            {
                if (runAsRoot)
                {
                    proc = r.Exec("su");
                }
                else
                {
                    proc = r.Exec("sh");
                }
            }

            OutputStreamWriter outputStream = new OutputStreamWriter(proc.OutputStream);

            for (int i = 0; i < cmds.Length; i++)
            {
                logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);

                outputStream.Write(cmds[i]);
                outputStream.Write("\n");
            }

            outputStream.Flush();
            outputStream.Write("exit\n");
            outputStream.Flush();

            if (waitFor)
            {
                char[] buf = new char[20];

                // Consume the "stdout"
                InputStreamReader reader = new InputStreamReader(proc.InputStream);
                int read = 0;
                while ((read = reader.Read(buf)) != -1)
                {
                    if (sc != null)
                    {
                        sc.ShellOut(new string(buf));
                    }
                }

                // Consume the "stderr"
                reader = new InputStreamReader(proc.ErrorStream);
                read   = 0;
                while ((read = reader.Read(buf)) != -1)
                {
                    if (sc != null)
                    {
                        sc.ShellOut(new string(buf));
                    }
                }

                proc.WaitFor();
            }

            sc.ProcessComplete(proc.ExitValue());

            return(proc);
        }
        public void RebootMachine()
        {
            //try
            //{
            //    Permission permissionCheck = ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.Reboot);

            //    if (permissionCheck != Permission.Granted)
            //    {
            //        // Android.Support.V4.App.ActivityCompat.RequestPermissions(Xamarin.Forms.Platform.Android.FormsApplicationActivity, new string[] { Manifest.Permission.Reboot },  0);
            //        Android.Support.V4.App.ActivityCompat.RequestPermissions(globalVariables.activity,
            //   new string[] { Manifest.Permission.Reboot },
            //   1);
            //    }
            //    else
            //    {
            //        //TODO
            //    }
            //    try
            //    {
            //        Runtime.GetRuntime().Exec(new string[] { "su", "-c", "am force-stop com.android.launcher" });
            //    }
            //    catch (System.Exception e)
            //    {
            //        e = e;
            //        //do something
            //    }
            //    PowerManager pm = (PowerManager)Forms.Context.GetSystemService(Context.PowerService);

            //    pm.Reboot(null);
            //}
            //catch (System.Exception ex)
            //{
            //    ex = ex;
            //}
            //try
            //{
            //    Runtime.GetRuntime().Exec(new string[] { "/system/bin/su", "-c", "reboot now" });
            //}
            //catch (System.Exception ex)
            //{
            //    ex = ex;
            //}
            //try
            //{
            //    Runtime.GetRuntime().Exec(new string[] { "su", "-c", "reboot now" });
            //}
            //catch (System.Exception ex)
            //{
            //    ex = ex;
            //}

            //  Android.OS.Process.KillProcess(Android.OS.Process.MyPid());

            string command = "adb reboot";

            try
            {
                Java.Lang.Process sh            = Runtime.GetRuntime().Exec("su", null, null);
                System.IO.Stream  os            = sh.OutputStream;
                byte[]            mScreenBuffer = Encoding.Unicode.GetBytes(command);
                os.Write(mScreenBuffer);
                os.Flush();
                os.Close();
                sh.WaitFor();
                Log.Verbose("PPX", "comple");
            }
            catch (Java.Lang.Exception e)
            {
                e.PrintStackTrace();
            }
        }