Geeks With Blogs

News
Joe Mayo

Glass is Google’s new wearable computer - no doubt you’ve heard about it in the news. What’s cool and new today is that you can now write Glass apps that use LINQ to Twitter. In this article, I’ll show you how.

Say What!?

That’s right, I said “You can now write Glass apps with LINQ to Twitter”. There might have been a day when the combination of Twitter, LINQ, and a Google device might have been unimaginable, but Xamarin has changed all that. You see, Xamarin lets you write C# apps that deploy to Android platforms, LINQ to Twitter runs on Xamarin.Android, and the Glass Development Kit (GDK) is an Android library. Therefore, it’s a quick leap to reference LINQ to Twitter in a Xamarin app that deploys to Glass.

Setting Up

Configuring this took most of the time, but the code rolled into place quickly after everything was set up. First, you’ll need to load the driver on your device. I started with this CodeProject article, Building Glassware with the Xamarin Android and Google’s GDK, but ran into problems loading the driver. (Make sure you have ADB tools and the USB driver downloaded, as the article specifies.)  Searching the Glassware-Developers forum, I found Omar’s post for Problems with Windows Driver that worked for me. There are multiple answers to how to configure drivers and this may depend on the platform you’re using, software versions, and device. I have a Glass XE12 device and am deploying from a Windows 8.1 (64-bit) system.

Next, you need the Glass Development Kit from the Xamarin Component Store. As of this writing, this is a (Sneak Peek) version, but it worked fine for this demo. I originally tried deploying the sample application from the 1.0.1 version, but ran into problems. The version I’m using now is 1.1 and it’s working fine so far.

I’m using Visual Studio 2013. Here are the steps for setting up your project.

1. Create a new Android project and make sure it’s the Ice Cream Sandwich template. I called my project HelloGlass.

SNAGHTML499b44f

2. The versioning on the Properties | Application tab is already set to API Level 15 (Xamarin.Android v4.0.3 Support), which is Ice Cream Sandwich. Remember to set the Application Name, Package Name, Version Number, and Version Name in the Properties | Android Manifest tab: I used HelloGlass, LinqToTwitter.HelloGlass, 1, and 1; respectively.

SNAGHTML4a115b6

3. After downloading, unzip the Glass Development Kit and locate the bindings DLL at \googleglass-1.1\lib\android\GoogleGlassBindings.dll. Add a reference to GoogleGlassBindings.dll. As of this posting, LINQ to Twitter v3.0 is available on NuGet is a pre-release. The v2.1.x version of LINQ to Twitter supports older versions, so make sure you get the v3.0.x version.

Note: LINQ to Twitter has a dependency on HttpClient, which includes additional references. You’ll might get errors on System.Threading.Tasks, System.Runtime, and System.IO, but you can remove these references with no problem.

SNAGHTML4b0541e

Now, your project is set up, let’s code.

Creating Cards

Google’s Web site for the Glass Developers Kit has a quick start that is remarkably easy to follow. In Xamarin.Android, you just convert the camelCase in their code examples to PascalCase. There are different types of UI elements, Cards, you can create: static, live, and immersions. For this demo, I’ll go with the simplest case of creating a static Card.

To get started, open Activity1.cs and add using declarations for LinqToTwitter, System.Linq, and Android.Glass.App. Then, replace OnCreate with the following code:

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

            Card card1 = new Card(this);

            var auth = new ApplicationOnlyAuthorizer()
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey = "Your Consumer Key",
                    ConsumerSecret = "Your Consumer Secret",
                },
            };
            await auth.AuthorizeAsync();

            var ctx = new TwitterContext(auth);

            var tweet =
                await
                (from twt in ctx.Status
                 where twt.Type == StatusType.Show &&
                       twt.ID == 421551271137378304ul
                 select twt)
                .SingleOrDefaultAsync();

            card1.SetText(tweet.Text);
            card1.SetFootnote(
                "@" + tweet.User.ScreenNameResponse +
                " - " + DateTime.Now.ToShortDateString());
            View card1View = card1.ToView();

            SetContentView(card1View);
        }

In the code above, I’m using ApplicationOnlyAuthorizer for OAuth with the Twitter API. Then, I do a query for a tweet I posted specially for this demo.

To set up the UI, the code creates an instance of Card, sets the text to the text of the tweet, and sets the footer to the tweeter’s screen name and date of the tweet.

Finally, you need a View from the card, by calling ToView on the Card instance, card1. Then set that as the Content view.

Running HelloGlass

To run the HelloGlass app, make sure your Glass is plugged into a USB on your computer. You should see a drop-down list of devices and emulators in your Visual Studio 2013 toolbar. Select your Glass device; mine is named Google Glass 1.

You have to deploy first, so right-click on your project and select deploy. If this is the first time you’ve deployed, it will take a few minutes to load. You can see the activity in the Output window.

After deployment, press Start. The app will deploy and you can see progress in the output screen. Keep glass awake while the app deploys and you’ll see the card appear, showing the tweet id #421551271137378304.

Summary

When preparing to write a first Glass app, you might have to jump through a few hoops to configure your system. However, after that it’s just coding. LINQ to Twitter v3.0 is now available through NuGet and you just need a reference to it and the Xamarin Glass Developer Kit. Of course you need to have Xamarin installed, which runs with Visual Studio, the details of which you can find on the Xamarin Web site. The demo instantiated a static card, did a query with LINQ to Twitter, and loaded that card with data from the tweet. Finally, you deploy, start, and view your app.

@JoeMayo

Posted on Friday, January 10, 2014 2:54 PM LINQ to Twitter , xamarin , android , glass | Back to top

Copyright © Joe Mayo | Powered by: GeeksWithBlogs.net