public async Task <ICommsSerial> GetUSB(DeviceInfo di) { var usbManager = (UsbManager)Application.Context.GetSystemService(Context.UsbService); foreach (var deviceListValue in usbManager.DeviceList.Values) { Log.Info(TAG, deviceListValue.ToJSON()); } Log.Info(TAG, "Refreshing device list ..."); var drivers = await AndroidSerialBase.GetPorts(usbManager); if (drivers.Count == 0) { Log.Info(TAG, "No usb devices"); return(null); } foreach (var driver in drivers) { Log.Info(TAG, string.Format("+ {0}: {1} port{2}", driver, drivers.Count, drivers.Count == 1 ? string.Empty : "s")); Log.Info(TAG, string.Format("+ {0}: {1} ", driver.Device.ProductName, driver.Device.ManufacturerName)); } var usbdevice = drivers.First(a => di.hardwareid.Contains(a.Device.VendorId.ToString("X4")) && di.hardwareid.Contains(a.Device.ProductId.ToString("X4"))); var permissionGranted = await usbManager.RequestPermissionAsync(usbdevice.Device, Application.Context); if (permissionGranted) { var portInfo = new UsbSerialPortInfo(drivers.First().Ports.First()); int vendorId = portInfo.VendorId; int deviceId = portInfo.DeviceId; int portNumber = portInfo.PortNumber; Log.Info(TAG, string.Format("VendorId: {0} DeviceId: {1} PortNumber: {2}", vendorId, deviceId, portNumber)); var driver = drivers.Where((d) => d.Device.VendorId == vendorId && d.Device.DeviceId == deviceId).FirstOrDefault(); var port = driver.Ports[portNumber]; var serialIoManager = new SerialInputOutputManager(usbManager, port) { BaudRate = 57600, DataBits = 8, StopBits = StopBits.One, Parity = Parity.None, }; return(new AndroidSerial(serialIoManager)); } return(null); }
public static async Task <IUsbSerialDriver> GetSpecificDriverAsync( UsbManager usbManager, UsbSerialPortInfo portInfo) { var drivers = await FindAllDriversAsync(usbManager); var driver = drivers.FirstOrDefault(d => d.Device.VendorId == portInfo.VendorId && d.Device.DeviceId == portInfo.DeviceId); return(driver); }
public async Task <ICommsSerial> GetUSB(DeviceInfo di) { var usbManager = (UsbManager)Application.Context.GetSystemService(Context.UsbService); foreach (var deviceListValue in usbManager.DeviceList.Values) { Log.Info(TAG, "GetUSB " + deviceListValue.DeviceName); } Log.Info(TAG, "GetUSB " + "Refreshing device list ..."); var drivers = await AndroidSerialBase.GetPorts(usbManager); if (drivers.Count == 0) { Log.Info(TAG, "GetUSB " + "No usb devices"); return(null); } foreach (var driver in drivers.ToArray()) { Log.Info(TAG, string.Format("GetUSB " + "+ {0}: {1} ports {2}", driver, drivers.Count, driver.Ports.Count)); Log.Info(TAG, string.Format("GetUSB " + "+ {0}: {1} ", driver.Device.ProductName, driver.Device.ManufacturerName)); } var usbdevice = drivers.First(a => di.hardwareid.Contains(a.Device.VendorId.ToString("X4")) && di.hardwareid.Contains(a.Device.ProductId.ToString("X4"))); var hasPermission = usbManager.HasPermission(usbdevice.Device); var permissionGranted = await usbManager.RequestPermissionAsync(usbdevice.Device, Application.Context); if (permissionGranted) { if (!hasPermission) { return(await GetUSB(di)); } var portnumber = 0; var port = usbdevice.Ports.First(); if (usbdevice.Ports.Count > 1) { if (di.board.EndsWith("-P2")) { port = usbdevice.Ports[1]; portnumber = 1; } } /* * var defaultport = usbdevice.Ports.First(); * if (usbdevice.Ports.Count > 1) * { * ManualResetEvent mre = new ManualResetEvent(false); * * var handler = new Handler(MainActivity.Current.MainLooper); * * handler.Post(() => * { * AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.Current); * alert.SetTitle("Multiple Ports"); * alert.SetCancelable(false); * var items = usbdevice.Ports.Select(a => * a.Device.GetInterface(a.PortNumber).Name ?? a.PortNumber.ToString()) * .ToArray(); * alert.SetSingleChoiceItems(items, 0, (sender, args) => * { * defaultport = usbdevice.Ports[args.Which]; * }); * * alert.SetNeutralButton("OK", (senderAlert, args) => { mre.Set(); }); * * Dialog dialog = alert.Create(); * if(!MainActivity.Current.IsFinishing) * dialog.Show(); * }); * * mre.WaitOne(); * } */ var portInfo = new UsbSerialPortInfo(port); int vendorId = portInfo.VendorId; int deviceId = portInfo.DeviceId; Log.Info(TAG, string.Format("GetUSB " + "VendorId: {0} DeviceId: {1} PortNumber: {2}", vendorId, deviceId, portnumber)); var serialIoManager = new SerialInputOutputManager(usbManager, port); return(new AndroidSerial(serialIoManager) { PortName = usbdevice.Device.ProductName }); } return(null); }