From 2cea8ba503663d534da9b365af2cb186b3270bf9 Mon Sep 17 00:00:00 2001 From: mort5161 Date: Tue, 24 Feb 2015 12:42:59 -0800 Subject: [PATCH] Added bluetooth sample code to store/phone --- .../SampleApp.Store.Shared/MainPage.xaml.cs | 48 ++++++++++++++----- .../Package.appxmanifest | 7 ++- .../Package.appxmanifest | 14 ++++-- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/SampleApp.Store/SampleApp.Store.Shared/MainPage.xaml.cs b/src/SampleApp.Store/SampleApp.Store.Shared/MainPage.xaml.cs index d7f2d08..a3b37bc 100644 --- a/src/SampleApp.Store/SampleApp.Store.Shared/MainPage.xaml.cs +++ b/src/SampleApp.Store/SampleApp.Store.Shared/MainPage.xaml.cs @@ -1,17 +1,9 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; +using Windows.Devices.Bluetooth.Rfcomm; +using Windows.Devices.Enumeration; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 @@ -27,15 +19,47 @@ namespace SampleApp.Store public MainPage() { this.InitializeComponent(); - LoadData(); + //Load a simulated GPS device that plays back an NMEA log file + LoadLocalGpsSimulationData(); + //Use this to connect to a bluetooth device instead: + //LoadBluetoothGPS(); } - public async void LoadData() + + private async void LoadBluetoothGPS() + { + //Ensure the bluetooth capability is enabled by opening package.appxmanifest in a text editor, + // and add the following to the section: + // + // + // + // + // + + //Get list of devices + string serialDeviceType = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort); + var devices = await DeviceInformation.FindAllAsync(serialDeviceType); + + string GpsDeviceName = "HOLUX GPSlim236"; //Change name to your device or build UI for user to select from list of 'devices' + //Select device by name + var bluetoothDevice = devices.Where(t => t.Name == GpsDeviceName).FirstOrDefault(); + //Get service + RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(bluetoothDevice.Id); + if (rfcommService != null) + { + var device = new NmeaParser.BluetoothDevice(rfcommService); + device.MessageReceived += device_MessageReceived; + await device.OpenAsync(); + } + } + + public async void LoadLocalGpsSimulationData() { var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///NmeaSampleData.txt")); var device = new NmeaParser.NmeaFileDevice(file); device.MessageReceived += device_MessageReceived; var _ = device.OpenAsync(); } + private void device_MessageReceived(object sender, NmeaParser.NmeaMessageReceivedEventArgs args) { var _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => diff --git a/src/SampleApp.Store/SampleApp.Store.Windows/Package.appxmanifest b/src/SampleApp.Store/SampleApp.Store.Windows/Package.appxmanifest index b16ebb9..58ef60e 100644 --- a/src/SampleApp.Store/SampleApp.Store.Windows/Package.appxmanifest +++ b/src/SampleApp.Store/SampleApp.Store.Windows/Package.appxmanifest @@ -36,6 +36,11 @@ - + + + + + + \ No newline at end of file diff --git a/src/SampleApp.Store/SampleApp.Store.WindowsPhone/Package.appxmanifest b/src/SampleApp.Store/SampleApp.Store.WindowsPhone/Package.appxmanifest index 5a1572d..8260d0a 100644 --- a/src/SampleApp.Store/SampleApp.Store.WindowsPhone/Package.appxmanifest +++ b/src/SampleApp.Store/SampleApp.Store.WindowsPhone/Package.appxmanifest @@ -5,7 +5,7 @@ Publisher="CN=mort5161" Version="1.0.0.0" /> - + SampleApp.Store.WindowsPhone @@ -19,7 +19,7 @@ - + @@ -34,11 +34,17 @@ ForegroundText="light" BackgroundColor="transparent"> - - + + + + + + + + \ No newline at end of file