'Can I run an adb shell command in C# and not have it interrupted when I unplug the USB cable?

I'm running adb shell commands using the method described below

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + cmd);
procStartInfo.RedirectStandardInput = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

On the android phone I'm starting an app that records a video so cmd = "adb shell am startservice -n com.xxx.xxx/.xxx".

The problem is that I need to unplug the USB cable after I start recording video. If I do that, the resulting video file won't play. Sometimes it's 0KB. If I leave the USB cable connected, then the video is fine.

Is there a way to perform the adb command so that the video will continue recording after I unplug the USB cable? Everything is fine when I enter the command from the command prompt and unplug the USB cable.



Solution 1:[1]

Try the following instead:

string cmd = " shell am startservice -n com.xxx.xxx/.xxx";
using (Process proc = new process())
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
procStartInfo.RedirectStandardInput = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
proc.StandardInput.Writeline("adb devices");
proc.StandardInput.Writeline("adb shell getprop dhcp.wlan0.ipaddress"); // this will not work on some newer os
string deviceip = proc.StandardOutput.Readline();
proc.StandardInput.Writeline("adb tcpip 5555"); // this will turn on ADB to wifi on port 5555
proc.StandardInput.Writeline("adb connect " + deviceip + " 5555"); // this allows you to at this point unplug your device
proc.StandardInput.Writeline("adb -s " + deviceip + ":5555" +  cmd);  // this connects running the command on the specific ip address so when you disconnect usb it will continue creating movie
proc.WaitForExit();
} // this might not be correct wait command

My primary language i use is Visual Basic, So this might need a little work to get working correctly

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1