ChrisNTR's Blog

Just another developer weblog.

Get Scanning With Google Glass

Since Google Glass has no easy way of fine detailed text entry (when it comes to entering in passwords or for authentication), the recommended way of getting specific data to an application is to use a QR code to send this information. To get this integrated with a Google Glass Xamarin.Android application is very simple, we’ll take full advantage of the ZXing.Net.Mobile barcode scanning library in C# and then use the Google Glass SDK to display the scanned results in our application.

Scanning Example Scanned Example

To help with a File –> New Project –> Google Glass application, take a look at the Xamarin Studio and Visual Studio templates.

Using the ZXing.Net.Mobile library (version 1.4.2 and above) from the component store for simple access to asynchronous scanning of a QR code. To get the ZXing library setup, you’ll also need to include the Android Support Library v4 component.

Next we will want to add the code to tell the app to scan a code, you may want to do this after telling the user that you are going to be scanning a QR code (and give them a hint as to where they can generate a QR code from). The code for this is as follows:

var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
var result = await scanner.Scan();

Since we’re using await, we’ll need to make our OnCreate method async:

protected override async void OnCreate (Bundle bundle)

And we’re going to be using the Camera, so let’s make sure that we add the Camera permission for that in the “Android Application” project options.

To handle the result, we’ll create a new Card and display the result in there, if the result is not empty.

if (result != null) {
    var card2 = new Card (this);
    card2.SetText (result.Text);
    card2.SetFootnote ("Just scanned!");
    SetContentView (card2.ToView());
}

We’ll also want to keep the screen on whilst we are scanning a QR code so we can use the Window AddFlags/ClearFlags API to do this and when we have scanned the QR code, we can play a success sound. Combining all this, you should get something like the following:

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

    var card = new Card (this);
    card.SetText ("Welcome to Xamarin Google Glass Development");
    card.SetFootnote ("Let's get hacking!");
    SetContentView (card.ToView ());

    Window.AddFlags (WindowManagerFlags.KeepScreenOn);

    var scanner = new ZXing.Mobile.MobileBarcodeScanner(this);
    var result = await scanner.Scan();

    AudioManager audio = (AudioManager) GetSystemService(Context.AudioService);
    audio.PlaySoundEffect((SoundEffect)Sounds.Success);

    if (result != null) {
        var card2 = new Card (this);
        card2.SetText (result.Text);
        card2.SetFootnote ("Just scanned!");
        SetContentView (card2.ToView());
    }

    Window.ClearFlags (WindowManagerFlags.KeepScreenOn);
}

Now we’ll need a QR code to scan, there’s a few websites that you can generate QR codes from (such as GoQR), but it might be worth generating your own QR codes, Scott Hanselman has a good blog post on this here: http://www.hanselman.com/blog/HowToDisplayAQRCodeInASPNETAndWPF.aspx, as an example, here’s a QR code to scan and get you started:

QR Code Example

Plug in Google Glass, hit run and that’s all there is to it. If you wanted to go further, you can also take advantage of the CustomOverlay support with ZXing to make the UI look a little more Google Glass friendly by adding in a custom layout resource and adding this with the following code:

scanner.UseCustomOverlay = true;
scanner.CustomOverlay = LayoutInflater.Inflate(Resource.Layout.QRScan, null);

which can end up getting a UI that looks a little like this:

QR Scanner Styled

You can find all of the code for this example on my GitHub page for Xamarin Glass QR Scanner.

Google I/O, Open Source Apps and More!

Episode nine of the Xamarin Podcast discusses some new announcements from Google, open source applications that you can learn from, MVVM frameworks to speed up your development and much much more. As always, you can see the links we mention on show below, or see the links inline on the SoundCloud file. You can use the following link to subscribe using your favourite podcast aggregator here: http://feeds.soundcloud.com/users/55118534-xamarin-podcast/tracks, or subscribe via the iTunes link here: https://itunes.apple.com/podcast/xamarin-podcast/id691368176?mt=2!


Fourth Episode of the Xamarin Podcast

