3 Replies - 979 Views - Last Post: 05 October 2017 - 06:43 AM Rate Topic: *---- 1 Votes

#1 claudiohi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 17

Trying to use a string to adb command line

Posted 04 October 2017 - 05:49 PM

Hello everyone, I kindly ask for an orientation in my code, which is done in C # with Android Debug Bridge (ADB) commands that run at a DOS prompt.

The command basically reads the serial number from my tablet and transfers it to a textBox. Until then, no problem. However I need to copy this serial number from this textBox and transfer it to a new ADB command in DOS to read the battery data. Below is the code for better understanding, the first block is to get the serial number and transfer it to textBox1:

{
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.FileName = "adb.exe";
            startInfo.Arguments = "get-serialno";
            process.StartInfo = startInfo;
            process.Start();
            string output1 = process.StandardOutput.ReadToEnd();
            textBox1.Text = output1;
            process.WaitForExit();
        }


And the second block is the code that will direct the commands to the serial number:

startInfo.FileName = "adb.exe";
                startInfo.Arguments = "-s " + textBox1.Text + " shell dumpsys battery";
                process.StartInfo = startInfo;
                process.Start();
                string output = process.StandardOutput.ReadToEnd();
                richTextBox1.Text = output;
                process.WaitForExit();


But I can not get it to recognize the variable in the argument line. I've tried to concatenate, without success ... where am I going wrong? What I've already tried, in the searches I've done:

startInfo.Arguments = String.Format(@"/c adb -s ""{0}"" shell dumpsys battery", output1);
 
startInfo.Arguments = "/c adb -s \"" + output1 + "\" shell dumpsys battery";
 
startInfo.Arguments = $@"/c adb -s ""{output1}"" shell dumpsys battery";


Thanks a lot!

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to use a string to adb command line

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,112
  • Joined: 05-May 12

Re: Trying to use a string to adb command line

Posted 05 October 2017 - 12:56 AM

And when you step through your code, what does the debugger tell you about the value of the variables and then string that you build up?

As an aside, your first approach of using the text box to store the serial number and then later reading from the text box is not the correct modern way to write code. Yes, back in the 80's and 90's, treating the UI as a variable was the way to do things, but not so much anymore. Your other attempts are better where you are accessing a real variable is better.
Was This Post Helpful? 0
  • +
  • -

#3 claudiohi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 17

Re: Trying to use a string to adb command line

Posted 05 October 2017 - 02:36 AM

View PostSkydiver, on 05 October 2017 - 12:56 AM, said:

And when you step through your code, what does the debugger tell you about the value of the variables and then string that you build up?

As an aside, your first approach of using the text box to store the serial number and then later reading from the text box is not the correct modern way to write code. Yes, back in the 80's and 90's, treating the UI as a variable was the way to do things, but not so much anymore. Your other attempts are better where you are accessing a real variable is better.



Hi, thanks for the help.
The debugger shows nothing, because it waits until the process is completed. As a matter of fact, I tried to store the value of the textBox in a string (in this case, output1) but it did not work either.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,112
  • Joined: 05-May 12

Re: Trying to use a string to adb command line

Posted 05 October 2017 - 06:43 AM

Huh? But you would be stepping though with the debuggger and have the value of the string before you call process.Start(). Recall that you said this:

View Postclaudiohi, on 04 October 2017 - 08:49 PM, said:

The command basically reads the serial number from my tablet and transfers it to a textBox. Until then, no problem.


That means that you already have output1. Now you just need to step through with the debugger as you build up this string:
startInfo.Arguments = $@"/c adb -s ""{output1}"" shell dumpsys battery";


prior to calling process.Start() to get the battery level.

This maybe redundant, but I will make it just to be clear: Step through the relevant code. Don't just run the code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1