Wearables: building cross-platform wearable apps with Visual Studio and C# (Part 2)

Please see: Creating beautiful cross-platform apps for iOS, Android and Windows with Visual Studio. Part I. User Interface

(this is a multi-part series on mobile cross-platform technologies available with Visual Studio)

Microsoft Bing predicts wearables to be the hottest trend in 2015. You can check Bing Predictions technology trends right here: http://www.bing.com/trends/us/predictions/technology. According to these predictions, wearables in 2015 are even more important than personal digital assistants, home automation, 3D printing and virtual reality gaming.  This post continues the series on wonders you can do with cross-platform C# and Visual Studio tools on all kinds of devices: Android, iOS, Windows and now getting to even smaller form factors.

image16

So what are the wearables today: at least devices that are available to developers with existing or upcoming SDKs and APIs? Wearables are definitely in the IoT category: smartwatches, bands, things that we wear on our body to take body-specific measurements. The evolution of watches started a very long time ago, literally centuries ago, with the watch becoming more of a fashion item. Later, fitness bands created a new category of smart wearable devices.

image_thumb4

Let’s compare some wearable devices available today. If we attempt to classify them, they differ in sensors, functionality, price and purpose. In 2015 we will get an idea of market share each of them have, but the most interesting point: devices you see below can be accessed or programmed with C# and Visual Studio!

image_thumb7

Wearables APIs: What’s in common

