protected static void ExecuteNetworkCommand(ExecuteNetworkDelegate command) { #if Android if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop) { command(); return; } if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M) { #pragma warning disable 618 Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork; try { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(null); command(); } finally { Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork); } #pragma warning restore 618 return; } ConnectParameterType connectParameter = ConnectParameter as ConnectParameterType; if (connectParameter == null) { throw new Exception("No connect parameter"); } Android.Net.Network boundNetwork = connectParameter.ConnectivityManager.BoundNetworkForProcess; try { connectParameter.ConnectivityManager.BindProcessToNetwork(null); command(); } finally { connectParameter.ConnectivityManager.BindProcessToNetwork(boundNetwork); } #else command(); #endif }
public static bool InterfaceConnect(string port, object parameter) { if (_usbPort != null) { return(true); } try { ConnectParameterType connectParameter = parameter as ConnectParameterType; if (connectParameter == null) { return(false); } if (!port.StartsWith(PortId, StringComparison.OrdinalIgnoreCase)) { InterfaceDisconnect(); return(false); } List <IUsbSerialDriver> availableDrivers = GetDriverList(connectParameter.UsbManager); if (availableDrivers.Count <= 0) { InterfaceDisconnect(); return(false); } string portData = port.Remove(0, PortId.Length); int portIndex = -1; if ((portData.Length > 0) && (portData[0] == ':')) { // special id if (portData.StartsWith(":SER=", StringComparison.OrdinalIgnoreCase)) { // serial number string id = portData.Remove(0, 5); int index = 0; foreach (IUsbSerialDriver serialDriver in availableDrivers) { if (serialDriver.Ports[0] != null && string.Compare(serialDriver.Ports[0].Serial, id, StringComparison.Ordinal) == 0) { portIndex = index; break; } index++; } } } else { portIndex = Convert.ToInt32(port.Remove(0, PortId.Length)); } if ((portIndex < 0) || (portIndex >= availableDrivers.Count)) { InterfaceDisconnect(); return(false); } IUsbSerialDriver driver = availableDrivers[portIndex]; UsbDeviceConnection connection = connectParameter.UsbManager.OpenDevice(driver.Device); if (connection == null) { InterfaceDisconnect(); return(false); } if (driver.Ports.Count < 1) { InterfaceDisconnect(); return(false); } _usbPort = driver.Ports[0]; _usbPort.Open(connection); _usbPort.SetParameters(9600, 8, StopBits.One, Parity.None); FtdiSerialDriver.FtdiSerialPort ftdiPort = _usbPort as FtdiSerialDriver.FtdiSerialPort; if (ftdiPort != null) { ftdiPort.LatencyTimer = LatencyTime; if (ftdiPort.LatencyTimer != LatencyTime) { InterfaceDisconnect(); return(false); } } _currentWordLength = 8; _currentParity = EdInterfaceObd.SerialParity.None; _usbPort.DTR = false; _usbPort.RTS = false; lock (QueueLock) { ReadQueue.Clear(); } _serialIoManager = new SerialInputOutputManager(_usbPort); _serialIoManager.DataReceived += (sender, e) => { lock (QueueLock) { foreach (byte value in e.Data) { ReadQueue.Enqueue(value); } DataReceiveEvent.Set(); } }; _serialIoManager.Start(UsbBlockSize); } catch (Exception) { InterfaceDisconnect(); return(false); } return(true); }