Episode four of the Xamarin Podcast discusses all things Xamarin and mobile development related, covering a lot of great articles focusing on iOS 7 development with Xamarin.iOS, Porting Silverlight apps to iOS and Android and various other links from around the web. As always, you can see the links we mention on show below, or see the links inline on the SoundCloud file. You can use the following link to subscribe using your favourite podcast aggregator here: http://feeds.soundcloud.com/users/55118534-xamarin-podcast/tracks, or subscribe via the iTunes link here: https://itunes.apple.com/podcast/xamarin-podcast/id691368176?mt=2!


Days Until Xmas Goes Open Source

For the past couple of years, I have created and maintained the Days Until Xmas applications. Originally created for Windows Phone to see what the development platform was like, this quickly ended up getting ported over to the iPhone and iPad and then on to Windows 8.

I’ve been lucky enough to have some success with the Windows phone version (as much success as you can have with a free app with no ads…) hitting over the 90,000 download mark on Windows Phone only a few days ago. With developing an application comes feedback and reviews. Whilst most of this is generally in the positive side or people just really hate Xmas, a lot of the feedback I hear, especially from developers is on the lines of, “Can you make it do X”.

Whilst I’d love to spend my time developing a “Days Until X” app, add in new music and new graphics, in reality, I find it difficult to find enough time keeping Days Until Xmas up to date on the new platforms that are coming out, such as iOS 7 support or adding the live tile information as a lock screen item on Windows Phone 8.

Therefore, I have decided to give back the weekend hacks of the app to the community and I have open sourced both the iOS and Windows Phone versions, with Windows 8 coming soon down the line. Feel free to get creative with what sort of applications you can derive from this app, wouldn’t it be cool to countdown the days until Independence Day (yes, I get the irony being British and all), have confetti fall down playing the Star-Spangled Banner song in the background? Or even a customised countdown until a anniversary for a loved one? I’m sure there’s many other ideas you can come up with.

You can the code directly from GitHub at https://github.com/chrisntr/DaysUntilXmas and get building, feel free to fork the project, refactor the code, remove some of my horrible code but make something awesome. The iOS version of Days Until Xmas uses Xamarin.iOS so you will need a Mac to develop that app, it works fully on the Xamarin Starter edition so there’s no need to purchase a license for the Xamarin tools. For Windows Phone and Windows 8, you can just use the Windows Phone SDK and the Windows a Store SDK within Visual Studio.

The music used in the application was created by Kevin MacLeod, so you should check his website at incompetech.com for music in your application, though you would need to make sure have the right license if you plan on selling your countdown app.

If you want to hear more details around the Days Until Xmas application then you should check out my talk at NDC 2013 and my appearance on the Tablet Show.

Third Episode of the Xamarin Podcast

Episode three of the Xamarin Podcast discusses all things Xamarin and mobile development related, talking about the recent iOS 7 and iPhone 5c and iPhone 5s releases, astronauts using Xamarin and helpful components and open source projects. As always, you can see the links we mention on show below, or see the links inline on the SoundCloud file. You can use the following link to subscribe using your favourite podcast aggregator here: http://feeds.soundcloud.com/users/55118534-xamarin-podcast/tracks, or subscribe via the iTunes link here: https://itunes.apple.com/podcast/xamarin-podcast/id691368176?mt=2!


Second Episode of the Xamarin Podcast

Today brings you the second episode of the Xamarin Podcast, we hope you enjoy it! As always, you can see the links we mention on show below, or see the links inline on the SoundCloud file. You can use the following link to subscribe using your favourite podcast aggregator here: http://feeds.soundcloud.com/users/55118534-xamarin-podcast/tracks, or subscribe via the iTunes link here: https://itunes.apple.com/podcast/xamarin-podcast/id691368176?mt=2!


First Episode of the Xamarin Podcast

I’m happy to share with everyone the first episode of the unofficial Xamarin Podcast, a podcast that discusses all things Xamarin and mobile development related, including news, events and interesting projects and apps in the mobile and Xamarin developer space. Listen to the podcast with myself and Pierce Boggan below using the embedded SoundCloud file below, feel free to leave comments in-line on the audio and share with your friends. You can find the links mentioned in the show below:

