Categories
Alexa Uncategorized

Alexa Skill with Azure Functions – Messaging

In the previous Alexa post I talked about building a List skill to integrate with a third-party list provider. This gives you a mechanism to react to changes in Alexa’s lists and write them to your external provider, but what about implementing a two-way sync?

When you setup account linking for your skill the user goes through an OAuth flow to authorise your app and this returns a token and a refresh token to Amazon. The Alexa infrastructure manages this securely and handles the refresh process for you. Therefore only your skill function can continue to access your third-party service. Therefore when you have a callback mechanism from your third-party provider you need a way of passing the change information into your skill to be processed. Luckily there is a messaging service to do this.

As with the list functionality there is a library to handle the messaging requests – Alexa.NET.SkillMessaging. The code you use to send the message will have to have the client id and client secret of your skill – you can find these from the Alexa Developer Console on the web.

var client = new AccessTokenClient(AccessTokenClient.ApiDomainBaseAddress);
var accessToken = await client.Send(clientId, clientSecret);

This access token can then be used to send messages to your skill. Each message consists of a payload which is a Dictionary<string,string> and a timeout. You create an SkillMessageClient and send the message to a specified user id. The Amazon user id is given to you when your skill is first enabled and the account is linked. The id is specific to the skill and cannot be used to personally identify a user.

var payload = new Dictionary<string, string> { { "Key", "Some Value" } };
var messageClient = new Alexa.NET.SkillMessageClient(alexaEndpoint + "/v1/skillmessages/users/", accessToken.Token);
var messageToSend = new Alexa.NET.SkillMessaging.Message(payload, 3600);
var messageId = await messages.Send(messageToSend, userId);

An extra complication is that there are multiple API endpoints for the SkillMessageClient depending on the region. This means you’ll have to store the endpoint along with the alexa userid so you know which to use for a specific user. If successful a unique id for the message is returned. In your skill code you have to enable support to recognise the incoming message and handle the action. In the case of a list change event from a third-party provider this would be to load the specific changed item and then write the values to the Alexa list.

As with the list support we need to register the messaging library so that the skill request can be correctly deserialised into a MessageReceivedRequest.

RequestConverter.RequestConverters.Add(new MessageReceivedRequestTypeConverter());

Then when reading your incoming request you can check the request type and add code to process the message. The MessageReceivedRequest contains a Message property with the dictionary of values sent from your other function. The user id is already included with all incoming requests in the Context.System.User.UserId property.

Combining this with the list support already discussed you can see how to use the ListManagement API to write changes into the Alexa lists.

Categories
Uncategorized

Latest Version of InTheHand.Core

I’ve just updated the NuGet package for InTheHand.Core. This adds a VibrationDevice which works across all Xamarin Platforms (hardware permitting) and fixes a bug with PowerManager.RemainingChargePercent on iOS. The library now has 18 namespaces full of UWP APIs for all Xamarin platforms (and this doesn’t count the sister library InTheHand.UI). Check it out here:-

https://www.nuget.org/packages/InTheHand.Core/

Categories
Uncategorized

Charming Apps – Latest Version

It’s been a few months since I did a NuGet release but lots has been going on in the code in the mean time. The focus this year has been less about harmonising Windows APIs across versions (although that’s still there of course), but widening the functionality to the Xamarin platforms. The result is that the vast majority of the APIs are now available across Android, iOS, Windows (UWP and 8.1) and Windows Phone (8.0, 8.1 Silverlight and 8.1 WinRT). In many cases I’ve added “light-up” support allowing Windows 10 functionality to be called from Windows (and Phone) 8.1 projects. This means you can write for maximum compatibility but take advantage of new features where available. The two packages are:-

https://www.nuget.org/packages/InTheHand.Core/

and

https://www.nuget.org/packages/InTheHand.UI/

As the name suggests the UI library separates out stuff which uses UI and so you can just use the former in a background task without worrying about dragging in UI dependencies.

The APIs are all modelled on UWP, the only difference being the root namespace is InTheHand rather than Windows and async methods return Task<T> rather than IAsyncOperation<T>.

