Added bluetooth sample code to store/phone

This commit is contained in:
mort5161 2015-02-24 12:42:59 -08:00
parent 2fa503c9b8
commit 2cea8ba503
3 changed files with 52 additions and 17 deletions

View file

@ -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 <Capabilities> section:
// <m2:DeviceCapability Name="bluetooth.rfcomm">
// <m2:Device Id="any">
// <m2:Function Type="name:serialPort" />
// </m2:Device>
// </m2:DeviceCapability>
//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, () =>

View file

@ -36,6 +36,11 @@
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="internetClient" />
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="name:serialPort" />
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>

View file

@ -5,7 +5,7 @@
Publisher="CN=mort5161"
Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="872d7e68-e005-4963-b8dd-141db0b2ccc9" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
<mp:PhoneIdentity PhoneProductId="872d7e68-e005-4963-b8dd-141db0b2ccc9" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>SampleApp.Store.WindowsPhone</DisplayName>
@ -19,7 +19,7 @@
</Prerequisites>
<Resources>
<Resource Language="x-generate"/>
<Resource Language="x-generate" />
</Resources>
<Applications>
@ -34,11 +34,17 @@
ForegroundText="light"
BackgroundColor="transparent">
<m3:DefaultTile Wide310x150Logo="Assets\WideLogo.png" Square71x71Logo="Assets\Square71x71Logo.png"/>
<m3:SplashScreen Image="Assets\SplashScreen.png"/>
</m3:VisualElements>
<m3:SplashScreen Image="Assets\SplashScreen.png" />
</m3:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
<DeviceCapability Name="proximity" />
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="name:serialPort" />
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>