I'm trying to get information about the USB Devices connected to my MacBook Pro Mid 2012 using MAUI .NET. Specifically the VID and PID of the device.
More details about my project:
- Code Editor - Visual Studio Community 2022
- Programming Language - C#
- Framework - MAUI .NET (Cross-platform)
- Physical Machine - MacBook Pro Mid 2012
- Operating System - MacOS Monterey
How can I accomplish what I'm trying to do?
Thank you
CodePudding user response:
I found a workaround.
Here are the steps:
- Write a shell script which gets all USB devices and filters according to PID and VID of the device that you want. You can also make the script filter according to name. You can get the script at the following link - https://gist.github.com/song940/7149576#file-lsusb-sh
You might need to edit it, in order to filter to your specific needs. You need to modify the first script (lsusb.sh) to return the BSD Name. If you want to get the device mount point in the steps ahead.
After editing the script and confirming the it works in the terminal. Place it in the .NET MAUI app inside the "Resources/Raw" folder
Make sure the build properties for the shell file are - Build action: Maui Asset, and Copy to directory: Always copy
Run the script in your app using the following code -
public string GetUsbDeviceInfoString()
{
string AppDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string fullPath = AppDirectory.Replace("MonoBundle", "Resources/lsusb.sh");
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "/bin/bash";
p.StartInfo.Arguments = " -c \"" "sh \'" fullPath "\' \"";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return output;
}
- In order to get the USB mount point (USB Device path) you need to create another script file which executes the command "diskutil info $BsdName | grep "Mount Point:"| awk -F":" '{print $2}'" which returns the path (Mount Point:). Execute this second script by repeating step 4, but change the name to point to that script file (Resources/getmountpoint.sh).
NOTE!!! I found a bug, the script files don't work if you build the project and release it as a package (.pkg). The scripts only work if you build release the app as a .app file.