1
0
mirror of https://github.com/DarthAffe/OBD.NET.git synced 2025-12-12 16:58:30 +00:00

first draft of uwp bluetooth serial connection (async)

This commit is contained in:
Roman Lumetsberger 2017-05-07 16:23:34 +02:00
parent 771ade2054
commit 2eef2f5e88
5 changed files with 371 additions and 0 deletions

View File

@ -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();
/// <summary>
/// Gets a value indicating whether this connection is open.
/// </summary>
/// <value>
/// <c>true</c> if this connection is open; otherwise, <c>false</c>.
/// </value>
public bool IsOpen { get; private set; }
public event EventHandler<string> 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();
}
}
}

View File

@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E0EAFF82-C514-4827-8F49-F1928EBA8E73}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OBD.NET.Universal</RootNamespace>
<AssemblyName>OBD.NET.Universal</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.15063.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10586.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="Communication\BluetoothSerialConnection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\OBD.NET.Universal.rd.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>5.2.3</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OBD.NET.Common\OBD.NET.Common.csproj">
<Project>{d985b70e-cdf3-4cf1-ab5d-8d19c7fe7b31}</Project>
<Name>OBD.NET.Common</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -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)]

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains Runtime Directives, specifications about types your application accesses
through reflection and other dynamic code patterns. Runtime Directives are used to control the
.NET Native optimizer and ensure that it does not remove code accessed by your library. If your
library does not do any reflection, then you generally do not need to edit this file. However,
if your library reflects over types, especially types passed to it or derived from its types,
then you should write Runtime Directives.
The most common use of reflection in libraries is to discover information about types passed
to the library. Runtime Directives have three ways to express requirements on types passed to
your library.
1. Parameter, GenericParameter, TypeParameter, TypeEnumerableParameter
Use these directives to reflect over types passed as a parameter.
2. SubTypes
Use a SubTypes directive to reflect over types derived from another type.
3. AttributeImplies
Use an AttributeImplies directive to indicate that your library needs to reflect over
types or methods decorated with an attribute.
For more information on writing Runtime Directives for libraries, please visit
https://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="OBD.NET.Universal">
<!-- add directives for your library here -->
</Library>
</Directives>

View File

@ -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