The Core library features many namespaces around ApplicationModel – Interrogate app package metadata, make calls, send SMS and Emails, interact with Clipboard and Sharing. Beyond this are classes which take away the need to know which APIs to call on which platform – interrogate display properties, check network connectivity, read and write files and settings. This version extends the support for settings by supporting roaming settings on iOS just like Windows does – except these settings are roamed across the iCloud infrastructure rather than OneDrive. It’s important to add here that these settings only roam for devices on that specific platform type – Windows and iOS roaming data exist in separate worlds so data roamed from your iPhone Xamarin app won’t roam to your Windows laptop.

AnalyticsInfo is available and can report the OS version on iOS/Android and Windows 10. This is supported in the 8.1 libraries if running on the newer OS version. DisplayRequest allows your app to ask the device to keep the screen on – for example for a reader or video player. PowerManager gives you battery information.

Over in the InTheHand.UI library there are Badge and Toast notification APIs (Badge is not supported on Android). BadgeNotificationCreator and ToastNotificationCreator helper classes make it easy to create these notifications without having to deal with XML templates on Windows. A MessageDialog and a StatusBar which allows you to toggle a standard busy animation. On Windows there is also a unified SettingsPane API which uses the best available appearance depending on platform.

Going forwards I’m looking at removing the Windows Phone 8.0 (and possibly 8.1 Silverlight) target to simplify things and would be interested in any feedback on that. As always all the code is on GitHub:-

https://github.com/inthehand/Charming

Categories
Uncategorized

Add Sharing to your UWP App

With Windows 10 the original Charms introduced with Windows 8 have gone. Old Windows 8.x apps will display a window menu with those commands if supported in the app e.g.

Window menu on Windows 8.1 Apps
Window menu on Windows 8.1 Apps

You’ll also notice the icon has changed in Windows 10 to a kind of “three person group hug from above” image whereas they had a kind of clockwise movement to them in Windows 8.1. When you take a look around the first-party apps in Windows 10 many make use of the same Share functionality but it is implemented within the app UI, either in an AppBar or somewhere relevant within the body of the app. For example Edge displays the Share button prominently at the top right of the window. The Store app displays a Share link inside the details for a specific app. Once you tap this the share experience is very similar to Windows 8.x.

Windows Phone never supported charms but did provide the same sharing API from 8.1.  My Charming project originally started out by providing these for WP8.0 apps. Apps would have to add a Share button to the AppBar or menu and trigger the full-screen sharing experience. Moving your app to Windows 10 you will want to make use of sharing wherever it is relevant to your app. Taking an approach like Edge would mean adding a TopAppBar. You might use something like the following XAML:-

<Page.TopAppBar>
  <CommandBar>
    <CommandBar.PrimaryCommands>
      <AppBarButton Label="Share" Icon="Share" Click="AppBarButton_Click"/>
    </CommandBar.PrimaryCommands>
  </CommandBar>
</Page.TopAppBar>

Until you realise that there isn’t an Icon called Share. It’s not a member of the Symbol enumeration which defines common symbols taken from the Segoe MDL2 Assets font. Luckily the actual glyph is available and we just need to use slightly longer syntax to specify it using a FontIcon:-

<Page.TopAppBar>
  <CommandBar>
    <CommandBar.PrimaryCommands>
      <AppBarButton Label="Share" Click="AppBarButton_Click">
        <AppBarButton.Icon>
          <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="& #xE72D;"/>
</AppBarButton.Icon>
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.TopAppBar>

That defines a visual element to start sharing, there are two tasks to be completed to allow successful sharing:-

The handler for that button click must call the API which displays the Sharing prompt on screen. On Windows PCs this pops out from the right-hand side and on phones it is a full page view. The code can be as simple as this:-

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
   Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
}

The other vital part of the process is to setup an event handler for the DataTransferManager which is called when the Share screen appears and asks your app for available data:-

Windows.ApplicationModel.DataTransfer.DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;

The handler for this merely checks the presence of some text and if available shares it:-

