Windows 8 sensors

Visual Basic tutorials for UWP apps.
1 post Page 1 of 1
Contributors
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Windows 8 sensors
comathi
It's been a while since I've posted a tutorial, but I came across some code the other day I thought I'd share.

Windows 8 being targeted at mobile devices, it has methods and functions to access the various sensors tablets and phones might have: accelerometer, compass, gyrometer, inclinometer and light sensor.

This tutorial will work for both Metro and Desktop apps, but you need Visual Studio 2012 or 2013 (Express versions included) and Windows 8 for it to work. The functions used are also available in C#.NET, for those wondering, and the procedure is basically the same.

Getting started:
You'll first need to create a project (obviously lol). Once that is done, unload it by right clicking on your project in the Solution Explorer and selecting "Unload Project"

Image

You'll then need to go to your project's .VBPROJ file and open it in Notepad.

Image

Image

Since sensors can only be used on a Windows 8 device, we need to specify our project's target platform. This is done by adding a new PropertyGroup and a new property to our VBPROJ file:
Code: Select all
<PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
Save the file and go back to your project. You can now reload it the same way you unloaded it, but click the "Reload project" button this time ;)

The last thing we need to do before we start is add a reference to the Windows DLL. The DLL is typically located in the following location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Windows.dll

Once the reference is added, add an Imports statement to your project:
Code: Select all
Imports Windows.Devices.Sensors
We're ready to begin!

Reading data
For the sake of this tutorial, I'll demonstrate how to get linear acceleration data from the accelerometer. However, all sensors are used in pretty much the same way, and specific information about their individual functions and properties are available here: http://msdn.microsoft.com/library/windo ... es.sensors

The code can be put in any Sub or a seperate function.

Let's start by declaring a few variables:
Code: Select all
Dim aX, aY, aZ As Double 'Our acceleration values

Dim accelero As Accelerometer = Accelerometer.GetDefault() 'Our accelerometer
Dim reading As AccelerometerReading 'Our accelerometer reading
In the case the device the app is running on doesn't have the sensor we're using, we don't want the app to crash. Therefore, the reading will be conditionnal:
Code: Select all
If Not accelero Is Nothing Then
    reading = accelero.GetCurrentReading()

    aX = reading.AccelerationX
    aY = reading.AccelerationY
    aZ = reading.AccelerationZ
End If
That's it, really! You can now access data from almost any sensor the devices offers. Events for each sensor also exist, so once again, don't forget to check out Microsoft's documentation: http://msdn.microsoft.com/library/windo ... es.sensors cooll;
1 post Page 1 of 1
Return to “Visual Basic”