Quick Start

Follow this guide to set up a project for Xamarin.Android in order to easily implement the Anyline SDK.

The Xamarin Bundle

The Xamarin SDK Bundle that you can download from GitHub contains the following parts for you to get started in Xamarin:

  • BindingSource - Xamarin iOS and Android Binding Libraries

  • Examples - Xamarin.iOS, Xamarin.Android, and Xamarin.Forms example apps

  • com.anyline.xamarin.examples_<version>.apk - Prebuilt Android APK ready to install on your Android device

Requirements

To use the Anyline SDK for Xamarin.Android you need:

  • Target Android SDK Level >= 21 on your project

  • An Android device with decent camera functionality (recommended: 720p and adequate auto focus)

Setup

In this section we will go through the basic process of creating and configuring a simple Anyline scanning application in Xamarin.Android. If you are not yet familiar with Xamarin.Android, follow the Official Xamarin Android Quickstart Guide to develop an understanding on the fundamentals of Android application development with Xamarin.

Create a new Xamarin.Android Project

If you are using Visual Studio, click File > New Project…​, select Visual C# > Android and create a new Blank App.

Add Anyline XamarinSDK.Droid via NuGet

To access the functionality of our SDK, simply right-click on your project, go to manage Nuget packages and search for "Anyline".
Select Anyline.Xamarin.SDK.Droid, install the latest available version, and accept the license agreement.

Set the minimum Android target

In the Application settings of your project, set the minimum version of your Android target to (at least) API level 21.

Add the necessary permissions and features

The scanning activity needs the following permissions and features:

  • Permissions

    • CAMERA

    • VIBRATE

    • NFC

  • Features

    • android.hardware.camera

    • android.hardware.camera.flash

    • android.hardware.camera.autofocus

    • android.hardware.nfc

This is how you can add them to your AndroidManifest.xml in your Properties node:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.anyline.xamarin.examples" android:installLocation="auto">
	<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
	<uses-permission android:name="android.permission.CAMERA" />
	<uses-permission android:name="android.permission.VIBRATE" />
	<uses-permission android:name="android.permission.NFC" />
	<uses-feature android:name="android.hardware.camera" />
	<uses-feature android:name="android.hardware.camera.flash"  android:required="false" />
	<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
	<uses-feature android:name="android.hardware.nfc" android:required="false" />
	<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:largeHeap="true"></application>
</manifest>

The NFC feature/permissions are only required when using the NFC scanning capability.

Additionally, you should use this code snippet in your OnCreate() method of your MainActivity to ensure your app requests the Camera Permission to the users:

protected override void OnCreate(Bundle savedInstanceState)
{
	...

	int REQUEST_CAMERA = 0;

	if (ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.Camera) != Permission.Granted)
	{
		// Should we show an explanation?
		if (!ActivityCompat.ShouldShowRequestPermissionRationale(this, Android.Manifest.Permission.Camera))
		{
			// No explanation needed, we can request the permission.
			ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Camera }, REQUEST_CAMERA);
		}
	}
	else
	{
		System.Diagnostics.Debug.WriteLine("Permission Granted!");
	}

	...
}
Camera API & Hardware Acceleration

Since newer devices might use the Android Camera2 API, it’s important to make sure that hardware acceleration is enabled in the activity that implements Anyline. This can be set as seen above through the AndroidManifest, but also directly in the C# code of the activity, the following way: [Activity(Label = "My Activity", HardwareAccelerated = true)]

Dex Compiler

Starting from SDK 21, Anyline uses Java 8 which requires Xamarin.Android projects to be compiled using the "d8" Dex compiler. To set this compiler, go to the project Properties and, under the "Android Options" tab, select the option "d8" in the Dex compiler. If the option is not available on Visual Studio (older versions), you can also add the following line directly on the .csproj file for both Debug and Release groups: <AndroidDexTool>d8</AndroidDexTool>

Generate an Anyline License Key

In order to run the Anyline SDK in your app, you require an Anyline License Key. To create a license key for your application, you have to identify the applicationId of your Xamarin.Android app.

License <> applicationId

Every license is bound to an applicationId. If you change your applicationId, you will require a new license. This also ensures that your license key cannot be used in any other application.

How to identify the Application ID

To identify the Application ID of your application, refer to the package name of your Xamarin.Android project.

xamarinAndroidPackageVS

Once you generated the license key for the package name of your app, you can integrate it as follows:

Integrate the License Key

In order to integrate your generated license key, you can have the license key as string to your .cs code-behind of your scanning activity, or create a string resource under Resources\Values\Strings.xml and reference it.

In your C# code-behind

string licenseKey = "YOUR_LICENSE_KEY";

From a resource string

<resources>
	<string name="license_key">YOUR_LICENSE_KEY</string>
</resources>

Initialize the SDK

Initialize the Anyline SDK in your MainActivity, or in another activity of your preference.

The Anyline SDK only needs to be initialized before its first usage, and only one time within the app’s lifecycle.

MainActivity - OnCreate

protected override void OnCreate(Bundle savedInstanceState)
{
	base.OnCreate(savedInstanceState);

	...

	// INSERT YOUR LICENSE KEY HERE
	string licenseKey = "YOUR_LICENSE_KEY_HERE";
	try
	{
		IO.Anyline2.AnylineSdk.Init(licenseKey, this);
	}
	catch (Exception e)
	{
		Toast.MakeText(this, e.Message, ToastLength.Long).Show();
		System.Diagnostics.Debug.WriteLine(e.Message);
	}

}

In the next section, we will implement a ScanView and provide a configuration to customize visual parameters and behaviours.