Windows 8 sensors
Visual Basic tutorials for UWP apps.
1 post
Page 1 of 1
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:
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:
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:
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](http://projects.comathi.com/sensors/unload.png)
You'll then need to go to your project's .VBPROJ file and open it in Notepad.
![Image](http://projects.comathi.com/sensors/vbproj.png)
![Image](http://projects.comathi.com/sensors/vbproj_loaded.png)
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
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 ;)<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
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
We're ready to begin!Imports Windows.Devices.Sensors
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
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:Dim aX, aY, aZ As Double 'Our acceleration values
Dim accelero As Accelerometer = Accelerometer.GetDefault() 'Our accelerometer
Dim reading As AccelerometerReading 'Our accelerometer reading
Code: Select all
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;If Not accelero Is Nothing Then
reading = accelero.GetCurrentReading()
aX = reading.AccelerationX
aY = reading.AccelerationY
aZ = reading.AccelerationZ
End If
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023