Using Windows Phone As Windows 7 Accelerometer Sensor

August 13, 2011

I just had a cool idea! (actually I had it like 6 month ago, but bear with me).

My idea involves mixing some of the features in Windows 7 with some of the features in Windows Phone 7, to create a glorious mix!

image

 

Windows 7

One of the new features in Windows 7 is the Sensor API which provides a standard interface for accessing sensors of various types, like accelerometer, etc..

I wrote in the past about how you can consume this API from a C# application using Windows API Code Pack.

 

Windows Phone 7

Windows Phone 7 comes with a few sensors out of the box, among them: accelerometer!

 

Idea

What if we use Windows Phone 7 accelerometer sensor as a standard device on Windows 7?

 

Roadmap

So how can we accomplish that? let’s break the idea to steps:

Step 1: Create a software driver

I’ll start with creating a dummy software driver. This driver will implement what’s necessary to be a standard Windows 7 Accelerometer sensor, but at this stage will simply return constant data.

In order to write a sensor driver for Windows 7 I’ll need to use User Mode Driver Framework (UMDF), a COM-like, C++ framework for writing user-mode drivers in Windows 7.

Step 2: Transfer Windows Phone 7 sensor data into the PC

The next step is to find a way to transfer the accelerometer data from a Windows Phone device into the PC. One possible way of doing this implementing a local WCF service on the desktop that the Phone will keep calling with accelerometer data. This will require an application to run on the phone to keep the data flowing.

Step 3: Consume the WCF service from the phone

Using the service from the previous step we can now consume it in a simple Windows Phone application that all it does is getting the accelerometer sensor data using the phone API and call the WCF service with the latest data.

Step 4: Make the software driver consume the Windows Phone 7 data

Now that I have a software driver that returns dummy accelerometer data and a way to get real accelerometer data from the Windows Phone, all I need to do is connecting the dots and make my accelerometer driver return the real data. To do this I’ll need some kind of inter process communication to transfer the data from the WCF service to the driver.

 

Long Story Short…

I present to you the Windows Phone Accelerometer Driver for Windows 7!

The full source code for this project can be found on the CodePlex project site.

 

Some Implementation Details

The actual driver was fairly easy to implement since I used the CodeProject article "Writing a Sensor Driver for the Wiimote on Windows 7" by Rajasekharan Vengalil as a base for my Windows Phone 7 driver. You can find the driver source code in the project named WP7AccelerometerDriver.

As mentioned earlier I’ve implemented a simple local WCF service for transferring the accelerometer data from the phone to the driver. You can find the service source code in the project named WP7AccelerometerService. The service uses named pipes to communicate with the driver.

The phone application is a very simple one that consumes the WCF service and call it whenever a new accelerometer value is available. You can find its source code in the project named WP7AccelerometerClient.

Finally, if you want to check the functionality of the accelerometer driver you can use either the SensorDiagnostics project or the more fun PlaneDemo project, which I borrowed for Sergey Zwezdin blog.

 

How to Install?

1. Download and extract project binaries or source from the CodePlex project site.

2. Open CMD as administrator

3. Go to the folder \Driver\Install\

4. According to the bitness of your OS Run install_wp7_driver_32bit.bat or install_wp7_driver_64bit.bat to install the driver.

image 

5. When the following notification appears, select "Install this driver software anyway":

image
This should take around 1-2 minutes to complete.

6. Enable installed Sensor in the Control Panel

image 

7. Run the WCF service from as administrator. If you run it from Visual Studio, make sure you ran Visual Studio as administrator.

image

 

8. Run the Windows Phone client on a real device while connected to the PC and enter your PC (LAN) IP

image

 

9. That’s it! you can now sit back and enjoy the ride using the PlaneDemo application or SensorDiagnostics, or any other software that uses the accelerometer sensor.

image

image

 

How to Uninstall?

If you want to uninstall the driver simply go to the Device Manager, select the "WP7 Accelerometer Sensor" device from the Sensors category, select uninstall from the context menu and make sure you mark the checkbox to delete the driver.

image

 

Final Note

I’ve seen the project works both on a real device and using the phone emulator, however it’s not a one-click install. I expect users to have problems with the installation. If you encounter some problems, please post on the CodePlex project site forum page and I’ll do my best to help.

Also, if you want to improve the source code (maybe adding GPS support?) or the installation procedure, I’ll be happy to include your improvements and even include you as one of the project members on CodePlex.

This project was pure fun since it combines many useful technologies to create real value. Among the technologies you can find: C++, COM, UMDF, C#, WCF, WP7, etc..

That’s it for now,
Arik Poznanski.

Add comment
facebook linkedin twitter email

Leave a Reply

13 comments

  1. arikAugust 15, 2011 ב 12:47

    Just a clarification, the implementation took 3 nights and not 6 months.. It just took e 6 month to decide to release it..

  2. shAugust 15, 2011 ב 16:58

    Any plans to release the WP7 client to the market place?

  3. Tishawn FahieAugust 15, 2011 ב 16:58

    Wow Very nice!!! I will play with this… I actually created a PC game using XNA that uses keyboard controls maybe I can use this to control the game? What do you think? I love my Windows Phone Even More now!!

  4. AlexYakAugust 15, 2011 ב 18:55

    Arik,

    You should be able to use sockets on Mango and desktop for a better throughtput of the sensor data from the device.

    -Alex

  5. arikAugust 16, 2011 ב 01:05

    sh, not sure, most of the users of this driver will be developers anyway, so they can use it on their unlocked device…
    If it has demand I’ll release it freely to the market.

    Tishawn Fahie,
    Thanks. Indeed you can use it as a controller for your game.
    Good luck.

    AlexYak,
    You are absolutely correct. If their enough demand for it I might just do that. This will help get rid of the WCF service altogether, since I can have the driver listen on the socket itself.

  6. Tishawn FahieAugust 16, 2011 ב 17:33

    Yes i hope there is enough demand.. Thankyou very much. I will give it a try. Not sure how I would implement it. I guess i would have to figure out a way for the Axis controll’s to emulate keyboard controls….

    Basically my game is a side shooter so with the keyboard arrows i can move the player up down left and right…. With this I can use the My Developer Phone as a controller… Sounds really good i will try it out.. and give you feedback.

  7. Tishawn FahieAugust 16, 2011 ב 21:59

    I was able to install the Drivers and get everything working Successfuly! The plane controll works greate… Thanks for including the source code.. For the plane demo.. I will dig around and see if i can add that control to my game I will get back to you.. One question I also wanted to ask – I thought If the phone was on the same wifi network as the Computer just entering the computer IP Address would work.. Turns about I have connect it with a usb.. No problem there.. Is there a way I can have work through wifi?

  8. arikAugust 17, 2011 ב 10:44

    Tishawn Fahie,

    Theoretically, it shouldn’t have any problem working wirelessly. All you need is to be able to consume the local WCF client from the phone application. If it doesn’t work then something might be interrupting, maybe a firewall or NAT or something else. you can debug the phone app and check the exception details..

    Good luck,
    Arik.

  9. Tishawn FahieAugust 17, 2011 ב 16:46

    Thankyou very much I will let you know how it goes!

  10. Tishawn FahieSeptember 13, 2011 ב 04:52

    IF you have some time would you be able to create a Wifi version of your drivers Since Windows phone now support sockets via UDP?

  11. arikSeptember 16, 2011 ב 04:18

    Tishawn Fahie,

    I would like to, but unfortunately I don’t have the time.
    But you have the code on codeplex and you can do it yourself…
    If you do, I would be more than happy to integrate it back to the codeplex project.