Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Justin Ribeiro, a software engineer, building on the edge with his company, Stickman Ventures, Inc. He will hack just about anything–code, hardware, house, car, and kids’ toys. You can find him on Twitter @justinribeiro.

While Google Glass may not be your average device, developing software for the platform takes on an approach that most developers are familiar with.

Mirror API or the GDK?

These days, you can’t start developing for Glass unless you take a look at the two approaches available: the Mirror API and the Glass Development Kit (better known as the GDK). Currently, the GDK is available as a sneak peek, which is to say it hasn’t been finalized and is subject to change. The Mirror API, however, late last year made it’s way into the Google API Console for use by all, allowing you to begin integrating applications.

Choosing which development tooling to use for Glass will largely depend upon your requirements; if you’re looking for real time access to hardware, the GDK is for you. If you need platform independence and are looking to integrate with existing applications you may have, the Mirror API is a good fit.

In this post, we’re going to explore the Mirror API.

Generating tokens

Before we can use the Mirror API, we first have to create a new API project that enables the Mirror API in the Google Developer Console. Once we’ve named our project, we have to enable the Mirror API:

mirror api

We then generate both a client id key and a public key. For this example, we’re going to point our authorization return to our localhost, though this would be whatever you use to handle the OAuth2 return.

creating the client

We can also define the consent screen, which is what our users who wish to enable our Glassware will see when authenticating.

define consent screen

Authenticating the user

Depending upon your existing infrastructure, how you authenticate users will differ. What has to occur, however, is that you obtain an Oauth2 token you can use on behalf of your user. In this example, we’re going to use PHP and the Google API Client Library to simplify the task of authenticating our end user to the Glassware so that we can send a card to the users’ timeline.

First we setup a client with information we obtained for the Developer Console:

Note the setScopes() call. The permissions here are what our end user will see when authenticating with the application and what we’ll have access to use.

Once we’ve setup our client, we have to do some housekeeping. Is the user who has accessed our application already authenticated? We have to run some basic check against our datastore to see if we need to call out.

On the return to our authorization endpoint we set for our client key, we can store the client token for later use:

Now that we’ve authenticated our end user and have the proper permissions, we can now begin interacting with Glass.

Sending a timeline item

Developing Glassware deals with inserting timeline items known as cards that the user can read and interact with. Through the Mirror API, we can insert, edit, read and delete cards as needed.

Let’s insert a basic timeline card with just some text:

This will inject our card into the users timeline as shown below.

injecting the card

Setting style

Timeline items can contain more than just text; they can contain HTML, or attachments such as media or location. So let’s send a more complicated card, this time with an image as well as some styled text.

This will inject our card into the users timeline as shown below.

injected styled card

The classes assigned to our HTML nodes are not random, but rather defined within the base stylesheet internally on Google Glass. The base stylesheet is available for viewing if you want to see what styles are available for use.

A note on the UI

Understanding the UI Guidelines is an important to successful (and readable) Glassware. The screen has limited real estate and you can’t pound your user with eight pixel fonts. You have to keep things readable and we don’t want to get in the way of our user.

There are set of well defined and in-use templates available in the Mirror API Playground that you can see represented even if you don’t currently have Google Glass available.

Adding menu actions

Now that we can add Timeline items, we can also add menu options to those timeline cards that allow the user to take additional action.

adding menu options

This adds a predefined action called delete that allows the user to remove this card from their timeline. We can expand upon this and create additional options as so:

In this case, our timeline item now allows us to read our speakable text aloud and share the menu item with our contacts on Glass.

Conclusion

In my next article, we’ll expand our understanding of the timeline to not only use default actions, but also to subscribe to pings from actions taken by the user and begin understanding how more advanced interactions can be obtained by using contacts.

Look below for Glass and PHP resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Web Designer’s Guide to Google Glass is a hands-on guide to optimizing web sites for Google Glass will show you how to take advantage of the latest advancements of this tiny screen, including watching videos and viewing forms. You’ll also take a look at the Mirror API and building web-based native apps for Google Glass, and get insightful tips from an expert designer on avoiding web design pitfalls.
Fluent Conference 2013: JavaScript & Beyond Complete Video Compilation is for anyone who missed the Fluent Conference 2013 in San Francisco this past May, allowing you to catch every workshop, session, and keynote with this complete video compilation of the event. Catch up with this year’s lineup of speakers—seasoned pros as well as notable newcomers—as they share their expertise with JavaScript, HTML5, CSS, and related technologies that power the Web, and don’t forget about Fluent Conference 2014.
Programming PHP, 3rd Edition is an updated edition that teaches everything you need to know to create effective web applications with the latest features in PHP 5.x. You’ll start with the big picture and then dive into language syntax, programming techniques, and other details, using examples that illustrate both correct usage and common idioms.

Tags: GDK, Glass, Glass Development Kit, Google Glass, Mirror API, OAuth2, php, timeline, tokens,

Comments are closed.