与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

时间:2023-01-02 17:41:56

原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

[索引页]
[源码下载]

与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之设备

  • 加速度传感器(加速度计)
  • 数字罗盘(磁力计)

示例
1、演示如何使用加速度传感器
AccelerometerDemo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Device.AccelerometerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <TextBlock Name="lblAccelerometerSupported" />
<Button Name="btnStart" Content="打开加速度传感器" Click="btnStart_Click" />
<Button Name="btnStop" Content="关闭加速度传感器" Click="btnStop_Click" />
<TextBlock Name="lblAccelerometerStatus" />
<TextBlock Name="lblTimeBetweenUpdates" />
<TextBlock Name="lblMsg" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

AccelerometerDemo.xaml.cs

/*
* 演示如何使用加速度传感器
*
* Accelerometer - 用于访问设备中的加速度计
* IsSupported - 设备是否支持加速度传感器
* IsDataValid - 是否可从加速度传感器中获取到有效数据
* CurrentValue - 加速度传感器当前的数据,AccelerometerReading 类型
* TimeBetweenUpdates - 触发 CurrentValueChanged 事件的时间间隔,如果设置的值小于 Accelerometer 允许的最小值,则此属性的值将被设置为 Accelerometer 允许的最小值
* State - 加速度计的当前的状态(Microsoft.Devices.Sensors.SensorState 枚举)
* NotSupported - 设备不支持加速度传感器
* Ready - 加速度传感器已准备好,并且正在解析数据
* Initializing - 加速度传感器正在初始化
* NoData - 加速度传感器无法获取数据
* NoPermissions - 无权限调用加速度传感器
* Disabled - 加速度传感器被禁用
* Start() - 打开加速度计
* Stop() - 关闭加速度计
* CurrentValueChanged - 加速度传感器获取到的数据发生改变时所触发的事件,属性 TimeBetweenUpdates 的值决定触发此事件的时间间隔
*
* AccelerometerReading - 加速度传感器数据
* Acceleration - 详细数据,Vector3 类型的值
* DateTimeOffset - 从加速度传感器中获取到数据的时间点
*
*
*
* 关于从加速度传感器中获取到的 Vector3 类型的值中 X Y Z 的解释如下
* 手机坐标系:以手机位置为参照,假设手机垂直水平面放(竖着放),屏幕对着你,那么
* 1、左右是 X 轴,右侧为正方向,左侧为负方向
* 2、上下是 Y 轴,上侧为正方向,下侧为负方向
* 3、里外是 Z 轴,靠近你为正方向,远离你为负方向
* 以上可以用相对于手机位置的右手坐标系来理解
* X Y Z 的值为中心点到地平面方向的线与各个对应轴线正方向的夹角的余弦值
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework; namespace Demo.Device
{
public partial class AccelerometerDemo : PhoneApplicationPage
{
private Accelerometer _accelerometer; public AccelerometerDemo()
{
InitializeComponent(); // 判断设备是否支持加速度传感器
if (Accelerometer.IsSupported)
{
lblAccelerometerStatus.Text = "此设备支持加速度传感器";
}
else
{
lblAccelerometerStatus.Text = "此设备不支持加速度传感器"; btnStart.IsEnabled = false;
btnStop.IsEnabled = false;
}
} private void btnStart_Click(object sender, RoutedEventArgs e)
{
if (_accelerometer == null)
{
// 实例化 Accelerometer,注册相关事件
_accelerometer = new Accelerometer();
_accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds();
_accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(_accelerometer_CurrentValueChanged); lblTimeBetweenUpdates.Text = "TimeBetweenUpdates 设置为 1 毫秒,实际为 " + _accelerometer.TimeBetweenUpdates.TotalMilliseconds.ToString() + " 毫秒";
} try
{
// 打开加速度传感器
_accelerometer.Start();
lblAccelerometerStatus.Text = "加速度传感器已打开";
}
catch (Exception ex)
{
lblAccelerometerStatus.Text = "加速度传感器已打开失败";
MessageBox.Show(ex.ToString());
}
} private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (_accelerometer != null)
{
// 关闭加速度传感器
_accelerometer.Stop();
lblAccelerometerStatus.Text = "加速度传感器已关闭";
}
} void _accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
// 注:此方法是在后台线程运行的,所以需要更新 UI 的话注意要调用 UI 线程
Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading));
} // 更新 UI
private void UpdateUI(AccelerometerReading accelerometerReading)
{
Vector3 acceleration = accelerometerReading.Acceleration; // 输出 X Y Z 的值
lblMsg.Text = "acceleration.X: " + acceleration.X.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "acceleration.Y: " + acceleration.Y.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "acceleration.Z: " + acceleration.Z.ToString("0.0");
}
}
}

2、演示如何使用数字罗盘传感器
CompassDemo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Device.CompassDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <TextBlock Name="lblCompassSupported" />
<Button Name="btnStart" Content="打开数字罗盘" Click="btnStart_Click" />
<Button Name="btnStop" Content="关闭数字罗盘" Click="btnStop_Click" />
<TextBlock Name="lblCompassStatus" />
<TextBlock Name="lblTimeBetweenUpdates" />
<TextBlock Name="lblOrientation" />
<TextBlock Name="lblMsg" /> </StackPanel> <Grid x:Name="gridCalibration" Visibility="Collapsed" Background="Black" Opacity="1">
<StackPanel>
<Image Source="CompassCalibrate.png" HorizontalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" TextAlignment="Center">
设备需要校准。校准方法:如图所示,对手机做“8字”旋转,直至完成校准(即 HeadingAccuracy 小于等于 10)
</TextBlock>
<Button Name="btnKnown" Content="知道了" Click="btnKnown_Click" />
</StackPanel>
</Grid>
</Grid> </phone:PhoneApplicationPage>

CompassDemo.xaml.cs

/*
* 演示如何使用数字罗盘传感器
*
* Compass - 用于访问设备中的磁力计
* IsSupported - 设备是否支持数字罗盘
* IsDataValid - 是否可从数字罗盘中获取到有效数据
* CurrentValue - 数字罗盘当前的数据,CompassReading 类型
* TimeBetweenUpdates - 触发 CurrentValueChanged 事件的时间间隔,如果设置的值小于 Compass 允许的最小值,则此属性的值将被设置为 Compass 允许的最小值
* Start() - 打开磁力计
* Stop() - 关闭磁力计
* CurrentValueChanged - 数字罗盘传感器获取到的数据发生改变时所触发的事件,属性 TimeBetweenUpdates 的值决定触发此事件的时间间隔
* Calibrate - 当系统检测到数字罗盘需要校准时所触发的事件
*
* CompassReading - 数字罗盘传感器数据
* HeadingAccuracy - 数字罗盘的精度,其绝对值如果大于 10 则需要校准
* TrueHeading - 与地理北极的顺时针方向的偏角(单位:角度),经测试可用
* MagneticHeading - 与地磁北极的顺时针方向的偏角(单位:角度),经测试理解不了
* MagnetometerReading - 磁力计的原始数据(单位:微特斯拉),经测试理解不了
* DateTimeOffset - 从数字罗盘传感器中获取到数据的时间点
*
*
*
* 手机坐标系:以手机位置为参照,假设手机垂直水平面放(竖着放),屏幕对着你,那么
* 1、左右是 X 轴,右侧为正方向,左侧为负方向
* 2、上下是 Y 轴,上侧为正方向,下侧为负方向
* 3、里外是 Z 轴,靠近你为正方向,远离你为负方向
* 以上可以用相对于手机位置的右手坐标系来理解
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System.Windows.Threading; namespace Demo.Device
{
public partial class CompassDemo : PhoneApplicationPage
{
private Compass _compass;
private Accelerometer _accelerometer; public CompassDemo()
{
InitializeComponent(); // 判断设备是否支持数字罗盘
if (Compass.IsSupported)
{
lblCompassSupported.Text = "此设备支持数字罗盘";
}
else
{
lblCompassSupported.Text = "此设备不支持数字罗盘"; btnStart.IsEnabled = false;
btnStop.IsEnabled = false;
}
} private void btnStart_Click(object sender, RoutedEventArgs e)
{
if (_compass == null)
{
// 实例化 Compass,注册相关事件
_compass = new Compass();
_compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds();
_compass.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<CompassReading>>(_compass_CurrentValueChanged);
_compass.Calibrate += new EventHandler<CalibrationEventArgs>(_compass_Calibrate); lblTimeBetweenUpdates.Text = "TimeBetweenUpdates 设置为 1 毫秒,实际为 " + _compass.TimeBetweenUpdates.TotalMilliseconds.ToString() + " 毫秒"; // 实例化 Accelerometer,注册相关事件,用于判断手机是横放状态还是竖放状态
_accelerometer = new Accelerometer();
_accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(_accelerometer_CurrentValueChanged);
} try
{
// 打开数字罗盘
_compass.Start();
lblCompassStatus.Text = "数字罗盘已打开"; _accelerometer.Start();
}
catch (Exception ex)
{
lblCompassStatus.Text = "数字罗盘打开失败";
MessageBox.Show(ex.ToString());
}
} private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (_compass != null && _compass.IsDataValid)
{
// 关闭数字罗盘
_compass.Stop();
_accelerometer.Stop(); lblCompassStatus.Text = "数字罗盘已关闭";
}
} void _compass_CurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
{
// 注:此方法是在后台线程运行的,所以需要更新 UI 的话注意要调用 UI 线程
Dispatcher.BeginInvoke(() => UpdateUI(e.SensorReading));
} // 更新 UI
private void UpdateUI(CompassReading compassReading)
{
// 显示从数字罗盘中获取到的各个参数
lblMsg.Text = "magneticHeading: " + compassReading.MagneticHeading.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "trueHeading: " + compassReading.TrueHeading.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "headingAccuracy: " + Math.Abs(compassReading.HeadingAccuracy).ToString("0.0");
lblMsg.Text += Environment.NewLine; Vector3 magnetometerReading = compassReading.MagnetometerReading; lblMsg.Text += "magnetometerReading.X: " + magnetometerReading.X.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "magnetometerReading.Y: " + magnetometerReading.Y.ToString("0.0");
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "magnetometerReading.Z: " + magnetometerReading.Z.ToString("0.0");
} void _compass_Calibrate(object sender, CalibrationEventArgs e)
{
// 注:此方法是在后台线程运行的,所以需要更新 UI 的话注意要调用 UI 线程 // 显示“提示页”,用于提示用户设备需要校准
Dispatcher.BeginInvoke(() => { gridCalibration.Visibility = Visibility.Visible; });
} private void btnKnown_Click(object sender, RoutedEventArgs e)
{
// 隐藏“提示页”
gridCalibration.Visibility = System.Windows.Visibility.Collapsed;
} // 判断手机是竖放还是横放
void _accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
Vector3 v = e.SensorReading.Acceleration; bool isCompassUsingNegativeZAxis = false; if (Math.Abs(v.Z) < Math.Cos(Math.PI / ) && (v.Y < Math.Sin( * Math.PI / )))
{
isCompassUsingNegativeZAxis = true;
} Dispatcher.BeginInvoke(() => { lblOrientation.Text = (isCompassUsingNegativeZAxis) ? "手机竖放" : "手机横放"; });
}
}
}

OK
[源码下载]

与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器的更多相关文章

  1. 与众不同 windows phone &lpar;19&rpar; - Device(设备)之陀螺仪传感器&comma; Motion API

    原文:与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API [索引页][源码下载] 与众不同 windows phone (19) - Dev ...

  2. 与众不同 windows phone &lpar;23&rpar; - Device(设备)之硬件状态&comma; 系统状态&comma; 网络状态

    原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态 [索引页][源码下载] 与众不同 windows phone (23) - Devic ...

  3. 与众不同 windows phone &lpar;22&rpar; - Device(设备)之摄像头(硬件快门&comma; 自动对焦&comma; 实时修改捕获视频)

    原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...

  4. 与众不同 windows phone &lpar;21&rpar; - Device(设备)之摄像头(拍摄照片&comma; 录制视频)

    原文:与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) [索引页][源码下载] 与众不同 windows phone (21) - Device ...

  5. 与众不同 windows phone &lpar;20&rpar; - Device(设备)之位置服务(GPS 定位)&comma; FM 收音机&comma; 麦克风&comma; 震动器

    原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...

  6. 张高兴的 Windows 10 IoT 开发笔记:三轴数字罗盘 HMC5883L

    注意,数据不包含校验,准确的来说我不知道怎么校验,但方向看起来差不多是对的... GitHub:https://github.com/ZhangGaoxing/windows-iot-demo/tre ...

  7. 与众不同 windows phone &lpar;2&rpar; - Control(控件)

    原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...

  8. 如何解决Windows 10系统下设备的声音问题

    如何解决Windows 10系统下设备的声音问题? 请阅读下面的说明来解决Windows 10设备上的声音问题. 1. 检查设备管理器 打开开始菜单,键入设备管理器, 从出现的结果中选择并打开它. 在 ...

  9. 与众不同 windows phone 8&period;0 &amp&semi; 8&period;1 系列文章索引

    [源码下载] [与众不同 windows phone 7.5 (sdk 7.1) 系列文章索引] 与众不同 windows phone 8.0 & 8.1 系列文章索引 作者:webabcd ...

随机推荐

  1. c&num;3&period;0新特性

    1.自动属性 public int ID { get; set; } // 上面的ID属性(自动属性)等同于下面的ID属性 // private int _id; // public int ID / ...

  2. docker存储结构解析

    由于aufs并未并入内核,故而目前只有Ubuntu系统上能够使用aufs作为docker的存储引擎,而其他系统上使用lvm thin provisioning(overlayfs是一个和aufs类似的 ...

  3. html5 自定义验证信息

      h5 为表单新增了很多类型,及属性. 根据这些新增的类型及属性 h5也为我们提供了验证这些数据的js函数,这些验证表单的函数都存在了ValidityState对象中,接下来让我们一起来了解一下这些 ...

  4. Entity Framework ModelFirst尝试

    前言 Model First我们称之为“模型优先”,这里的模型指的是“ADO.NET Entity Framework Data Model”,此时你的应用并没有设计相关数据库,在Visual Stu ...

  5. javascript 正则匹配手机号码

      <form class="form-horizontal" name="mobileform" style="padding:10px;&q ...

  6. Unity User Group 北京站图文报道:《Unity虚拟现实实战技巧》

    时间来到了盛夏,北京UUG活动也来到了第八期.本次活动的主题为<Unity虚拟现实实战技巧>,为此我们邀请了4位资深的行业大神.这次我们仍然在北京市海淀区丹棱街5号微软大厦举行活动,在这里 ...

  7. springMVC对异常处理的支持

    无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了.跟其他MVC框架一样, ...

  8. 解决 Intellij IDEA Cannot Resolve Symbol &OpenCurlyQuote;XXX’ 问题

    1.java类报错 https://blog.csdn.net/qq_32040767/article/details/77096680 2.类对应的依赖没有加载进来.编译器自身的设置和缓存问题类. ...

  9. elasticsearch视频34季

    02_结构化搜索_在案例中实战使用term filter来搜索数据 课程大纲 1.根据用户ID.是否隐藏.帖子ID.发帖日期来搜索帖子 (1)插入一些测试帖子数据 POST /forum/articl ...

  10. div凹角实现

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...