Treehopper C# API
Android

Introduction

Treehopper supports C# Android apps written in Xamarin.Android.

Getting Started

You can use Visual Studio or Visual Studio for Mac to build Android apps in C#.

Step 1: App manifests

You can connect to Treehopper boards without declaring a manifest or device filter, but if you want your app to be started automatically when you plug in a board, create a device filter, such as:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- 0x04d8, 0xf426 -->
<usb-device vendor-id="1240" product-id="62502" />
</resources>

Step 2: Override methods to call into ConnectionService

To get ConnectionService working properly, it needs access to the MainActivity. To make this less tedious, the Treehopper.Android ConnectionService implements additional methods you must call from OnStart and OnResume, like so:

public class MainActivity : Activity
{
...
protected override void OnStart()
{
base.OnStart();
ConnectionService.Instance.ActivityOnStart(this);
}
protected override void OnResume()
{
base.OnResume();
ConnectionService.Instance.ActivityOnResume();
}
}

Step 3: Get a board reference

Just like with other C#-based Treehopper projects, you'll need a reference to a connected TreehopperUsb board, which will arrive asynchronously.

var board = await ConnectionService.Instance.GetFirstDeviceAsync();

You can also monitor CollectionChanged events generated by the ConnectionService.Instance.Boards ObservableCollection.

While it is always a good idea to check the ConnectionService.Instance.Boards collection to see if a board has already been added (and thus, will not fire a CollectionChanged event), this usually isn't necessary for Android applications, as the permission authorization user interface routines are quite time-consuming (that is, unless an intent filter has been created).