Update: Subscribe to the podcast with your favourite podcast aggregator here: http://feeds.soundcloud.com/users/55118534-xamarin-podcast/tracks, iTunes link is here: https://itunes.apple.com/podcast/xamarin-podcast/id691368176?mt=2!

Register for the March Xamarin Seminars

Xaminar Logo

With three great Xamarin Seminars out the door, it looks like the series is only going to keep getting better. Today we are announcing topics for our March seminars:

  • Thursday 8th March 2012 at 11am EST – MonoTouch 5.2 with Mike Bluestein
  • Thursday 22nd March 2012 at 11am EST – Using iCloud with MonoTouch with Craig Dunn

Registration is open for the March Xamarin Seminars via GoToWebinar.

You’ll get an automated reminder the day before and an hour before the event to make sure you don’t miss a moment. You can even join us on your iPhone, iPad or Android phone with their GoToMeeting app.  If you can’t make them, don’t worry, we have putting up all our previous seminars, including code samples and slides on YouTube, SlideShare and GitHub. You can see the previous Xamarin Seminars below:

Third Party Libraries with MonoTouch and Mono for Androids

Slides for this session: http://www.slideshare.net/Xamarin/third-party-libraries-with-monotouch-and-mono-for-android Code for this session: https://github.com/xamarin/Seminars/tree/master/2012-02-23-ThirdPartyLibraries Give your feedback on the session: http://bit.ly/xamfeedback Uservoice for Seminar suggestions: http://xamarin.uservoice.com/forums/148414-xamarin-seminars James Clancey goes through using third party libraries for MonoTouch and Mono for Android covering the following third party libraries: Flurry Analytics ATMHud Facebook Sqlite-net Steema TeeChart MonoGame


Top 5 Features of Ice Cream Sandwich

Slides for this session: http://www.slideshare.net/Xamarin/top-5-features-of-ice-cream-sandwich-with-mono-for-android-40-11503971 Code for this session: https://github.com/xamarin/Seminars/tree/master/2012-02-09-Top5FeaturesOfICS Give your feedback on the session: http://bit.ly/xamfeedback Mike has been an active member of the community since MonoTouch launched, and recently joined Xamarin’s excellent documentation team. In this seminar, Mike shows you how to use 5 of the new APIs available to developers with Mono for Android 4.0 and Ice Cream Sandwich; Calendar API ShareActionProvider Action Bar Tabs TextureView Android Beam


Getting Started with CoreGraphics

Slides for this session: http://www.slideshare.net/Xamarin/getting-started-with-coregraphics Code for this session: https://github.com/xamarin/Seminars/tree/master/2012-01-26-CoreGraphics Give your feedback on the session: http://bit.ly/xamfeedback Mike has been an active member of the community since MonoTouch launched, and recently joined Xamarin’s excellent documentation team. In this seminar, Mike shows you how to get to grips with creating 2D graphics and modify PDF files on the fly within your MonoTouch applications. Q&A; Q: What is the best way to draw shaped UIViews? A: Subclass UIView and override Draw like we showed with the StarView example. Use the Bounds in the subclass to control the size of the view within its own coordinate system. Q: Where would I start if I want to allow text selection in a PDF (page) that is drawn with Core Graphic’s PDF features? (Like iBook does it) A: You can use a CGPDFScanner to parse a PDF document’s content. See this: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf_scan/dq_pdf_scan.html#//apple_ref/doc/uid/TP30001066-CH220-TPXREF101 One approach to highlighting the text would be to calculate the size and position of the text and draw a layer on top of the desired area. For more information on text with Core Graphics see: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF103 Q: What would happen if you call Multiple SaveStates before calling RestoreState, does it replace the last save state or does it work like a stack, whereas Save is a push and Restore is a pop. A: It’s a stack. Q: I’m not sure I quite understood what SaveState/RestoreState does, does it affect the transformation matrix changes done within the “state”, such that caling restore would revert all changes done to the matrix ? A: Correct, in the example this was used so that the text drawing would not be in the cropped pdf box.