public static bool InterfaceConnect(string port, object parameter) { if (TcpElmClient != null) { return(true); } try { ConnectPort = port; ConnectParameter = parameter; #if Android if (ConnectParameter is ConnectParameterType connectParameter) { ConnManager = connectParameter.ConnectivityManager; } #endif TcpClientWithTimeout.ExecuteNetworkCommand(() => { TcpElmClient = new TcpClientWithTimeout(IPAddress.Parse(ElmIp), ElmPort, ConnectTimeout, true).Connect(); }, ConnManager); TcpElmStream = TcpElmClient.GetStream(); _edElmInterface = new EdElmInterface(Ediabas, TcpElmStream, TcpElmStream); if (!_edElmInterface.Elm327Init()) { InterfaceDisconnect(); return(false); } } catch (Exception) { InterfaceDisconnect(); return(false); } return(true); }
public static bool InterfaceDisconnect() { bool result = true; if (_edElmInterface != null) { _edElmInterface.Dispose(); _edElmInterface = null; } try { if (_bluetoothInStream != null) { _bluetoothInStream.Close(); _bluetoothInStream = null; } } catch (Exception) { result = false; } try { if (_bluetoothOutStream != null) { _bluetoothOutStream.Close(); _bluetoothOutStream = null; } } catch (Exception) { result = false; } try { if (_bluetoothSocket != null) { _bluetoothSocket.Close(); _bluetoothSocket = null; } } catch (Exception) { result = false; } return(result); }
public static bool InterfaceDisconnect() { bool result = true; if (_edElmInterface != null) { _edElmInterface.Dispose(); _edElmInterface = null; } try { if (TcpElmStream != null) { TcpElmStream.Close(); TcpElmStream = null; } } catch (Exception) { result = false; } try { if (TcpElmClient != null) { TcpElmClient.Close(); TcpElmClient = null; } } catch (Exception) { result = false; } return(result); }
public static bool InterfaceConnect(string port, object parameter) { if (_bluetoothSocket != null) { return(true); } FastInit = false; ConvertBaudResponse = false; AutoKeyByteResponse = false; AdapterType = -1; AdapterVersion = -1; LastCommTick = DateTime.MinValue.Ticks; if (!port.StartsWith(PortId, StringComparison.OrdinalIgnoreCase)) { InterfaceDisconnect(); return(false); } BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter; if (bluetoothAdapter == null) { return(false); } _rawMode = false; _elm327Device = false; _connectPort = port; _reconnectRequired = false; try { BluetoothDevice device; string portData = port.Remove(0, PortId.Length); if ((portData.Length > 0) && (portData[0] == ':')) { // special id string addr = portData.Remove(0, 1); string[] stringList = addr.Split('#', ';'); if (stringList.Length == 0) { InterfaceDisconnect(); return(false); } device = bluetoothAdapter.GetRemoteDevice(stringList[0]); if (stringList.Length > 1) { if (string.Compare(stringList[1], Elm327Tag, StringComparison.OrdinalIgnoreCase) == 0) { _elm327Device = true; } else if (string.Compare(stringList[1], RawTag, StringComparison.OrdinalIgnoreCase) == 0) { _rawMode = true; } } } else { InterfaceDisconnect(); return(false); } if (device == null) { InterfaceDisconnect(); return(false); } bluetoothAdapter.CancelDiscovery(); _bluetoothSocket = device.CreateRfcommSocketToServiceRecord(SppUuid); try { _bluetoothSocket.Connect(); } catch (Exception) { try { // sometimes the second connect is working _bluetoothSocket.Connect(); } catch (Exception) { _bluetoothSocket.Close(); _bluetoothSocket = null; } } if (_bluetoothSocket == null) { // this socket sometimes looses data for long telegrams IntPtr createRfcommSocket = Android.Runtime.JNIEnv.GetMethodID(device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;"); if (createRfcommSocket == IntPtr.Zero) { throw new Exception("No createRfcommSocket"); } IntPtr rfCommSocket = Android.Runtime.JNIEnv.CallObjectMethod(device.Handle, createRfcommSocket, new Android.Runtime.JValue(1)); if (rfCommSocket == IntPtr.Zero) { throw new Exception("No rfCommSocket"); } _bluetoothSocket = Java.Lang.Object.GetObject <BluetoothSocket>(rfCommSocket, Android.Runtime.JniHandleOwnership.TransferLocalRef); _bluetoothSocket.Connect(); } Thread.Sleep(500); _bluetoothInStream = _bluetoothSocket.InputStream; _bluetoothOutStream = _bluetoothSocket.OutputStream; if (_elm327Device) { _edElmInterface = new EdElmInterface(Ediabas, _bluetoothInStream, _bluetoothOutStream); if (!_edElmInterface.Elm327Init()) { InterfaceDisconnect(); return(false); } } } catch (Exception) { InterfaceDisconnect(); return(false); } return(true); }