diff --git a/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs new file mode 100644 index 0000000..65eef74 --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Communication/BluetoothSerialConnection.cs @@ -0,0 +1,126 @@ +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Windows.Devices.Bluetooth.Rfcomm; +using Windows.Networking.Sockets; +using Windows.Storage.Streams; + +namespace OBD.NET.Communication +{ + public class BluetoothSerialConnection : ISerialConnection + { + + private StreamSocket _socket; + private DataReader _reader; + private DataWriter _writer; + + private readonly byte[] _readBuffer = new byte[1024]; + private readonly StringBuilder _lineBuffer = new StringBuilder(); + + /// + /// Gets a value indicating whether this connection is open. + /// + /// + /// true if this connection is open; otherwise, false. + /// + public bool IsOpen { get; private set; } + + + public event EventHandler MessageReceived; + + public void Connect() + { + throw new NotSupportedException(); + } + + public async Task ConnectAsync() + { + var services = await Windows.Devices.Enumeration.DeviceInformation + .FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort)); + + if (services.Count > 0) + { + // Initialize the target Bluetooth BR device + var service = await RfcommDeviceService.FromIdAsync(services[0].Id); + + // Check that the service meets this App's minimum requirement + + _socket = new StreamSocket(); + await _socket.ConnectAsync(service.ConnectionHostName, + service.ConnectionServiceName); + _writer = new DataWriter(_socket.OutputStream); + StartReader(); + + + } + } + + private void StartReader() + { + Task.Factory.StartNew(async () => + { + _reader = new DataReader(_socket.InputStream); + while (true) + { + await _reader.LoadAsync(1); + var data = _reader.ReadByte(); + _readBuffer[0] = data; + SerialPortOnDataReceived(); + } + + }); + } + + private void SerialPortOnDataReceived() + { + int count = 1; + for (int i = 0; i < count; i++) + { + char c = (char)_readBuffer[i]; + switch (c) + { + case '\r': + FinishLine(); + break; + + case '>': + continue; //ignore + + case '\n': + case (char)0x00: + break; // ignore + + default: + _lineBuffer.Append(c); + break; + } + } + } + + private void FinishLine() + { + string line = _lineBuffer.ToString(); + _lineBuffer.Clear(); + + MessageReceived?.Invoke(this, line); + } + + public void Dispose() + { + _socket?.Dispose(); + } + + public void Write(string text) + { + throw new NotImplementedException(); + } + + public async Task WriteAsync(string text) + { + _writer.WriteString(text); + await _writer.StoreAsync(); + await _writer.FlushAsync(); + } + } +} diff --git a/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj new file mode 100644 index 0000000..9848fca --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/OBD.NET.Universal.csproj @@ -0,0 +1,135 @@ + + + + + Debug + AnyCPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73} + Library + Properties + OBD.NET.Universal + OBD.NET.Universal + en-US + UAP + 10.0.15063.0 + 10.0.10586.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + x86 + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + + + x86 + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + + + ARM + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + + + ARM + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + + + x64 + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + + + x64 + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + + + PackageReference + + + + + + + + + 5.2.3 + + + + + {d985b70e-cdf3-4cf1-ab5d-8d19c7fe7b31} + OBD.NET.Common + + + + 14.0 + + + + \ No newline at end of file diff --git a/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs b/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..37c5a63 --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Properties/AssemblyInfo.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("OBD.NET.Universal")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("OBD.NET.Universal")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml b/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml new file mode 100644 index 0000000..874b02e --- /dev/null +++ b/OBD.NET/OBD.NET.Universal/Properties/OBD.NET.Universal.rd.xml @@ -0,0 +1,33 @@ + + + + + + + + + diff --git a/OBD.NET/OBD.NET.sln b/OBD.NET/OBD.NET.sln index 5ffecc7..67dbd52 100644 --- a/OBD.NET/OBD.NET.sln +++ b/OBD.NET/OBD.NET.sln @@ -7,20 +7,68 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OBD.NET.Common", "OBD.NET.C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODB.NET.Desktop", "ODB.NET.Desktop\ODB.NET.Desktop.csproj", "{14CB98E1-95DE-4923-8896-FDF5171AA49E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OBD.NET.Universal", "OBD.NET.Universal\OBD.NET.Universal.csproj", "{E0EAFF82-C514-4827-8F49-F1928EBA8E73}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|ARM = Debug|ARM + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|ARM = Release|ARM + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|ARM.ActiveCfg = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|ARM.Build.0 = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|x64.ActiveCfg = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|x64.Build.0 = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|x86.ActiveCfg = Debug|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Debug|x86.Build.0 = Debug|Any CPU {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|Any CPU.ActiveCfg = Release|Any CPU {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|Any CPU.Build.0 = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|ARM.ActiveCfg = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|ARM.Build.0 = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|x64.ActiveCfg = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|x64.Build.0 = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|x86.ActiveCfg = Release|Any CPU + {D985B70E-CDF3-4CF1-AB5D-8D19C7FE7B31}.Release|x86.Build.0 = Release|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|ARM.ActiveCfg = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|ARM.Build.0 = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|x64.ActiveCfg = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|x64.Build.0 = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|x86.ActiveCfg = Debug|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Debug|x86.Build.0 = Debug|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|Any CPU.ActiveCfg = Release|Any CPU {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|Any CPU.Build.0 = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|ARM.ActiveCfg = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|ARM.Build.0 = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x64.ActiveCfg = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x64.Build.0 = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x86.ActiveCfg = Release|Any CPU + {14CB98E1-95DE-4923-8896-FDF5171AA49E}.Release|x86.Build.0 = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.ActiveCfg = Debug|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|ARM.Build.0 = Debug|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.ActiveCfg = Debug|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x64.Build.0 = Debug|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.ActiveCfg = Debug|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Debug|x86.Build.0 = Debug|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|Any CPU.Build.0 = Release|Any CPU + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.ActiveCfg = Release|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|ARM.Build.0 = Release|ARM + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.ActiveCfg = Release|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x64.Build.0 = Release|x64 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.ActiveCfg = Release|x86 + {E0EAFF82-C514-4827-8F49-F1928EBA8E73}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE