Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

First implementation of a barcode reader for windows store apps #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.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>
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AAA4065C-46B6-4403-A84A-6D8ACF1534DE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MvvmCross.Plugins.Accelerometer.WindowsCommon</RootNamespace>
<AssemblyName>MvvmCross.Plugins.Accelerometer.WindowsCommon</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile32</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\bin\Debug\Mvx\WindowsCommon\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\Release\Mvx\WindowsCommon\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<TargetPlatform Include="WindowsPhoneApp, Version=8.1" />
<TargetPlatform Include="Windows, Version=8.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="MvxWindowsCommonBarcodeScanner.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MvvmCross.Binding, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Binding.4.1.6\lib\portable-win81+wpa81\MvvmCross.Binding.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Core, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Core.4.1.6\lib\portable-win81+wpa81\MvvmCross.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Localization, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Binding.4.1.6\lib\portable-win81+wpa81\MvvmCross.Localization.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Platform, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Platform.4.1.6\lib\portable-win81+wpa81\MvvmCross.Platform.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.Platform.WindowsCommon, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Platform.4.1.6\lib\portable-win81+wpa81\MvvmCross.Platform.WindowsCommon.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MvvmCross.WindowsCommon, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MvvmCross.Core.4.1.6\lib\portable-win81+wpa81\MvvmCross.WindowsCommon.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MvvmCross.Plugins.BarcodeScanner\MvvmCross.Plugins.BarcodeScanner.csproj">
<Project>{0e10e61c-78b8-437a-b044-6d1886f82ed9}</Project>
<Name>MvvmCross.Plugins.BarcodeScanner</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.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>
@@ -0,0 +1,230 @@
// <copyright file="MvxStoreAccelerometer.cs" company="Cirrious">
// (c) Copyright Cirrious. http://www.cirrious.com
// This source is subject to the Microsoft Public License (Ms-PL)
// Please see license.txt on http://opensource.org/licenses/ms-pl.html
// All other rights reserved.
// </copyright>
//
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com - Hire me - I'm worth it!

using System;
using MvvmCross.Platform.Core;
using MvvmCross.Platform.Exceptions;
using Windows.Devices.Sensors;
//using Windows.Devices.PointOfService = POS;


namespace MvvmCross.Plugins.BarcodeScanner.WindowsStore {
public class MvxBarcodeScanner : IMvxBarcodeScanner {

private MvxBarcodeScannerReading _lastReading;

private bool _started;
private Windows.Devices.PointOfService.BarcodeScanner _barcodeScanner;
private Windows.Devices.PointOfService.ClaimedBarcodeScanner _claimedBarcodeScanner;

public async void Start() {
if (_started) {
throw new MvxException("Barcodescanner already started");
}

if (await CreateScanner()) {
Log.Trace("BarcodeScanner Registration Successfull");
if (await ClaimScanner()) {
Log.Trace("BarcodeScanner Claimed Successfull");
_started = true;
// Make sure the scanner remains claimed for our app
_claimedBarcodeScanner.ReleaseDeviceRequested += _claimedBarcodeScanner_ReleaseDeviceRequested;
_claimedBarcodeScanner.DataReceived += _claimedBarcodeScanner_DataReceived;
_claimedBarcodeScanner.IsDecodeDataEnabled = true;
if (await EnableScanner()) {
Log.Trace("Barcode Scanner ready to Scan");
}
}
}
}

private void _claimedBarcodeScanner_DataReceived(Windows.Devices.PointOfService.ClaimedBarcodeScanner sender, Windows.Devices.PointOfService.BarcodeScannerDataReceivedEventArgs args) {
var rawdata = Windows.Storage.Streams.DataReader.FromBuffer(args.Report.ScanData);
var barcodedata = Windows.Storage.Streams.DataReader.FromBuffer(args.Report.ScanDataLabel);

// Remove the checksum
string strippedbc = barcodedata.ReadString(args.Report.ScanDataLabel.Length);
strippedbc = strippedbc.Remove(strippedbc.Length - 2);
// Trim the end
strippedbc = strippedbc.TrimEnd();

OnBarcodeScanned(args.Report.ScanDataType, rawdata.ReadString(args.Report.ScanData.Length), strippedbc);
}


protected virtual void OnBarcodeScanned(uint scanDataType, string rawData, string barcode) {
Log.MethodEnter();
Log.Debug($"scanDataType {scanDataType}");
Log.Debug($"rawData {rawData}");
Log.Debug($"barcode {barcode}");
ReadingAvailable?.Invoke(this, new MvxValueEventArgs<MvxBarcodeScannerReading>(new MvxBarcodeScannerReading(scanDataType, rawData, barcode)));
Log.MethodExit();
}

private void _claimedBarcodeScanner_ReleaseDeviceRequested(object sender, Windows.Devices.PointOfService.ClaimedBarcodeScanner e) {
e.RetainDevice();
Log.Trace("Event ReleaseDeviceRequest received. Retaining the Barcode Scanner.");

}

public void Stop() {
_started = false;
ReleaseScanner();
}

public MvxBarcodeScannerReading LastReading
{
get { return _lastReading; }
}

public event EventHandler<MvxValueEventArgs<MvxBarcodeScannerReading>> ReadingAvailable;

/// <summary>
/// Create a barcodescanner object
/// </summary>
/// <returns>true if system has a barcodescanner</returns>
public async Task<bool> CreateScanner() {
_barcodeScanner = await Windows.Devices.PointOfService.BarcodeScanner.GetDefaultAsync();

if (_barcodeScanner == null) {
DeviceInformationCollection col = await DeviceInformation.FindAllAsync(Windows.Devices.PointOfService.BarcodeScanner.GetDeviceSelector());
if (col.Count > 0) {
_barcodeScanner = await Windows.Devices.PointOfService.BarcodeScanner.FromIdAsync(col[0].Id);
}
}

return _barcodeScanner != null;
}
/// <summary>
/// Claim the barcode scanner
/// </summary>
/// <returns>true if claimed</returns>
private async Task<bool> ClaimScanner() {
if (_claimedBarcodeScanner != null) {
Log.Trace("Scanner was allready claimed");
return true;

} else {
// claim the barcode scanner
_claimedBarcodeScanner = await _barcodeScanner.ClaimScannerAsync();
// enable the claimed barcode scanner
if (_claimedBarcodeScanner == null) {
return false;
}
}
return true;
}

private async Task<bool> EnableScanner() {
// enable the claimed barcode scanner
if (_claimedBarcodeScanner == null) {
return false;
} else {
await _claimedBarcodeScanner.EnableAsync();

Log.Trace("Enable Barcode Scanner succeeded.");

return true;
}

}

public void ReleaseScanner() {
if (_claimedBarcodeScanner != null) {
// Detach the event handlers
_claimedBarcodeScanner.DataReceived -= _claimedBarcodeScanner_DataReceived;
_claimedBarcodeScanner.ReleaseDeviceRequested -= _claimedBarcodeScanner_ReleaseDeviceRequested;
// Release the Barcode Scanner and set to null
_claimedBarcodeScanner.Dispose();
_claimedBarcodeScanner = null;
}
_barcodeScanner = null;
}
}
}

