There is new metadata query support in Windows 10 to allow your code to react at runtime based on which APIs are available. For example if running on a phone device you may want to start a call, and offer alternative actions on a desktop PC. The correct way to check the availability is to use the ApiInformation class which has a number of methods which take the string name of the type or method and return a Boolean. Without delving into the question of whether a string is the correct approach lets look at an alternative issue.
You want to determine the device type you are running on – say Windows, Windows Phone, Xbox etc to change the type of content you display. You want this high level information not just to alter screen layout or use a specific API. Each Extension SDK provides the device specific APIs for the target devices. Currently the preview tools ship with Windows Desktop Extension SDK and Windows Mobile Extension SDK. If you look at the Object Browser in Visual Studio you can see that adding each of these adds additional root level entries (like a new DLL) and each named after a Contract which is a type contained within it alongside the associated APIs. So you want to quickly determine if you are running on a “Windows Phone” device you could use the following code:-
bool isPhone = ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract",1);
It’s not currently clear exactly which contracts will be present on which devices – the APIs provided by the Windows.Phone.PhoneContract lib are all familiar APIs present from previous versions of Windows Phone. Since “Windows Mobile” will also be the SKU used for small tablets it isn’t clear if these will also implement this contract or not. If no extension SDKs are added all UAP apps will support Windows.Foundation.FoundationContract and Windows.Foundation.UniversalApiContract.
Although not a part of the current preview tools one can expect a similar set of contracts for the other flavours of Windows – Xbox, IoT etc
In Silverlight 8.0 there was a capability to run an application for a period of time after the user switched away to perform continuous GPS tracking for up to a few hours. With the switch to the Windows Runtime there was no direct equivalent of this. I discovered however that because there are differences with what you are allowed to do on Windows Phone versus Windows Store apps with the DeviceUseTrigger you can do something similar.
The GPS device itself doesn’t qualify for use with the trigger but you can use another sensor on the device. Since all Windows Phone devices have an Accelerometer you can use this. The documentation states that you must poll the sensor at least every 5 seconds otherwise it will kill your background process. Since GPS commonly updates once per second you are given the choice of polling with a 1-5 second interval depending on what you need. With a bit of logic you could extend this but you’d still be polling the accelerometer at least once every 5 seconds.
The GPS adaptation is simply to recreate a DeviceUseTrigger based background task – using the official code sample as a base. Then we add a Geolocator and get the position on each call of the handler which is called for each reading. In the attached sample we simply capture the position every 5 seconds and update the application tile. So you’ll need to run the app and pin it to the start page to see it at work.
In order to show the minimal code to demonstrate the technique I’ve not added any decent error handling or analytics so don’t just paste this code into a real project and use it as-is.
Recently I open-sourced a number of Compact Framework projects and when I was working on re-writing an application which used them as a Windows Runtime app I started to think about how much of the code might be useful for app projects. Obviously the API surface for Windows Runtime is totally different to the .NET Compact Framework. In some cases functionality which I wrote is built into the runtime, in other cases I’d made use of P/Invoke to call native APIs which is not an option. However one thing I noticed was that the Windows.Devices.Geolocation namespace which supersedes System.Device.Location was missing one key feature – a built in method to determine the distance between two points. Since I had code for this which was using the same method as the .NET framework I thought this was a prime candidate for migration.
The Windows Runtime is a rather more complex API and whereas .NET had a single GeoCoordinate type the runtime has BasicGeoposition (containing raw latitude, longitude etc) and a number of other types – Geocoodinate, Geocircle, Geopoint etc I decided to first implement an extension method which would work with two BasicGeopositions and then as a helper added an extension method for Geocoordinate which used the logic from the first.
The methods now live in a new NuGet package currently supporting Windows 8.1 and Windows Phone 8.1 – Charming Geolocation and the code will be available in the Charming project. Just add:-
and you can use the GetDistanceTo methods.
Part of the project using this code required an iOS implementation too for which I’ve used Xamarin. Anyone who has used this will know that while the location functionality is similar to what we are familiar with on Windows the API is significantly different. Because I wanted to fix that just the once too I wrote a wrapper API to expose the Windows API wrapping all the CoreLocation stuff. I’ll be adding this library to the NuGet package shortly and am also in the process of moving all the Charming code from CodePlex to GitHub too.
I’ve been working on a Windows Phone 8.1 project which has several background tasks. One of these uses the device’s sensors – using a DeviceUseTrigger. This is different to how a regular periodic task works because the task implementation creates a deferral and keeps running handling the event generated by the sensor device until it is explicitly cancelled. The task is created normally with a class implementing IBackgroundTask and runs under the context of BackgroundTaskHost.exe. Because I wanted to share some data with the other tasks as a when they run I was interested if these other IBackgroundTask implementations were also hosted in the same process. If so this means that any static instances would be shared between them. Since I couldn’t find the answer I did some testing with some additional debugging and discovered that no, they each run in a separate process.
This lead me to explore how to share data between them. There are no built in IPC classes in the Windows Runtime like MessageQueues or similar so really the only options are the LocalSettings for individual setting values and files placed in the LocalFolder for any other data. The added complication here is that you have to handle the situation where both processes may try to read/write the file at the same time. Luckily you can use a named Mutex to enforce exclusive access to the file and have the other process wait on the Mutex.
In Windows Phone 8.1 a new API was added to both retrieve the current battery level as a percent of fully charged and also handle an event fired when the value changed. Sadly this API is not common across this and “big” Windows.
In a couple of projects I’ve needed to retrieve the battery level periodically so that the app can perform different actions depending on the device state. This encouraged me to “port” the API to Xamarin iOS which does provide the same capability but in a platform-specific way through UIDevice. When it came to Windows 8.1 there was an interesting problem. There is a native API you can call to query the battery level, however you can’t call it from within an app you submit to the public Windows store because the API is not on the Whitelist (http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx). For my initial use this was okay because the app was to be privately distributed. Sadly I don’t have a solution for a Store app. You may have seen various Store apps for battery which get around the problem by asking you to install a desktop “service” which retrieves the value and passes it to the store app. Why the API was not added to Windows 8.1 at the same time as Phone 8.1 is odd with the big push on “universal apps”, at the bare minimum the native API should have been whitelisted. It returns no identifiable information and is read-only. There are plenty of more malicious things that could be done via supported APIs…
Given the caveat mentioned above I decided to release my code in case it is of use to others. The iOS version is fully functional and can be use for public apps. An Android implementation will follow in due course…
Last Year Michel blogged about a binary incompatibility introduced in the May 2014 update of Compact 7 which broke existing Silverlight for Windows Embedded code including pre-compiled system apps.
The XAML In The Hand library was also affected. It turns out that one of the breaking changes was the addition of the GetTemplateChild method to the IXRControl interface. Previously this was present in the Compact 2013 codebase but not Compact 7.By adding a method to an existing COM interface (breaking the rules of COM again) means that the vtable offsets of all the methods in interfaces which inherit from this one are all shifted by one. Our next update will incorporate a fix for this (as well as exposing the GetTemplateChild functionality to the managed API) and will support Compact 7 from the May 2014 update onwards. The current released version of Compact 7 is the November 2014 update.
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.
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.
I recently got a Lumia 930 and was looking for a simple car holder/charger for it. I saw the CR-201 (which is updated from the CR-200 and advertised as compatible with the 930) and thought it would be perfect because:-
1) It has wireless charging so just pop the phone in, no fiddling with USB cables
2) It looks nice
However it doesn’t really fit the 930 and so this leads to a couple of issues – The clamp mechanism grabs the camera button, the sticky cable ties don’t stick to any material other than the plastic film they ship with and the wireless charger doesn’t deliver a charge capable of supporting charging while using the holder. No doubt this latter issue is also due to the size/positioning just not being right. Sadly it’s only after you discover these issues yourself you then find other reviewers online have encountered the same problems. So it’s going back and I’m looking for an alternative.
Last month I posted the latest “charming” helper for Windows development which is a Windows Phone (8.0 or Silverlight 8.1) ProgressRing with the same appearance as its big Windows counterpart.
Today I’ve just pushed an update to NuGet which improves the flexibility of the control by allowing you to override the Foreground colour of the rotating dots. The default is still to use the phone’s accent colour so this is what you’ll see if you don’t specify the Foreground brush explicitly. Details here:-