Hi all, I had the same problem and I found a workaround to access accelerometer data. Unfortunately Microsoft doesn't support standard HTML5 deviceorientation and devicemotion events, so you have to deal with deep code changes in your visual studio project.
The following code snippets are simply a workaround to pass accelerometer data from C# code to the webbrowser control in an extremely unelegant way. You may notice some performance drop and you'll have to deal with trigonometry to obtain correct values from sensors.
I needed only touch.beta values on a landscape app, if you are familiar with Visual Studio and Trigonometry you can mod this code to obtain also alpha and gamma values.
1) Export your game as a Windows Phone 8 project without minifying the script.
2) Open Mainpage.Xaml in VS2012 and add the following line before the end of the tag phone:WebBrowser
ScriptNotify="Browser_ScriptNotify"
3) Open Mainpage.Xaml.cs and search for this.
public void OnAppDeactivated()
{
Browser.InvokeScript("eval", "if (window.C2WP8Notify) C2WP8Notify('deactivated');");
}
Add this snippet under these lines of code.
private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "startAccelerometer")
{
if (accelerometer == null)
{
accelerometer = new Microsoft.Devices.Sensors.Accelerometer { TimeBetweenUpdates = TimeSpan.FromMilliseconds(100) };
accelerometer.CurrentValueChanged += (o, args) => Dispatcher.BeginInvoke(() =>
{
var x = args.SensorReading.Acceleration.X;
var y = args.SensorReading.Acceleration.Y;
var accbeta = (Math.Atan2(-x, y) * 180.0 / Math.PI) - 90;
Browser.InvokeScript("eval", string.Format("accelerometerCallback({0})", accbeta));
});
accelerometer.Start();
}
}
}
4) Open index.html and replace tag <body> with <body onload="onLoad()">
5) Open c2runtime.js and add on top these lines
var wp8_beta = {};
function onLoad() {
window.external.notify("startAccelerometer");
}
function accelerometerCallback(accbeta) {
wp8_beta = accbeta;
}
6) Find on c2runtime.js the line
return this.orient_beta;
and replace with
return wp8_beta;
Now you'll have touch.beta values correctly passed from wp8 to your game.
Since I suck at trigonometry I hope someone will mod the code to obtain also alpha and gamma values.