
Pre-requisites: Eclipse IDE, Android SDK, Google Glass
You need to run the application directly on the Glass device, there is no emulator (make sure you enable debugging on Glass)
In this tutorial, we will learn how to write a simple “Hello,World!” program for Google Glass using the GDK.
Step 1: Create Android project for Glass
Create a new Android application project called HelloGlass with package name com.app.glass and choose the Target platform as Glass Development Kit Preview for Android 4.4
Step 2: Creating cards
As per the Glass design patterns, developers can either create static cards or Live cards. In this case I will choose to create a static card since I would only be displaying a message to the user. The Card class creates well-formed cards given a set of properties.
A Card has the following properties:
1. Main body text
2. Left-aligned footer
3. One or more images that are displayed as a mosaic on the background of the card or on the left side of the card.
Step 3: Voice Invocation model
Voice commands play an important role in user interaction with Glass allowing hands-free and quick actions. Any Glass application that you develop needs to be invoked using a voice command. Developers can also add their own voice commands. However, please make sure you read the voice command checklist before you proceed.
Step 4: Putting it all together
Let’s start by creating our Hello,World application. Create a new Activity class called HelloWorldActivity and add the following code!
HelloWorldActivity.java
package com.app.glass;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.google.android.glass.app.Card;
public class HelloWorldActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Card myCard = new Card(this);
myCard.setText("Hello, World!");
myCard.setFootnote("First Glassware for Glass");
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);
}
}
Next, we need to create a Service class that will recognize the voice command.
HelloGlass.java
package com.app.glass;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class HelloGlass extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
Intent i = new Intent(this, HelloWorldActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return START_STICKY;
}
}
In order to launch the Glassware from the “ok,glass” menu we need to add the following service in the AndroidManifest.xml file!
AndroidManifest.xml
<!--?xml version="1.0" encoding="utf-8"?-->
package="com.app.glass"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<!-- Don't use themes -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.app.glass.HelloWorldActivity"
android:label="@string/app_name"
android:enabled="true">
<service
android:name="com.app.glass.HelloGlass"
android:enabled="true"
android:exported="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Voice command found in res/xml/voice_trigger_start -->
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="@xml/voice_trigger_start" />
res/xml/voice_trigger_start.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<!-- The string used to start the application from the "Okay, Glass" menu -->
res/values/strings.xml
<!--?xml version="1.0" encoding="utf-8"?-->
HelloGlass
Settings
hello glass