void MainPage_DataRequested(Windows.ApplicationModel.DataTransfer.DataTransferManager sender, Windows.ApplicationModel.DataTransfer.DataRequestedEventArgs args)
{
   if(!string.IsNullOrEmpty(ContentText.Text))
   {
      args.Request.Data.SetText(ContentText.Text);
      args.Request.Data.Properties.Title = Windows.ApplicationModel.Package.Current.DisplayName;
   }
   else
   {
      args.Request.FailWithDisplayText("Nothing to share");
   }
}

This is only a very basic example. The package title is merely the app name. Imagine you have a news app – you may want to share the article title and a Url to the article on the web. You will probably want to control the availability of the Share button. For example in Edge the button is disabled while a page is loading and enabled once your sat on a finished page.

By adding a Share button to your app you open it up to a variety of targets with very little investment – suddenly you have a mechanism to share your app content across social networks, email and productivity software like OneNote. Unlike Windows 8 you have the power to control the visibility and location of the share command so it fits best with how your app works – but it makes sense to use the same icon as your users will be familiar with this from elsewhere in the system.

Categories
Uncategorized

.NET 4.6 and DateTime extras

In case you missed it there is a great blog post on .NET 4.6 which is a part of Windows 10. Among the various performance and Hi-DPI improvements there are some more subtle enhancements. Perhaps as a nod to Microsoft’s new openness to other platforms there are some helper methods on DateTimeOffset for converting to and from UNIX times. These are represented as the number of seconds since 00:00 on the 1st of January 1970. I’d already come across situations where I needed this and had written a couple of simple conversion methods. They come in useful when doing interop with Android APIs for example. Why not match the .NET 4.6 API I thought so slightly tweaked them and put them in a Gist here:-

using System;
namespace InTheHand
{
/// <summary>
/// Helper class for DateTimeOffset.
/// </summary>
public static class DateTimeOffsetHelper
{
private static DateTimeOffset dt = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a DateTimeOffset value.
/// </summary>
/// <param name="seconds">A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC).
/// For Unix times before this date, its value is negative.</param>
/// <returns>A date and time value that represents the same moment in time as the Unix time. </returns>
public static DateTimeOffset FromUnixTimeSeconds(long seconds)
{
return dt.AddSeconds(seconds);
}
/// <summary>
/// Returns the number of seconds that have elapsed since 1970-01-01T00:00:00Z.
/// </summary>
/// <param name="date">The DateTimeOffset value.</param>
/// <returns>The number of seconds that have elapsed since 1970-01-01T00:00:00Z. </returns>
/// <remarks>Unix time represents the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at 12:00 AM UTC).
/// It does not take leap seconds into account.
/// <para>This method first converts the current instance to UTC before returning its Unix time.
/// For date and time values before 1970-01-01T00:00:00Z, this method returns a negative value.</para></remarks>
public static long ToUnixTimeSeconds(this DateTimeOffset date)
{
return Convert.ToInt64(date.Subtract(dt).TotalSeconds);
}
}
}

Categories
Uncategorized

iRAPP Remote Desktop

Firstly, just to clarify, I don’t rap – this is a post about a useful Remote Desktop server for OSX. Wait, I hear you cry, you’re a Windows developer! Well that is true but I also use Xamarin to produce apps for iOS and Android and to build and deploy iOS apps you have to have a Mac in your workflow. Xamarin have done a great job to minimise this – you can do your development in Visual Studio on your PC but you have to connect to a Mac running their Build Server and you need to use the Mac to deploy apps to the iTunes store.

Because it was only for occasional interaction I don’t want the Mac Mini (which is a very nice looking piece of hardware BTW) setup with a Keyboard/Mouse/Monitor taking up space so it’s running as a headless device. I originally used TeamViewer to occasionally connect to the device but randomly last month the PC client started crashing on load and even an uninstall/reinstall wouldn’t get it working again so I looked for an alternative. I came across iRAPP by CodeRebel. I’d not heard of it before but it seemed perfect – it’s a Remote Desktop provider supporting Microsoft’s RDP protocol which means you can connect using the standard Windows Remote Desktop app. The app is available for Windows, Windows Phone, Android, Mac and iOS and since I use other Windows machines I have it installed on every device I use. By installing iRAPP on my Mac it just works in my Windows environment with no hassles. I found it very reliable over the trial period and have just purchased a license – $79 for a year and even that process is straight-forward and just works. Purchase the license online and click Update License from the iRAPP control panel and boom it’s up and running again!