namespace MvvmCross.Plugins.Accelerometer.WindowsCommon
{
public class MvxWindowsCommonBarcodeScanner : IMvxAccelerometer
{
private bool _started;
private Windows.Devices.Sensors.Accelerometer _accelerometer;

public void Start()
{
if (_started)
{
throw new MvxException("Accelerometer already started");
}
_accelerometer = Windows.Devices.Sensors.Accelerometer.GetDefault();
if (_accelerometer != null)
{
_accelerometer.ReadingChanged += AccelerometerOnReadingChanged;
}
_started = true;
}

public void Stop()
{
if (!_started)
{
throw new MvxException("Accelerometer not started");
}
if (_accelerometer != null)
{
_accelerometer.ReadingChanged -= AccelerometerOnReadingChanged;
_accelerometer = null;
}
_started = false;
}

private void AccelerometerOnReadingChanged(Windows.Devices.Sensors.Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
var handler = ReadingAvailable;

if (handler == null)
return;

var reading = ToReading(args.Reading);

handler(this, new MvxValueEventArgs<MvxAccelerometerReading>(reading));
}

private static MvxAccelerometerReading ToReading(AccelerometerReading sensorReading)
{
var reading = new MvxAccelerometerReading
{
X = sensorReading.AccelerationX,
Y = sensorReading.AccelerationY,
Z = sensorReading.AccelerationZ
};
return reading;
}

public bool Started => _accelerometer != null;

public MvxAccelerometerReading LastReading
{
get
{
try
{
var reading = ToReading(_accelerometer.GetCurrentReading());
return reading;
}
catch (Exception exception)
{
throw exception.MvxWrap("Problem getting current Accelerometer reading");
}
}
}

public event EventHandler<MvxValueEventArgs<MvxAccelerometerReading>> ReadingAvailable;
}
}
@@ -0,0 +1,23 @@
// <copyright file="Plugin.cs" company="Cirrious">
// (c) Copyright Cirrious. http://www.cirrious.com
// This source is subject to the Microsoft Public License (Ms-PL)
// Please see license.txt on http://opensource.org/licenses/ms-pl.html
// All other rights reserved.
// </copyright>
//
// Project Lead - Stuart Lodge, Cirrious. http://www.cirrious.com - Hire me - I'm worth it!

using MvvmCross.Platform;
using MvvmCross.Platform.Plugins;

namespace MvvmCross.Plugins.Accelerometer.WindowsCommon
{
public class Plugin
: IMvxPlugin
{
public void Load()
{
Mvx.RegisterSingleton<IMvxAccelerometer>(new MvxWindowsCommonBarcodeScanner());
}
}
}
@@ -0,0 +1,28 @@
using System.Reflection;
using System.Resources;

// 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("MvvmCross.Plugins.Accelerometer.WindowsCommon")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MvvmCross.Plugins.Accelerometer.WindowsCommon")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]

// 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("4.0.0.0")]
[assembly: AssemblyFileVersion("4.0.0.0")]
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MvvmCross.Binding" version="4.1.6" targetFramework="portable46-win81+wpa81" />
<package id="MvvmCross.Core" version="4.1.6" targetFramework="portable46-win81+wpa81" />
<package id="MvvmCross.Platform" version="4.1.6" targetFramework="portable46-win81+wpa81" />
</packages>