Building a Xamarin Android app in Visual Studio Team Services and deploying to App Center

In the old days, we use to build our Xamarin apps with Visual Studio and find a way to get the resulting .apk file to the end user for them to install.  Then came Hockey App.  It was and sort of still is (in transition) a distribution platform for Xamarin apps.  It was awesome.  Now of course, we have App Center, a completely modern and full fledged mobile app management and distribution service by Microsoft.  Since Microsoft purchased Xamarin, App Center was the next logical step and is among the many benefits of the acquisition.  Not surprisingly, using Microsoft’s Visual Studio Team Services (VSTS) you get integration with App Center.  Namely, you can configure build definitions in VSTS that allow you to deploy right to App Center.   Before we take that for granted let’s pause and let that sink in for a moment.  What this means is that when I check in my code, it triggers the build definition in VSTS which builds the code in the cloud on a hosted Xamarin agent, then deploys the resulting .apk file to App Center.   I can wash my hands of the mundane, boring task of building in Visual Studio, and manually updating App Center forever!  Dev Ops is all the rage right now, and I must admit, although building a single build and deploy definition is merely dabbing my feet in the water, this stuff can get pretty addictive.  Its like life hacking but with code.  The more I can automate my CI/CD (cringe) pipeline the better.

To get started, I’m going to assume you have a VSTS subscription and a project with a code repository set up.  I’ll assume you have an App Center account and an established Android application.  You can get help with those Here and Here.

To get started with your build definition, navigate to your project and select builds.  Then click the button to create a new definition.

 

Select the type and location of the source code you’re going to build.

 

Now we’re given the option to select a template.  Here we’ll search for Xamarin and select the Xamarin Android template.

 

Now the template is loaded and ready for us to configure as our build definition.  It contains steps that will execute in the order they are listed from the top down.  Templates are great for newbies like me in that they guide us on the right path.  Notice the first step that sets the cloud build environment to use Nuget 4.4.1.  This is a requirement and is already set up for you.  The next step is the Nuget restore step and will pull down the any nuget packages for your code.  I’ve actually set a custom nuget feed in addition to the regular official nuget to that pulls down some extra packages from a private source.   This is set up as a custom Service Endpoint and not covered here.

Next, configure the build step to your liking.  The defaults for this step pretty much work.  Make sure you use JDK version 8 and JDK architecture x64.

 

In the next step, upload the Keystore File that will be used to sign your app and enter the alias and password.

 

The default settings for the “publish artifact: drop” step should already be ok. After that, we’ll configure the deploy step that will send our app on over to App Center.  I’m skipping the optional ‘Test with Visual Studio App Center’ step that allows you to queue and run unit tests for the deployed code in App Center’s Test Cloud.

The App Center distribute step requires you to set up a service endpoint (pictured below) connection to App Center.  Follow the link above to set that up.

 

Go back to the App Center distribute step and configure the following:

– The App Center app slug. The app slug is how the build step locates your app in App Center. This is how you find out what your app slug is in App Center as described by Team Services:

“The app slug is in the format of {username}/{app_identifier}. To locate {username} and {app_identifier} for an app, click on its name from https://appcenter.ms/apps, and the resulting URL is in the format of https://appcenter.ms/users/{username}/apps/{app_identifier} . If you are using orgs, the app slug is of the format {orgname}/{app_identifier}.”

– Enter your desired release notes. You can direct the step to use a specially formatted release file here. I haven’t messed around with this specifically as I trigger the builds manually anyway, and I just fill in the release notes by hand.

– Select an App Center distribution group to publish to by filling in the group ID. If you select none, the default group will be used. To find the ID of a distribution group in App Center, go to the group and select the gear icon to change the group settings. The ID will be displayed there.

Save the definition and you’re ready to go! You just hacked life and saved time by letting VSTS do some work for you.

Using Azure Maps with a Xamarin Forms app

The new Azure Maps service is available, offering APIs and services that enable several common scenarios for location aware apps, all in one convenient place.   This is actually an impressive set of services, namely Search, Maps, Geocoding, Traffic, Routing, and even time zones!

Azure Maps Offerings

 

