1

I am studying android and .NET

I would like to use VB.NET to execute adb command

For example, I would like to enable or disable airplane mode from PC to Android phone

such as

adb shell settings put global airplane_mode_on 1 adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

Dim psi As New ProcessStartInfo
Dim psi2 As New ProcessStartInfo
psi.WorkingDirectory = "C:\ad\adsystem\adb file"
psi.Arguments = "adb shell settings put global airplane_mode_on 0"
psi.FileName = "adb"
psi.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(psi)

psi2.WorkingDirectory = "C:\ad\adsystem\adb file"
psi2.Arguments = "adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false"
psi2.FileName = "adb"
psi2.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(psi)

I tried to use example from

How to get Output of a Command Prompt Window line by line in Visual Basic?

Shell commands in VB

However, It didn't really work and i am not really sure why ....

is there anyway to run adb command from VB.NET?

thanks

Community
  • 1
  • 1
Bob Ma
  • 197
  • 5
  • 11
  • 22
  • for starters `.Arguments` should not include the `.FileName`. the command you are trying to run is `adb adb shell ...` – Alex P. Jan 31 '15 at 11:08
  • I have a question. Can we do like Shell("cmd.exe /c cd adb shell settings put global airplane_mode_on 0", AppWinStyle.Hide). Like this ? – Dc Redwing Jan 31 '15 at 11:31

1 Answers1

0

You can use cmd.exe to communicate with adb commands see my code example.

Declaring new process and process info

Dim My_Process As New Process()
Dim My_Process_Info As New ProcessStartInfo()

Using cmd.exe as the filename of my process.

My_Process_Info.FileName = "cmd.exe"

in argument you can use adb commands like this.

My_Process_Info.Arguments = "/c adb devices"

/c Carries out the command specified by string and then terminates.

Now set WorkingDirectory for adb.exe in cmd.exe. Don't confuse, it is same as cd command. Setting our working Directory to where your adb.exe file exists.

My_Process_Info.WorkingDirectory = "Directory of your adb.exe file"

Now some necessary settings for process.

My_Process_Info.CreateNoWindow = True  ' Show or hide the process Window
My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
My_Process_Info.RedirectStandardOutput = True  '  Redirect (1) Output
My_Process_Info.RedirectStandardError = True  ' Redirect non (1) Output
My_Process.EnableRaisingEvents = True ' Raise events
My_Process.StartInfo = My_Process_Info

Setting completed, Now start the process.

My_Process.Start()

If you want to get the reply/result of your sended adb command you can use StandardOutput property of the process.

Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)

Example Function:

Function adb(ByVal Arguments As String) As String
    Try

        Dim My_Process As New Process()
        Dim My_Process_Info As New ProcessStartInfo()

        My_Process_Info.FileName = "cmd.exe" ' Process filename
        My_Process_Info.Arguments = Arguments ' Process arguments
        My_Process_Info.WorkingDirectory = "C:\Users\<Your User Name>\AppData\Local\Android\android-sdk\platform-tools" 'this directory can be different in your case.
        My_Process_Info.CreateNoWindow = True  ' Show or hide the process Window
        My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
        My_Process_Info.RedirectStandardOutput = True  '  Redirect (1) Output
        My_Process_Info.RedirectStandardError = True  ' Redirect non (1) Output

        My_Process.EnableRaisingEvents = True ' Raise events
        My_Process.StartInfo = My_Process_Info
        My_Process.Start() ' Run the process NOW

        Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
        Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)

        ' Return output by priority
        If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput ' Returns the ErrorOutput (if any)
        If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput ' Returns the StandardOutput (if any)

    Catch ex As Exception
        Return ex.Message
    End Try

    Return "OK"

End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)     Handles Button1.Click

    'Usage:

    'Get the list of connected devices.
    MsgBox(adb("/c adb devices")) 

    'Connect your phone wirelessly using wifi (required phone I.P)
    MsgBox(adb("/c adb disconnect 192.168.xx.xx:5555")) 

    'Get the list of connected devices.
    MsgBox(adb("/c adb devices"))

    'Put your phone on airplane mode. 
    MsgBox(adb("/c adb shell settings put global airplane_mode_on 1"))

End Sub
Muhammad Saqib
  • 2,185
  • 3
  • 35
  • 48