Windows Phone によるセンサプログラミング 基応専般 太田 寛 日本マイクロソフト ( 株 ) Windows Phone とセンサ Windows Mobile 5.x Windows Mobile 6.x Windows Phone Windows Mobile OS 2010 Windows Phone 7.0 Windows Phone 7.1 2011 9 Windows Mobile Windows Phone Windows Phone 7.1 Windows Phone Windows Phone Windows Phone SDK 7.1 SDK API Windows Phone PC Windows 7 Android OS Windows Phone OS OS Windows Phone 2011 10 IS12T au 35 21 Windows Phone Windows Phone 1 表 -1 29
A-GPS GPS Accelerometer Compass Gyro 表 -1 搭載センサ一覧 Windows Phone アプリケーション C#/Visual Basic Sensors Camera Windows Phone フレームワーク Device Launchers Bing Deactivate/ t Integration & Choosers MapControl Activate FMRadio Phone Application Framework Phone Application Page Push Notification WebBrowser Control Silverlight Presentation and Media Isolated Controls Drawing Media Storage XNA Framework Input Media Content Markup Shapes Navigation Game Services Graphics Audio アプリケーションオブジェクト.NET Framework 共通基本クラスライブラリ Location Resources Globalization Net Text IO Diagnostics Configuration Security Threading Reflection Collections Component Model Runtime Service Model Linq 太字で示した, Sensors, Location は, 本稿で取り上げるライブラリを示す 図 -1 Windows Phone アプリケーションアーキテクチャ Windows Phone Windows Phone Windows Phone Windows Phone 図 -1 Windows Phone C# Visual Basic Java JVM.NET Framework Windows Phone Windows Phone Silverlight XNA Framework 30
3 Windows Phone によるセンサプログラミング Silverlight XNA Framework Windows Phone 7.1 Silverlight XNA Framework 2 Windows Phone Silverlight XNA Framework 2 Windows Phone OS Windows Phone Windows Phone Windows Phone SDK 7.1 SDK 3 SDK SDK PC Visual Studio 2010 Express for Windows Phone Windows Phone Emulator PC Emulator Expression Blend SDK for Windows Phone Application Deployment Windows Phone Developer Registration Emulator PC Windows Phone OS Windows Phone App Hub Marketplace Windows Phone 31
Accelerometer 3 Gyroscope 3 0 360 Compass 0 360 3 3 3 Motion 3 3 3 3 GeoCoordinateWatcher 表 -2 センサ関連クラスと計測可能なデータ 3 Windows Phone SDK 7.1 表 -2-2 -1 1 1 Accelerometer Gyroscope Compass -1 Accelerometer Gyro Compass Motion Accelerometer Gyro Compass 理 -2 3 図 -2 GeoCoordinateWatcher GPS 情報 Wi-Fi Web GPS 情報 -2 図 -2 センサ計測値の座標 Accelerometer Motion 4 Microsoft. 32
3 Windows Phone によるセンサプログラミング IsSupported State CurrentValue IsDataValid TimeBetweenUpdates CurrentValueChanged Start Stop CurrentValueChanged 表 -3 センサクラスのメンバ Devices.Sensors GeoCoordinateWatcher System.Device.Location 2 Windows Phone 4 センサプログラミングの基本 Accelerometer Gyroscope Compass Motion 表 -3-3 IsSupported Accelerometer 3 IsSupported true State Ready CurrentValue XXX XXXReading IsDataValid CurrentValue 理 false IsDataValid 33
TimeBetweenUpdates Windows Phone Start Stop CurrentValue Start Stop Start Stop Dispose C# Visual Basic OS 処理 Dispose 処理 Java Listener addeventlistener C# Visual Basic CurrentValueChanged CurrentValueChanged SensorReadingEven targs<xxxreading> XXXReading XXX CurrentValue センサチュートリアル C# Silverlight Windows Phone 34
3 Windows Phone によるセンサプログラミング 図 -3 Windows Phone アプリケーション開発ファイル一式 Accelerometer Visual Studio Visual Studio Visual Studio Visual C# Silverlight for Windows Phone Windows Phone OK Windows Phone OS Windows Phone OS 7.1 図 -3-3 Visual Studio MainPage.xaml Silverlight XAML XML XAML 図 -4 Visual Studio Button 2 TextBlock 1 図 -5 XAML XAML -4 Button Name Content Button buttonstart Start Button buttonend End -4 XAML 35
図 -4 MainPage.xaml とエディタ 図 -5 必要な UI コントロールの配置 XAML Start Button MainPage.xaml.cs C# buttonstart_click MainPage.xaml End Button buttonstop_click C# 処理 MainPage.xaml Button Click= Button XAML MainPage.xaml.cs buttonstart_click textblock1.text = Start ; 36
3 Windows Phone によるセンサプログラミング 図 -6 センサプログラミングに必要なコンポーネントの参照設定 buttonstop_click textblock1.text = Stop ; F5 Emulator XAML Start Stop Silverlight UI Microsoft.Devices.Sensors 図 -6.NET Microsoft.Devices.Sensors OK XNA Framework Microsoft.Xna.Framework MainPage.xaml.cs namespace // Part using Microsoft.Devices.Sensors; using Microsoft.Xna.Framework; // Part public partial class MainPage : PhoneApplicationPage private Accelerometer sensor; // Part // public MainPage() InitializeComponent(); // Part sensor = new Accelerometer(); sensor.currentvaluechanged += 37
new EventHandler<SensorReadingEventArgs<AccelerometerReading>>( sensor_currentvaluechanged); // Part void sensor_currentvaluechanged(object sender, SensorReadingEventArgs<AccelerometerReading> e) // Part TextBlock Deployment.Current.Dispatcher.BeginInvoke(() => Vector3 accel = e.sensorreading.acceleration; textblock1.text = String.Format("X=0:0.000,Y=1:0.000,Z=2:0.000", accel.x, accel.y, accel.z); ); // Part private void buttonstart_click(object sender, RoutedEventArgs e) sensor.start(); private void buttonstop_click(object sender, RoutedEventArgs e) sensor.stop(); private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e) sensor.dispose(); MainPage.xaml.cs MainPage Part MainPage Accelerometer Part Visual Studio 理 Visual Studio MainPage() sensor Part Visual Studio XAML TextBlock Part SensorReading Timestamp Acceleration X Y Z Acceleration 38
3 Windows Phone によるセンサプログラミング 図 -7 デバッグターゲットの選択と実行開始 1 Silverlight UI Windows UI UI UI TextBlock 処理 Deployment.Current.Dispatcher.BeginInvoke(() => ); UI 処理 buttonstart_click buttonstop_click MainPage.xaml XAML 1 Unloaded PhoneApplicationPage_ Unloaded Dispose() Dispose 図 -7 Windows Phone Emulator F5 Windows Phone Emulator Emulator Start Phone Emulator >> 図 -8 Accelerometer Accelerometer Gyroscope Compass Motion CurrentValue e.sensorreading Emulator Gyroscope Compass Motion 39
図 -8 Windows Phone Emulator 環境 3 6 Sensor Checker Marketplace GeoCoordinateWatcher Compass Accelerometer Windows Phone BingMap GeoCoordinateWatcher Compass Visual Studio Silverlight for Windows Phone Windows Phone 4 Microsoft.Devices.Sensors Microsoft.Xna.Framework System.Device GeoCoordinateWatcher 40
3 Windows Phone によるセンサプログラミング Microsoft.Phone.Controls.Maps MainPage.xaml xmlns: xmlns:map Xmlns:map= Microsoft.Phone.Controls.Maps xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:map="clrnamespace:microsoft.phone.controls.maps;assembly=microsoft.phone.controls.maps" mc:ignorable="d" d:designwidth="480" d:designheight="768" xmlns:map 2 1 ContentPanel - Grid Canvas map:map Compass RotateTransform <!--ContentPanel - --> <Grid x:name="contentpanel" Grid.Row="1" Margin="12,0,12,0"> <Canvas> <map:map x:name="mymap" ZoomLevel="15" Width="400" Height="400" Canvas.Left="28" Canvas.Top="50" CredentialsProvider="CredentialKey"> <map:map.rendertransform> <RotateTransform x:name="maprotation" CenterX="200" CenterY="200" Angle="0"/> </map:map.rendertransform> </map:map> </Canvas> </Grid> BingMap CredentialKey http://www.bingmapsportal.com/ MainPage.xaml.cs 41
using System.Device.Location; using Microsoft.Devices.Sensors; using Microsoft.Xna.Framework; using Microsoft.Phone.Controls.Maps; public partial class MainPage : PhoneApplicationPage private Compass compass; private GeoCoordinateWatcher gcwatcher; // public MainPage() InitializeComponent(); if (Compass.IsSupported) compass = new Compass(); compass.currentvaluechanged += new EventHandler<SensorReadingEventArgs<CompassReading>>( compass_currentvaluechanged); compass.start(); gcwatcher = new GeoCoordinateWatcher(); gcwatcher.positionchanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>( gcwatcher_positionchanged); gcwatcher.start(); // Part void gcwatcher_positionchanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) Deployment.Current.Dispatcher.BeginInvoke(() => mymap.center = e.position.location; ); // Part // Part void compass_currentvaluechanged( object sender, SensorReadingEventArgs<CompassReading> e) Deployment.Current.Dispatcher.BeginInvoke(() => maprotation.angle = 360 - e.sensorreading.trueheading; ); // Part private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e) if (compass!= null) compass.stop(); compass.dispose(); gcwatcher.stop(); gcwatcher.dispose(); 42
3 Windows Phone によるセンサプログラミング 図 -9 Windows Phone Emulator 環境 Accelerometer mymap maprotation MainPage.xaml map:map RotationTransform x:name XAML C# GeoCoordinateWatcher 情報 Position.Location mymap Center Part Compass SensorReading. TrueHeading maprotation Angle Part Map RotationTransform 2 Silverlight XNA Framework 3D Windows Phone SDK 7.1 Unloaded F5 図 -9 BingMap Emulator Windows Phone SDK 7.1 43
Map http://www.bing.com UI UI Emulator Compass USB PC PC Windows Phone Zune PC PC Visual Studio Windows Phone Device F5 図 -10 Compass Emulator Compass Compass 2 IS02T 25ms if (Compass.IsSupported) compass = new Compass(); compass.currentvaluechanged += new EventHandler<SensorReadingEventArgs<CompassReading>>( compass_currentvaluechanged); compass.timebetweenupdates = TimeSpan.FromSeconds(2); compass.start(); 2 Compass_CurrentValueChanged BeginInvoke 図 -10 実端末での実行 44
3 Windows Phone によるセンサプログラミング Deployment.Current.Dispatcher.BeginInvoke(() => Duration duration = new Duration(TimeSpan.FromSeconds(1)); DoubleAnimation animation = new DoubleAnimation(); animation.duration = duration; animation.to = e.sensorreading.trueheading; Storyboard sb = new Storyboard(); sb.children.add(animation); sb.duration = duration; Storyboard.SetTarget(animation, maprotation); Storyboard.SetTargetProperty( animation, new PropertyPath(RotateTransform.AngleProperty)); sb.begin(); ); DoubleAnimation StoryBoard 2 1 Windows Phone Emulator API 6 5 Windows Phone 情報 参考文献 1 Windows Phone 7.1 Hardware Specification http://msdn.microsoft.com/en-us/library/ff637514(v=vs.92). aspx 2 Fundamental Concepts for Windows Phone http://msdn.microsoft.com/en-us/library/ff967549(v=vs.92). aspx 3 Windows Phone http://msdn.microsoft.com/ja-jp/windowsphone/ 4 Windows Phone Class Library Reference http://msdn.microsoft.com/en-us/library/ff626516(v=vs.92).aspx 5 UX UX-TV http://msdn.microsoft.com/ja-jp/hh162048 6 http://blogs.msdn.com/hirosho/ 2011 10 17 太田寛 hirosho@microsoft.com 45