Upload
windowsphonerocks
View
271
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Building Apps for Windows Phone 8.1 Jump Start . Videos at: http://channel9.msdn.com/Series/Building-Apps-for-Windows-Phone-8-1
Citation preview
Andy Wigley @andy_wigleyMatthias Shapiro @matthiasshap
Sensors and Proximity: NFC and Bluetooth
Windows XAML+ Silverlight 8.1
30 April 2014
Building Apps for Windows Phone 8.1 Jump Start
In this module…
Introduction to using accelerometer, compass etcEnhancements to ProximityQuery the status of Wi-Fi, NFC, and Bluetooth radiosNFC Tag read and writeExpanded NFC tag scenarios so you can make your tags read-only, write to unformatted tags, and to communicate with other contactless cardsNFC ‘Tap to Connect’ [reprise WP8]Direct APDU access to smart cards, examples include accessing transit passes, credit cards, loyalty cards, hardware accessories, and other types of device
Bluetooth commsBluetooth connect triggering background process
Programming Sensors using Windows Runtime
Choosing the right sensor
Accelerometer
Inclinometer
Device Orientation
Light SensorGyrometer
Compass
Sensors across Windows platforms
Raw Sensor Type Windows 8.1 Windows Phone 8.1
3D-Accelerometer Yes Yes
3D-Magnetometer Yes Yes
3D-Gyroscope Yes Yes
Ambient Light Yes Yes
Short Range Proximity No Yes
Simple Device
Orientation
Device Orientation Inclinometer Compass Shake
Derived Sensors (Fusion)
Determining Sensor Availability
All the sensor classes have a GetDefault() method This method only returns a non-null value if the hardware is available on the device
// Determine whether we have a gyro on the phone _gyrometer = Gyrometer.GetDefault();
if (_gyrometer != null) { // Establish the report interval (units are milliseconds) _gyrometer.ReportInterval = 100; _gyrometer.ReadingChanged += _gyrometer_ReadingChanged; } else { MessageBox.Show("No gyrometer found"); }
Activating and Deactivating a Sensor
// Establish the report interval (units are milliseconds) uint reportInterval = 100; if (_gyrometer.MinimumReportInterval > reportInterval) { reportInterval = _gyrometer.MinimumReportInterval; }
_gyrometer.ReportInterval = reportInterval;
Application must set the report interval to a non-zero value prior to registering an event handler or calling GetCurrentReading() to activate itWhen finished with the sensor, set it to zero
Using the Sensor ReadingChanged event
Register the ReadingChanged event handler to obtain sensor readingsMust set the ReportInterval property first
_gyrometer.ReportInterval = 100;_gyrometer.ReadingChanged += _gyrometer_ReadingChanged;…
private void _gyrometer_ReadingChanged(Gyrometer sender, GyrometerReadingChangedEventArgs args){ Dispatcher.BeginInvoke(() => { GyrometerReading reading = args.Reading; X_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX); Y_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY); Z_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ); });}
Getting Readings By Polling a Sensor
An application can poll the sensor for the current reading as an alternative to registering a ReadingChanged event handlerThe preferred alternative for an application that updates its user interface at a specific frame rate
Must still establish a desired ReportInterval before polling in order to activate the sensor
// Alternative to ReadingChanged event, call GetCurrentReading() to poll the sensorGyrometerReading reading = _gyrometer.GetCurrentReading();
if (reading != null){ X_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX); Y_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY); Z_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ);}
Sensor access in the backgroundWindows.Devices.Sensors and Microsoft.Devices.Sensors API can continue to return data in the background on Windows Phone 8.1
Windows Runtime apps can use DeviceUseTrigger in background TasksApp can only run one DeviceUse Trigger background task at a time
Silverlight 8.1 apps can access triggers while active by ‘Run under lock’ or through Continuous Background Execution (background location tracking apps)
Bluetooth
Bluetooth ScenariosThere are four Bluetooth communication scenarios supported by Windows Phone 8.1 devicesApp to deviceA program running on a Windows Phone can establish a connection to an external device The Windows Phone must be paired with the device
App to appAn application can communicate with another instance of itself running on a different deviceIn this situation pairing is not required
Bluetooth RFCOMM (*new in Windows Phone 8.1)A program can interact with a device using RFCOMM protocols, often called serial port emulation
Bluetooth GATT to access Bluetooth LE devices (*new in Windows Phone 8.1)A program can use the Bluetooth GATT APIs to access Bluetooth LE services, descriptors, and characteristics
App to DeviceAn application running on a Windows device can obtain an enumeration of all the Bluetooth devices that have been paired with the phone
The application can then attempt to make a connection to the required service on that device
The following capabilities must be enabled for the application to make use of the Bluetooth communications to a device• ID_CAP_PROXIMITY and
ID_CAP_NETWORKING (Silverlight app)
• PROXIMITY (Windows Runtime app)
App to App communicationApp to App communication allows an app to interact with the same app running on another device using Bluetooth to exchange messagesAn application can wait for and respond to messages from another applicationThe PeerFinder class exposes an event which is raised when a communication request is received from another systemThe communication is performed using a StreamSocket that connects the two programs
The devices do not need to be paired in order to implement app to app connectionThe PROXIMITY capability must be enabled
For more information…
These two scenarios: App-to-App and App-to-device are supported on Windows Phone 8
To find out more on these scenarios, see:Building Apps for Windows Phone 8 Jump Start: (12) Proximity Sensors and Bluetooth http://aka.ms/Gc7rwo
What’s new in Windows Phone 8.1
Bluetooth RFCOMMBluetooth LE (also called Bluetooth Smart)Bluetooth triggers enabling background tasksRfcommConnectionTriggerDeviceChangeTriggerBluetoothSignalStrengthTriggerGattCharacteristicNotificationTrigger
18
Bluetooth RFCOMM
Connect the unconnected
RFCommLTE
• Windows.Devices.Bluetooth.Rfcomm API to connect, host, and communicate with Bluetooth RFCOMM device services
• Devices using Serial Port Profile (SPP) are supported
• Client and server Service Discovery Protocol (SDP)
Declaring app capabilities
<!-- We'll use the Serial Port Profile service on any device. --><m2:DeviceCapability Name="bluetooth.rfcomm"> <m2:Device Id="any"> <!-- or ="vidpid:xxxx xxxx bluetooth" --> <m2:Function Type="name:serialPort" /> <!-- use name:<service> as above, or… --> <m2:Function Type="serviceId:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"/> </m2:Device></m2:DeviceCapability>You cannot modify the Bluetooth device capability using the Manifest editor in Microsoft Visual Studio. You must right-click the Package.appxmanifest file in Solution Explorer and select Open With..., and then XML (Text) Editor.
Use RfcommDeviceService’s helpers to retrieve an RFCOMM-specific selector
• RfcommDeviceService.GetDeviceSelector
Use an enumerated DeviceInformation object to instantiate an RfcommDeviceService
• RfcommDeviceService.FromIdAsync
Discover RfcommDeviceServices with the Windows.Devices.Enumeration API
Client: Service discovery and instantiation
Use StreamSocket.ConnectAsync to connect to the remote device.
• Use the ConnectionHostName and ConnectionServiceName properties on the RfcommDeviceService to connect.
I/O over the connected StreamSocket is available via its InputStream and OutputStream properties.
• You can use the DataReader and DataWriter classes to read primitive types (such as UInt32s or strings).
Use a StreamSocket to communicate with the remote device
Client: Communicating with the Device
! You must dispose of your Rfcomm objects when your application is suspended
23
Bluetooth LE
• GATT Client support
• Windows Runtime API for GATT
• Windows.Devices.Bluetooth.GenericAttributeProfile
• Flexibility to build your own GATT-based profiles
Familiarity&Extensibility
Bluetooth Smart (BLE) - GATT
Declaring app capabilities
<m2:DeviceCapability Name="bluetooth.genericAttributeProfile">
<m2:Device Id="model:xxxx;xxxx"|"any">
<m2:Function Type="name:heartRate"/>
<!-- use name:<service> as above, or… -->
<m2:Function Type="serviceId:xxxxxxxx"/>
</m2:Device>
</m2:DeviceCapability>
You cannot modify the Bluetooth GATT device capability using the Manifest editor in Microsoft Visual Studio. You must right-click the Package.appxmanifest file in Solution Explorer and select Open With..., and then XML (Text) Editor.
Example: Bluetooth LE Health Thermometer Service async void Initialize() { var themometerServices = await Windows.Devices.Enumeration .DeviceInformation.FindAllAsync( GattDeviceService.GetDeviceSelectorFromUuid( GattServiceUuids.HealthThermometer), null);
GattDeviceService firstThermometerService = await GattDeviceService.FromIdAsync(themometerServices[0].Id);
Debug.WriteLine("Using service: " + themometerServices[0].Name);
GattCharacteristic thermometerCharacteristic = firstThermometerService.GetCharacteristics(GattCharacteristicUuids.TemperatureMeasurement)[0];
thermometerCharacteristic.ValueChanged += temperatureMeasurementChanged;
await thermometerCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Notify); }
Example: Bluetooth LE Health Thermometer Service void temperatureMeasurementChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs) { byte[] temperatureData = new byte[eventArgs.CharacteristicValue.Length]; Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(temperatureData);
var temperatureValue = convertTemperatureData(temperatureData); temperatureTextBlock.Text = temperatureValue.ToString(); }
double convertTemperatureData(byte[] temperatureData) { // Read temperature data in IEEE 11703 floating point format // temperatureData[0] contains flags about optional data - not used UInt32 mantissa = ((UInt32)temperatureData[3] << 16) | ((UInt32)temperatureData[2] << 8) | ((UInt32)temperatureData[1]);
Int32 exponent = (Int32)temperatureData[4]; return mantissa * Math.Pow(10.0, exponent); }
28
Bluetooth Triggers
Bluetooth triggersDeviceUseTriggerForeground app launches a background task immediately, and have the background task continue even when the app is no longer in the foreground. Background task can run as long as the underlying connection exists.A sports heartrate monitor application is a good example.
RfcommConnectionTriggerBackground task to run whenever we establish an RFCOMM connection with a particular service on a remote deviceThis will run when the connection occurs, even if the foreground app has not been launched
DeviceConnectionChangeTriggerBackground task to run whenever a specific Bluetooth device becomes connected and/or disconnectedExample is a task that raises an alert when the phone loses connectivity with an LE key fob
GattCharacteristicNotificationTriggerBackground task to run whenever a specific Bluetooth device informs the phone that the value of one its characteristics has changedAn example includes a device that you can put into a flower pot and will notify you when the plant needs water
30
Bluetooth RfcommConnectionTrigger example // The service Uuid: 662DD79F-3CD4-4D16-8279-DB1D45ED9628 private static readonly Guid BluetoothServiceUuid = Guid.Parse("662DD79F-3CD4-4D16-8279-DB1D45ED9628"); ...
// Register a background Task var trigger = new RfcommConnectionTrigger(); trigger.InboundConnection.LocalServiceId = RfcommServiceId.FromUuid(BluetoothServiceUuid); trigger.OutboundConnection.RemoteServiceId = RfcommServiceId.FromUuid(BluetoothServiceUuid); trigger.AllowMultipleConnections = false; var builder = new BackgroundTaskBuilder(); builder.SetTrigger(trigger);
demo
Bluetooth triggers
Near Field Communications (NFC)
Near Field Communications
NFC provides a connection between devices that are very close together (within 3-4 centimetres)The data is transferred at a rate of 106, 212 or 424 Kbit/secondNFC operates at slower speeds than Bluetooth (BT v2.1 is up to 2.1Mbit/s) but consumes less power and doesn’t require pairing
It is assumed that this data transfer is intentional so there is not normally any authentication as suchThe user has positioned their device close to the other device
The phone can connect to an unpowered NFC chip/tagTags currently support between 96 and 4096 bytes of memory
PictureContact cardsURLsVideos
NFC ScenariosShort message
exchange
Read
Read/Write
Read and Write NFC Tags or smartcards
Bootstrap higher capacity connections
Secure NFC Payment
Tap to share
Smartcard emulation
Transit passesEnterprise access control
Mobile Payments (Credit/debit cards etc)
Supported since Windows Phone 8…
These two scenarios: Short message exchange and Bootstrap Bluetooth or WiFi connections were supported since Windows Phone 8.Not covered again in this Jump Start
To find out more, see:Building Apps for Windows Phone 8 Jump Start: (12) Proximity Sensors and Bluetooth http://aka.ms/Gc7rwo
What’s New in 8.1 for Windows Phone NFCNo more pop-up messages for tap to read if the foreground application handles the message readTrusted applications conceptUsers can determine which apps are trusted to eliminate the confirmation pop-up for launching the app through tappingUsers can manage the list of trusted apps according to their preference
UICC-based card emulation overhaulCommunicate (at a lower level) with Smartcards via NFC radio
Lots of new card/tag featuresNDEF formatting and read-only support for tag operations
New NFC driver modelSupported on new phone hardware
Trusted Apps: Recap - Use NFC to launch an app Windows.Networking.Proximity.ProximityDevice proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
if (proximityDevice != null) { // The format of the app launch string is: "<args>\tWindows\t<AppName>". // The string is tab or null delimited. The <args> string can be an empty string (""). string launchArgs = "user=default"; // The format of the AppName is: PackageFamilyName!PRAID. string praid = "MyAppId"; // The Application Id value from your package.appxmanifest. string appName = Windows.ApplicationModel.Package.Current.Id.FamilyName + "!" + praid; string launchAppMessage = launchArgs + "\tWindows\t" + appName;
var dataWriter = new Windows.Storage.Streams.DataWriter(); dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE; dataWriter.WriteString(launchAppMessage); var launchAppPubId = proximityDevice.PublishBinaryMessage( "LaunchApp:WriteTag", dataWriter.DetachBuffer()); }
User Experience Improvements: Apps I trustThe user of the target device has to confirm the app launchConfirmation required every time on Windows Phone 8On Windows Phone 8.1, confirmation still required the first time, but on subsequent launches, user can check ‘I trust this App’App is listed in the Apps I trust section in NFC Settings
NDEF formatting is automaticJust publish any tag writing operation with Windows.Networking.Proximity.ProximityDevice, tag will get formatted automatically if needed
Tags supported:MiFare Ultralight, MiFare Classic, MiFare Desfire
Set tag as read-onlyJust publish a message of type “SetTagReadOnly”Will process any active tag writing operations first, then lock the tag
Tags supported:MiFare UL, MiFare Desfire, Topaz
*Both these features require new Phone hardware with the PN547 chip and associated driver
NDEF Formatting and Read-Only Support
39
SmartCard WinRT API for Smart Card AccessSuitable for applications that require raw access to smartcards/tags:Accessing transit passes or cardsReading balance off a credit card or adding funds to a debit cardManaging Loyalty cards Enterprise Access Control
Supported Smart Card Types:ISO 7816-4/APDUMiFare UltralightMiFare ClassicMiFare DesfireFelica
*Low-level raw access requires new Phone hardware with the PN547 chip and associated driver
Provide 3rd party apps access to UICC SE• MNO wallet apps (formerly known as
the plug-in) • NFC service applications (e.g.
banking apps, transit apps, access) Provide 3rd party apps read-only access to Card Emulation settingsProvide 3rd party apps the event type and payload for the following NFC Events:
• NFC Entry• NFC Exit• Transaction
SmartCard WinRT APIs
41
Bank/MNO App
UICC
Card
NFC Controller
SE Manager with ACL Enforcer
3rd party Smartcard API
OS Platform
Using the SmartCard API (1 of 2)
public async void SetupCardReader() { var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync( SmartCardReader.GetDeviceSelector(SmartCardReaderKind.Nfc)); var reader = await SmartCardReader.FromIdAsync(devices.First().Id); reader.CardAdded += CardAdded; reader.CardRemoved += CardRemoved; }
public async void CardAdded(SmartCardReader sender, CardAddedEventArgs args) { DoCardOperations(args.SmartCard); }
void CardRemoved(SmartCardReader sender, CardRemovedEventArgs args) { // Handle card removal }
Using the SmartCard API (2 of 2)
async void DoCardOperations(SmartCard card) { using (var connection = await card.ConnectAsync()) { var atr = await card.GetAnswerToResetAsync();
string adpuCommand = "Some ADPU command"; var dataWriter = new Windows.Storage.Streams.DataWriter(); dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE; dataWriter.WriteString(adpuCommand);
var apduResponse = await connection.TransmitAsync(dataWriter.DetachBuffer()); } }
44
Card Access
Demo
Easier than in WP8.0: No Firmware Customization Required• Any updated Windows Phone should work with any MO (mobile
operator)• No more OEM/MO plugin, no dependency on OEM or MS
• Generic open-market handsets are no problem
Global Platform SE Access Control Standard v1.0• Built-in to the OS• Can use the same app certificates and UICC rules as on other
platforms
Easy App Development• Develop the app as you would any other, no special hardware or tools
required• Write the app once, will work across all Windows Phone models
Card Emulation Features
45
©2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.