First, thank you for reading my Post. I am currently working in my Thesis and need to implement a WPF-Application, which can send Data to a Android Device on the Public Folder. The app communicates with a App on the Android Device(exactly a Scanner). A Part of the WPF already existed, the WPF can read the Data, which are on the Scanners public folder stored. It is using the https://github.com/jtorjo/external_drive_lib. And i need help on how i can create a Method to send (Create) and update the Data on the PC, which i am inputing on the WPF with textboxes. So that they get stored on the Android Device and are visble. Here is some Code from a AndroidDeviceService Class, which only reads the Data from the public folder:
//Gets all the connected android devices
private List<AndroidDevice> GetAndroidDevices()
{
List<IDrive> androidDrives = drive_root.inst.drives.Where(d => d.type.is_portable() && d.type.is_android() && d.is_connected()).ToList();
List<AndroidDevice> scannerAppDevices = new List<AndroidDevice>();
foreach (var drive in androidDrives)
{
scannerAppDevices.Add(new AndroidDevice(drive));
}
return scannerAppDevices;
}
//Tries to get a folder on the drive or returns null
public IFolder TryToGetFolder(IDrive drive, string folderPath)
{
if (drive.is_connected())
{
try
{
return drive.try_parse_folder(folderPath);
}
catch
{
return null;
}
}
else
return null;
}
//Tries to get a file in a folder or returns null
public IFile TryToGetFile(IFolder folder, string filename)
{
return folder.files.Where(f => f.name == filename).FirstOrDefault();
}
//Tries to read the text of a file or returns null
public string TryToGetFileText(IFile file)
{
//Uses the tempDir to copy the file to it
string tempDir = _tempDir + "temp-" + DateTime.Now.Ticks;
Directory.CreateDirectory(tempDir);
file.copy_sync(tempDir);
//Read file from tempDir
StreamReader reader = new StreamReader(tempDir + "\\" + file.name);
return reader.ReadToEnd();
}
//Tries to delete a file
public void TryToDeleteFile(IFile file)
{
if(file != null)
{
if (file.exists)
file.delete_sync();
}
}
Then i am using the methods in the ScannerAppCommunicationService:
/*
* Takes an AndroidDevice, gets the communication file, deserialize it and returns a List of CWCProducts
*/
public List<CWCProduct> GetProductsFromScanner(AndroidDevice scannerAppDevice)
{
IFile file = GetScannerAppFile(scannerAppDevice, CWC_FILENAME);
if (file == null)
throw new NoFileOnScannerException("No File, therefore no Products on the Scanner!");
try
{
string fileText = _deviceService.TryToGetFileText(file);
CWCFile cwcFile = Utilities.JsonDeserialize<CWCFile>(fileText);
return cwcFile.products.ToCWCProducts();
}
catch
{
throw new FileDecodeException($"Decoding Text from {CWC_FILENAME} File failed!");
}
}
//Deletes the communication file from the AndroidDevice
public void DeleteProductsOnScanner(AndroidDevice scannerAppDevice)
{
IFile file = GetScannerAppFile(scannerAppDevice, CWC_FILENAME);
if (file == null)
throw new NoFileOnScannerException("No File, therefore no Products on the Scanner!");
_deviceService.TryToDeleteFile(file);
}
//Gets a file from the public ScannerApp folder on the AndroidDevice
private IFile GetScannerAppFile(AndroidDevice scannerAppDevice, string filename)
{
IFolder folder = _deviceService.TryToGetFolder(scannerAppDevice.Drive, SCANNERAPP_FOLDERNAME);
if (folder == null)
throw new ScannerAppNotFoundOnDeviceException("ScannerApp not found on this device or the device has been disconnected!");
return _deviceService.TryToGetFile(folder, filename);
}
I already tried some Methods:
`//Tries to Send a file from the WPF to the Android Device
public bool SendFileToAndroid(AndroidDevice device, string filePath, string destinationFolder)
{
if (!device.Drive.is_connected())
return false;
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "adb",
Arguments = $"push {filePath} {destinationFolder}",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
process.WaitForExit();
return true;
}
catch
{
return false;
}
}
//Trys to Update a File on the Android Advice
public bool TryToUpdateFile(AndroidDevice scannerAppDevice, string destinationFolder)
{
string filePath = "";
IFolder folder = scannerAppDevice.Drive.try_parse_folder(filePath);
IFile file = TryToGetFile(folder, filePath);
if (file != null)
{
//Delete the old file
TryToDeleteFile(file);
}
//Send the new file
return SendFileToAndroid(scannerAppDevice, filePath, destinationFolder);
}`
//Creates a File in the Public Folder of the Adroid Advice
public void CreateFileInPublicFolder(AndroidDevice scannerAppDevice, object data)
{
// Serialize the data to json
string jsonData = Utilities.JsonSerialize(data);
// Create a temporary file to store the json data
string tempFilePath = Path.GetTempPath() + "tempJsonFile.json";
File.WriteAllText(tempFilePath, jsonData);
// Set the destination folder for the file
string destinationFolder = SCANNERAPP_FOLDERNAME;
// Send the file to the android device
if (!_deviceService.SendFileToAndroid(scannerAppDevice, tempFilePath, destinationFolder))
{
throw new FileTransferException("Error while trying to transfer the file to the android device");
}
// Delete the temporary file
File.Delete(tempFilePath);
}
public void UpdateFileOnScanner(AndroidDevice scannerAppDevice, string newFileContent)
{
IFile file = GetScannerAppFile(scannerAppDevice, CWC_FILENAME);
if (file == null)
throw new NoFileOnScannerException("File not found on the scanner!");
try
{
_deviceService.TryToUpdateFile(scannerAppDevice, newFileContent);
}
catch
{
throw new FileUpdateException($"Failed to update file {CWC_FILENAME} on the scanner!");
}
}