From the app prospective, since wearables are so lightweight as a platform, they commonly make use of several API tiers that are more specific to small devices:

  • Actionable Notifications: wearables make an extensive use of notifications, including actionable notifications, i.e. those that call back into the host device app, like a phone. You can think of a actionable notification as a message with multiple attached actions-buttons. Android Wear SDK gives a good example of these.
  • Glance, Watchface, Tiles etc – ways to present readable information on the screen. Android SDK has CanvasWatchFaceService for example, which allows developers drawing the face. Microsoft’s concept of tiles is perfect for wearables developers.
  • Apps (optional) – some wearables may or may not have deployable apps (depending on the device and platform) . With apps come specialized controls that can present information on the small screens.
  • History (cloud APIs) – typically wearables synchronize with the phone device and that device in tern synchronizes with the cloud
  • Form factors: Microsoft Band is of band shape, with natural sequence of tiles. Android wear is round and square. Apple watch has a rectangular shape.

    Visual Studio and C# story with Wearables

    With Visual Studio and C# you can develop cross platform wearable apps that run on Android Wear, Apple WatchKit, and we hope to see Band SDK coming out soon. This makes Visual Studio a unique Wearable development platform for all devices. C# is a fantastic language, perfectly suitable for mobile and wearable development. In many ways it is easier to make apps with C# than with the native languages in each platform.

    You can start with these guides:

    Android Wear Developer Preview Now Available

    Introducing the Xamarin WatchKit Preview

    Practical examples of cross-platform apps built with C#

    Here’s a few apps I built with Visual Studio that run on wearables or are capable of interacting with wearable devices.

    Active Fitness

    Active Fitness is a cross-platform (Windows, Android, iOS) fitness, health and sport GPS tracking app and social network of more than a million users. Great for running, walking, cycling, hiking, mountain biking, skiing, snowboarding, urban sports, such as skateboarding, scooter and hundreds of other activities. Active Fitness motivates you by providing ways for sharing your activities with your buddies and friends. Active Fitness has leaderboards for any sports and activities and includes professionally designed training plans. Enjoy and stay fit!

     

    activefitness-watch4

    Winter Sports

    Winter Sports is a cross-platform (Windows, Android, iOS) app, the official app of Professional Ski Instructors of America and American Association of Snowboard Instructors (PSIA-AASI). Beautiful GPS tracking, ski run analytics, snow reports for thousands of resorts, zoomable resort maps, activity tracking, customizable goals, a social network, leaderboards and ski lessons by snow pros. The app includes many innovations, including voice guidance.

     

    ski-snowboard-watch4

  • Creating beautiful cross-platform apps for iOS, Android and Windows with Visual Studio. Part I. User Interface

    Download free Android, iOS or Windows Phone apps used in this article and GET FIT!

    See also this article on my MSDN Blog

    Cross platform apps: dream becomes reality

    With recent additions to Visual Studio 2015, it becomes possible to develop apps for all 3 major platforms: iOS, Android, Windows, and also target desktop Windows and OSX. Clearly, the world is changing and I’d like to share my experience in developing rich cross-platform apps that look great and work on iPhone, Android and Windows. Yes, you heard it right, it is now possible to create rich code that compiles to native bits on each platform, with only one code base (or one developer team, with 80-90% code reuse!), all with Visual Studio, C# and .NET. This is very powerful, and clearly developers benefit from this approach by Microsoft: it greatly increases productivity even for small app development teams and allows developers to create native apps that work across all major platforms.

    I’ll share more of my experience developing Active Fitness app (http://activefitness.co) for Android, iOS and Windows. You can download the app and see for yourself by clicking the links above and downloading the app for the device of your choice. I want to share the experience of developing these apps, because I feel that it’s a revolution or rather a natural evolutionary step in app development that changes our perception of 3 operating systems incompatibility, and instead focuses on making developers productive.

    What’s especially important, Microsoft provides Azure Mobile Services cross-platform libraries for Android, iOS, Windows Phone, Windows and Xamarin, so that developers of cross-platform apps can use cloud resources efficiently.

    Designing cross-platform user interface

    Mobile user interfaces have changed dramatically since the first iPhone. Windows has Modern Design and Google has Material Design, while Apple made significant changes to its user interface in iOS 8. What is common? Designers call it dropping skeuomorphism, or using material world objects in user interface graphics, and using clean functional design paradigms instead. I like calling a cross-platform combination of Modern Design, Material Design and Apple Design guidelines a pragmatic design, but I don’t pretend that I own the name.

    image
    From the very beginning I decided to create a common interface design that would satisfy design guidelines of Apple, Google and Microsoft. With Xamarin Forms, a recent addition to Xamarin cross platform suite, available now directly from Visual Studio, it becomes possible, with code re-use close to 80-90%, including user interface! This is a stunning achievement, given that most shops that make apps these days have very experienced developers that know iOS very well, then they need to hire developers that know Android and Windows. The cross-platform native approach taken by Xamarin Forms and other cross-platform tools changes the way developers make apps!

    The three screenshots here are from (left to right) Android, iOS and Windows Phone Active Fitness app. They are developed with the same code base, with 100% C# and XAML and naturally fit Apple, Google and Windows ecosystems. The main approach was: keep it simple: keep it beautiful! Modern lines in this design naturally blend with Google Material Design, Windows Modern Design and Apple design, because this design is clean and functional.

    Xamarin Forms introduced a set of controls: Pages, Views, Layouts and Cells that blend naturally with all three platforms: Android, iOS and Windows and provide a natural user experience. What’s even more important: one developer or a very small team of developers can now develop for all 3 major platforms (plus OSX and Windows Desktop), with one code, and one language! You can look at Xamarin Forms Gallery to get a feel of what cross-platform controls are available.

    Pages

    Pages are very natural part of Web sites, as well as native apps in iOS, Android and Windows. Xamarin Forms offer a wide selection of Pages.

    Active Fitness app takes advantage of MasterDetailPage which is very popular with Android and iPhone apps. The action bar is also very similar in Android and iPhone apps. In Windows Phone we’re more used to bottom application bar. The “sandwich” icon is becoming very popular to present a “drawer” navigation pattern.  In Xamarin Forms you can use Master Detail page to take advantage of this design. If you look at some of the latest designs for Windows Phone: Facebook app, or OneDrive app implement drawer design pattern, which is also very common in Android and iOS.

     

    clip_image008

    Layouts

    Layouts are ways to align and position controls on your user interface. Most common layouts are grids, stacks, relative or absolute layouts. It’s interesting that while Xamarin Forms use XAML as a metadata language, most layouts and controls are actually very similar to Android AXML objects. Android developers building cross-platform apps find themselves right at home by using Xamarin Forms!

     

    Views

    Views are often called UserControls in Windows apps. With the cross-platform nature of Xamarin Forms, there’re many views: for labels, entry controls, buttons, date and time pickers, progress controls, and they all look native on the platform you compile the app for:

    Cells

    Xamarin Forms also provide cells. Cells are specialized elements used for items in tables.

    Summary

    The cross-platform apps in this example are developed in XAML and C# with .NET that natively runs in iOS, Android and of course Windows. .NET has a long history of open source development, and it now becomes official with Microsoft integrating cross-platform tools with Visual Studio, open sourcing .NET. Many popular 3D games also run on multiple platforms backed by .NET and C#, with Unity. Game designs are very different from other apps, so we’ll focus on Xamarin Froms as technology suitable for cross-platform apps. Interested in this post?

    Please, let me know by sending a cheerful COMMENT or twit (@kashleytwit) and I’ll keep this series to tell you more about making beautiful cross-platform apps for Windows, Android and iOS. In the following posts I’ll show you how to make cross-platform apps work with speech, voice, storage, GPS, sensors and more!

    Download free Android, iOS or Windows Phone apps used in this article and get fit!

    Push Notifications for Xamarin.iOS

    Configuring Notification Hubs for Xamarin.iOS apps

    Some user devices are iOS-based, some Android, some Windows Phone, and the list goes on. Each of the native platforms has its own push notification framework and backend service. Azure Mobile Services allows the developer to configure a single Notification Hub (based on Microsoft Azure Service Bus Notification Hubs) through which notifications may be sent. The notification goes to each user via the correct push service according to her device’s requirements. Broadcast to millions in consumer scenarios, to tagged groups, to individuals with the same ease.

    To use notifications hub from Azure Mobile Services with your iOS apps, you need to generate a certificate, register your iOS app for push notifications, save your certificate to a file and upload to Azure Mobile Services Management Portal, into your Notifications Hub. The whole process of generating a certificate, and configuring Azure Notification Hubs for use with iOS apps here, which is fairly extensive and has some screenshots.

    Code snippet: Xamarin.iOS app using Push Notification Hubs

    We use Xamarin Forms for best cross-platform portability: we can re-use the same UI and most of the middle tier in our Android project as well.

    1. In Xamarin Studio on your Mac, or Visual Studio on your PC, create a new Xamarin Forms project. We will need to use WindowsAzure.Messaging from GitHub. Compile WindowsAzure.Messaging.dll and reference it in your project. This assembly provides iOS bindings for Notification Hub:

    clip_image002

    2. For Xamarin.iOS project, we need to make some changes in the AppDelegate.cs file. First, start by adding:
    SBNotificationHub _hub;

    public const string _azureConnection = “<Azure connection string>”;

    public const string _notificationHubPath = “<Azure hub path>”;

    3. Next in FinishedLaunching method add this to register for remote notifications in your app (providing several types of notifications):

    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(

    UIRemoteNotificationType.Alert |

    UIRemoteNotificationType.Badge);

    4. Add the following methods to handle registration and notification processing.

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

    {

    _hub = new SBNotificationHub(_azureConnection, _notificationHubPath);

    _hub.UnregisterAllAsync (deviceToken, (error) => {

    if (error == null)

    {

    NSSet tags = null; // create tags if you want

    _hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>

    {

    // handle errors here

    });

    }

    else

    {

    // handle errors here

    }

    });

    }

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary options)

    {

    if (null != options && options.ContainsKey(new NSString(“aps”)))

    {

    NSDictionary aps = options.ObjectForKey(new NSString(“aps”)) as NSDictionary;

    string alert = string.Empty;

    if (aps.ContainsKey(new NSString(“alert”)))

    alert = (aps[new NSString(“alert”)] as NSString).ToString();

    if (!string.IsNullOrEmpty(alert))

    {

    UIAlertView avAlert = new UIAlertView(“Notification”, alert, null, “OK”, null);

    avAlert.Show();

    }

    }

    }

    We can now register for hub notifications and receive notifications, displaying alerts in our Xamarin.iOS apps.

    Reference:

    Get started with Notification Hubs with Xamarin.iOS
    Xamarin.iOS: Push Notifications in iOS

    Native Cross Platform UI Ideas for iOS, Android, Windows Phone with Xamarin

    For many developers using C# and coming from the modern UI apps on Windows, developing cross-platform UI design with the best usability for Windows, iOS and Android presents an interesting challenge. First, they feel like they are coming to a more ‘traditional’ UI: let’s face it Microsoft team has created some very advanced and neat design concepts with the modern apps: with Modern design language, minimalism, panorama, motion animations and pivot. How do you translate that into iOS and Android?

    Luckily, Xamarin team has done a lot of research porting and thinking through many of these ideas. Xamarin Forms are based on XAML and work on iOS, Android and Windows Phone natively. Here’s some interesting ideas to keep in mind building a cross platform app with Xamarin Forms:

    Is cross-platform UI achievable and does it work fast and look native? Thanks to Xamarin team’s efforts the answer to this is yes! You can write UI code that will look and feel native with native speeds on iOS, Android and Windows Phone.

    Things to remember: Forms markup uses XAML, but Xamarin team chose to use objects from iOS and Android stack. So instead of StackPanel you get StackLayout, TextBlock is Label. Attributes also resemble those of Android AXML: Visibility becomes IsVisible (we actually like that, it eliminates a converter which otherwise needs to be used on each boolean property). Background color property becomes BackgroundColor, Foreground property becomes TextColor, ForegroundColor etc depending on the context. In other words: instead of cut and paste from Windows Phone you need to really update your XAML. Which makes a lot of sense, Xamarin team chose to use Android/iOS UI elements in XAML.

    Xamarin also figured out a very smart strategy wrapping native controls, while maximizing code re-use. On the screenshot below, Xamarin Forms maps control renders as a native Android, iOS and Windows Phone map on each platform.

     

    Source: Xamarin: Controls Gallery