As soon as I saw this I thought why not build a Xamarin Forms app and use the Maps API to get POI data based on your location and display the results on a map?  So that’s what I did.

Its a simple proof of concept, one screen mobile app for Android.  The screen contains a Map and a search bar.  I used Xamarin.Forms.Maps Nuget package to display the map.  When you enter a search term, I query the Azure Maps Fuzzy Search REST API endpoint to sesrch for Points of Interest using the term you enter into the search box.   To get results local to you, I pass in the Lat and Long of your current location which I obtain by using the Geolocator Plugin.  Since the results of the API can be returned as JSON, I parse the JSON into my model, which I created using QuickType IO.  Then I simply loop through the results and add each of them to the map as a map Pin.  When you tap on a map pin, it shows you the venue name and address.  So that’s it, it was actually pretty easy to get started!  Here’s a screenshot from my Android Emulator with a simulated location.

 

 

 

Raspberry Pi Online Simulator for Azure IoT Hub

Have you thought about trying out Azure IoT Hub, but don’t have an IoT device?  Try the Raspberry Pi Online Simulator.  This is what it sounds like, a simulated Raspberry Pi device running a small NodeJS program that sends data to your IoT hub.  It sends simulated temperature data using a mock BME280 temperature sensor.  All you do is plug in the connection string to your IoT hub and hit ‘Run’.  Its even got a little led that blinks when sending data and receiving the callback.  Send data with this, and you can view it in near real time using the Device Explorer for IoT Hub Devices.  Happy coding!

 

 

 

 

 

Why Azure is so remarkable

Technology moves so fast these days its easy to take for granted the innovations in recent years,  combined with exponential accessibility of new technologies made available to developers at large.  Azure continues to usher in new possibilities of virtually unlimited scale, while removing the burden of overhead, giving us resources we could have only dreamed of just a short time ago.  In that sense, Azure is the Robin Hood of technology, giving the common developer what only those with lots of resources enjoyed previously!  What I’m referring to here transcends the technical.  It’s about empowerment.   In keeping of Microsoft’s every person and every organization on the planet to achieve more,  Azure levels the playing field by giving us the power of super computers, AI, and internet of things, all with global scale and ease of accessibility.

This ushers in a new approach to architecture.  No longer is it necessary to write from scratch some of the traditional models like pub sub, you can stand up services for that.  Take event hubs as an example.  A globally ready,  event ingestion service that can scale to millions of messages per second.    Aha!  You just did it didn’t you?  That last sentence blew past and perhaps it wasn’t as astonishing as it would have been to you a few years ago.  But today, it has become more commonplace and acceptable.  Its worth repeating, millions of messages per second.  Try standing up your own servers 10 years ago that could handle this volume of data, and at an affordable price.  This alone makes possible solutions for consultants such as myself to offer to small and medium size businesses with virtually no overhead and investment.  Where I would have (and have before) struggled by provisioning servers and/or hosting together with writing my own message based distributed system, I can now just spin up a hub and I’m good to go.

Indeed the inspiring Microsoft commercials featuring Common speak truth to the times we currently enjoy.  The future we look forward to.  The possibilities available to us.  We’re experiencing a time unlike I’ve ever seen before.  Its a great time to be a developer.

Telemetry data with Raspberry Pi 3, Windows 10 IoT core and Azure IoT Hub

I had a great time getting the Raspberry Pi 3 set up with Windows 10 IoT Core, a BME280 temperature sensor, and sending temperature data to Azure IoT Hub.  This was my first experience with each of these components, so I was learning as I went.  Working in technology, if you’re not used to or not comfortable learning as you go, you might want to take a step back and re-examine why.  Its well known that change is common in this field, and we should embrace the unknown as opportunities to learn and grow, not merely stay in our comfort zones.  Staying in your comfort zone will get you left quickly behind.  I’m reminding myself of this as much as anyone else, but I digress.

First off, Windows 10 IoT Core.  Mind blown.   Extra points for exceeding expectation.  This is a really useful  operating system geared toward IoT devices, including the Raspberry Pi.  I didn’t know what to expect, and was super stoked about how easy it way to set up.  I was expecting to have lots of trial and error, set up time, guessing, incompatibility and a steep learning curve.   But with Windows 10 IoT core, not only flashing my micro-SD to install it, but setting it up and getting it working with my Raspberry Pi was a breeze.   Here’s  a quick step by step to get started.

