Click here to Skip to main content
14,547,011 members

Building Glassware with the Xamarin Android and Google’s GDK

Rate this:
4.10 (3 votes)
Please Sign up or sign in to vote.
4.10 (3 votes)
21 Nov 2013CPOL
Building Glassware with the Xamarin Android and Google's GDK

Within the past 48 hours, Google’s Glass Explorer program has gotten a whole lot more interesting. As developers, we now have a native development kit (GDK), and a distribution method of deploying and installing APKs on to your device. Before this, Glass was limited to a RESTful timeline based API called Mirror which relied on a service to push notifications to the device. The introduction of the GDK now opens up immersive, offline experiences, which can take full advantage of the device hardware. As a developer, this is exciting to explore some more detailed ideas, but working primarily in the Microsoft camp, I thought I’d push to see if we can create native Glassware in C# – and we can. So here is a quick guide to getting up and running with Xamarin Android and the Glassware GDK.

1. Setting Up ADB

I’m developing on Windows 8.1, using Visual Studio 2013 and the latest beta builds of the Xamarin stack. So my first issue was getting Glass connected to the machine via USB so we debug and deploy. Out of the box ADB doesn’t include the latest Glass device. So we need to edit the driver file.

To do so, first ensure you have the ADB tools installed, by launching the Android SDK Manager, and install the Google USB Driver:

image

Now you have the driver on your system, you can attempt to install Glass. Plug your device in via USB and see whether it installs as an Android device in Device Manager.

image

If it does not, you’ll need to find the USB composite device or camera that it would have been installed as and force it to use the Google driver. The driver can be found at “C:\Users\[UserName]\AppData\Local\Android\android-sdk\extras\google\usb_driver”. As I mentioned, the driver version I had didn’t actually support Google Glass so I had to edit the android_winusb.ini file to include the following in the sections [Google.NTx86] and [Google.NTamd64]:

;Google Glass
%SingleAdbInterface% = USB_Install, USB\VID_18D1&PID_4E11&REV_0216
%CompositeAdbInterface% = USB_Install, USB\VID_18D1&PID_4E11&MI_01

By editing the driver file, you invalidate the signature so you’ll need to disable driver enforcement mode by following these instructions. Once you have restarted, you should be able to use this driver for Glass from no-onwards.

2. Porting the GDK to Xamarin

A lot of Glass development can be leveraged using out-of-the-box Android APIs, but if you want to utilize native UX or voice inputs, you’ll need access to the GDK libraries. Luckily in Android, this is pretty straight forward. Firstly, we need to download the extensions via the Android SDK Manager, which can be found under the API 15 ICS group:

image

Once this is installed, you’ll find a JAR file appears on your file system in “C:\Users\[USER]\AppData\Local\Android\android-sdk\add-ons\addon-google_gdk-google-15\libs” call GDK.jar. So we will need to bring this into Visual Studio, so it’s time to launch a new Visual Studio instance, and create a new solution containing a new Java Bindings library:

image

Once the project opens, copy gdk.jar into the Jars folder within the project, and change the Build Action of the file to “embedded Jar”. Then build the solution. When I do so, I get 6 build errors which related to two invalid method signatures, and a missing property from a base class. If you manually fix these, the output will be a GDK.dll which we can use inside our Glass project.

If you run into any issue, a copy of the assembly can be found at http://sdrv.ms/I0jy28.

3. Building Your Glassware in Visual Studio

Back in your Visual Studio instance, add a new Ice Cream Sandwich project:

image

Once the project loads, add a reference to the GDK project/assembly you’ve created.

Now we can start writing code.

Glassware are simply Android applications, which can leverage standard Android APIs or some of the custom extensions found within the GDK to create more familiar Glass interfaces such as cards. To make things easy, we’ll use the help objects.

So in your Activity, clear out the code in the OnCreate method, and create a new timeline card:

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

            var card = new Card(this);
            card.SetText("Xamarin on Glass is awesome");

            SetContentView(card.ToView());
        }

The card object, provides an extension to convert the template across to a View object, so we can simply set the properties required to generate our UI with no need to worry about the AXML interface files. For more information on styling cards, check out https://developers.google.com/glass/develop/gdk/ui/theme-widgets.

If the Glass device is connected to your PC via USB (and setup correctly), you should be able to Hit F5 and deploy directly to your device. You timeline needs to be open to see the app load (i.e., debugging won’t turn the screen on).

The above code when debugged should render the following on your Glass device:

image

So we now have our app on the device, and we can start to build awesome stuff. Yet at this point, we can only launch the app via debugging. To change this, we’ll hook into the “OK glass…” speech launch menu. To do so, we simply use some basic Android APIs.

First, we want to register an intent on our activity, so we add the follow metadata to our Activity class:

[IntentFilter(new[] { "com.google.android.glass.action.VOICE_TRIGGER" })]
[MetaData("com.google.android.glass.VoiceTrigger", Resource = "@xml/use_csharp")]
public class Activity1 : Activity{
  ...
 }

Next we wire-in the metadata, so add a new directory in the resources folder called Xml, and then add a new XML file called use_csharp.xml. Inside the file, add the following:

<?xml version="1.0" encoding="utf-8"?>
<trigger keyword="@string/glass_voice_trigger" />

This registers the launch command for our voice trigger. If you want a more complex launch structure, follow the guide at https://developers.google.com/glass/develop/gdk/input/voice.

Now add the actual command into the strings.xml file in the values directory:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="glass_voice_trigger">use csharp</string>
</resources>

If you deploy the app, we should see the app appear in the OK glass... menu which allows us to launch our APK from the Glass home menu.

image

And that’s it… It’s as simple as that to write Glassware in C# using Xamarin for Android. So enjoy creating your wearable experiences.

The full sample is available at http://sdrv.ms/19IuNmk.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lewis Benge
Software Developer (Senior)
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionIoT Contest article? Pin
Kevin Priddle14-Nov-14 10:35
professionalKevin Priddle14-Nov-14 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 21 Nov 2013

Tagged as

Stats

17.7K views
4 bookmarked