There is also an iRAPP client for Windows which adds an extra dimension – it allows you to “blend” your desktop – switching to it adds the OSX menubar and dock to your desktop and will open apps in windows transparently on your desktop so you can feel like you are running OSX applications side by side with your Windows applications. Fun stuff but I didn’t really need this.

If you’re interested take a look here – http://www.coderebel.com/products/irapp-client/. There’s no ulterior motive here – I’m not receiving a commission or anything, I was just really impressed by the product and it’s proved reliable and useful.

Categories
Uncategorized

Calendar Import Now Universal

In order to extend the app onto Windows devices I’ve re-written the Calendar Import app as a Universal app with separate UI and features for Windows 8.1 and Windows Phone 8.1. Both apps add the new feature of browsing for files to import from directly within the app itself. This is on top of the existing filetype support which means you can import an item directly from your Email attachments, Web Browser, NFC, Bluetooth etc

Switching to a Windows Phone 8.1 project also means there are other new features we can support in upcoming releases. Users with a Windows Phone 8.0 device will continue to receive the 1.8 version until their device is updated to 8.1.

WindowsStore_badge_en_English_Black_small_154x40

Categories
Uncategorized

Microsoft Band Initial Thoughts

I received my Band today and have just started setting it up, loading in my Starbucks card and tweaking settings. One thing which is interesting is that the phone doesn’t use GATT Bluetooth LE to expose functionality unlike some other wearables. Instead it has three RfComm services named:-

  • Cargo Service
  • Cargo Service 2
  • Push Service

Cargo is a code-name for the Microsoft Band and appears in a few places…

Because the screen is flat it makes the band feel very wide and I have the large size so I can only imagine this is more pronounced in the smaller sizes. I think a portrait orientation for the watch screen would be quite useful…

I thought the Starbucks integration would be some clever NFC magic but it is simply a 2D barcode displayed on the screen. This could be replicated for any loyalty card or similar provided they have a scanner which can read from a screen (Imager rather than laser scanner).

The Cortana integration is very slick and is even able to wake your phone and activate Cortana bypassing the lock screen. Whoa that’s a security risk you may think – well if you try and navigate away from Cortana you’ll then get your lock screen.

I haven’t had a chance to test the UV capabilities but since I’m in Seattle and it’s a dark and rainy November evening I’m not too worried about over-exposure at the moment.

Categories
Uncategorized

Tasks v1.19

The latest update to Tasks is now available in the Marketplace. This release continues the improvements to the sync experience by providing more resilience in situations where the network drops out during a sync. It also improves the reminders feature in Windows 7.5 and improves the display of reminders for tasks with very long titles.

http://www.windowsphone.com/en-GB/apps/55fcf880-6b03-e011-9264-00237de2db9e

 

Categories
Uncategorized

Developer Unlock Expiry

When you’ve developer unlocked your Windows Phone you can merrily deploy and debug on it and everything is great. If you’ve upgraded to Mango you had to run the Unlock tool again but this is fairly painless. What is not obvious is that the developer unlock has a 12 month expiry from when you first register the device and even when you subsequently use the unlock tool on the same device this never gets extended.

So one day you’ll be developing as normal and get the error “Failed to connect to device as it is developer locked. For details on developer unlock, visit http://go.microsoft.com/fwlink/?LinkId=195284.”. Instictively you launch the developer unlock tool, enter your App Hub credentials and unlock the phone. This completes with no errors but you’ll soon find that it hasn’t helped despite the fact you entered valid credentials and your App Hub account is valid.

The solution to this problem is to log into App Hub on the web and go to your account profile, select devices and remove the offending device from the list (here you’ll see that the expiry date is shown). After doing this you can run the unlock tool again and successfully unlock the device.

Microsoft could certainly improve this process in the future by using a more descriptive error message and also by improving the unlock tool so that when you try to unlock a device that has expired it refreshed the registration and provide 12 more months of use (just like the manual process described above does).

Merry Christmas, and here’s to a wonderful 2012 filled with Windows Phone goodness!