1. Download and install Windows IoT Core Dashboard
2. Insert your SD card into your computer and navigate to the ‘Setup a New Device’ tab in the dashboard
3. Select the version of IoT Core you want to install, a name for your device, and an administrator password
4. Click ‘download and install’. The image is downloaded from the internets and installed on the card

You’re good to go now.  Slide the SD card into your PI and boot it. The IoT Core Dashboard should recognize your PI on the network, and it will appear on the My Devices tab.   If it does not try reading this.    When you see the device in the IoT Dashboard, right click it to open a browser window, sign in with the admin user name and password you provided, and you will be interacting with the OS through the Device Portal.

Windows 10 IoT Core has some pretty neat tools built in that you can navigate to through the device portal.  You can view installed apps, which apps are running, as well as start and stop apps.   There are even sample apps you can deploy to your device and run.

Windows 10 Iot Core App Samples

View CPU, View memory, I/O and network usage

Viewing Resource Usage in the Windows 10 IoT Core Viewing Available Networks in Windows 10 IoT Core Device Portal

View available networks

Viewing Available Networks in Windows 10 IoT Core Device Portal

For this post, I’m going to demonstrate how I was able to use the BME280 with the Raspberry Pi and Windows 10 IoT Core to send temperature data to Azure IoT Hub.  One of the great things that attracted me to Winows 10 IoT core was the ability to write C# code to access, interact with and control the various components of the Raspberry Pi.  For this example, I was able to use Visual Studio Community Edition, and mix and match some samples I found online either through the sensor vendor or MSDN.

So now to the fun part and back to ‘mind blown’ when I was actually writing C#, not Java, not Python, not Node.js, C# to interact with the Pi, I/O pins and everything.  We’ve all got our favorite languages, C# happens to be mine.  Not only that, but I was able to send messages to the Hub.

The IoT Hub is interesting and revolutionary.  I say revolutionary because I remember not long ago when such concepts were not available and easily accessible to the masses.  There were no (limited) options to send data into a nebulous cloud endpoint, with virtually unlimited scale, and plug in any number of listeners which then can take action via any number of workflows or triggers.  There are solutions I’ve built in the past that could have benefited from using the Hub.  I wish I could go back and retrofit these solutions using the Hub now.   Sending temperature data, or any other data to the IoT hub, and for that matter, the Azure Event Hub kind of implements set and forget in a new way, sort of a send and forget.   Get your data up to the cloud and handle it there.  The Hub acts as an abstraction layer between ingestion and processing.  Moreso, a “component or service that sits between event publishers and event consumers to decouple the production of an event stream from the consumption of those events”  This is easily understated and misunderstood.  Its truly remarkable, and also made easily available at scale with Azure.

Let’s dive into the code.  I’m using an open source library to access the temp readings from the BME280 sensor.  First, I just new up a timer to call the _timer_Tick method when the app starts and the entry point is hit (OnNavigatedTo event of the main page)

Then in the timer tick event, I read the temp from the sensor

Then send it to the hub using the Microsoft Azure Devices Client

Don’t forget to new up your devices client with your hub connection string and device ID, which you get when you set up your hub and register your device.

To deploy this app to your device, right click on the project in Visual Studio, then click properties. On the properties page, select the Debug menu item, then set “Remote Machine” as your target device.  Set the IP address of your remote machine in the Remote Machine setting.  This will deploy the app to the IoT device, and allow you to debug it.

Deploying to Windows 10 IoT Core from Visual Studio

Now let’s see the data in real time as it arrives in the hub. To do this, we’ll download, install and run the Windows Device Explorer.   Enter your hub connection string on the connection string tab.   Your device will appear on the management tab.  Select that device, then click on the data tab and click monitor.  You should see the telemetry messages appear as they are sent to the hub.  Here’s mine:

View device to cloud messages using Device Explorer for IoT Hub

So there you have it. Now imagine scaling this to 1000 devices, then building a reporting and monitoring solution from this. Happy coding!