wearables presentation from queensland c# mobile developers august 2016

31
WORKING WITH DEVICES Glenn Stephens @glenntstephens

Upload: glenn-stephens

Post on 11-Apr-2017

156 views

Category:

Technology


1 download

TRANSCRIPT

WORKING WITH DEVICES

Glenn Stephens@glenntstephens

WHAT WE’LL COVER The Different Device Types The ways you can interact with the devices Various options and APIs

SOME WAYS TO USE THE DEVICES

Fitness/Health Related

Information

Notifications & Communication

sMotion Sensors

HEALTH RELATED INFORMATION

IOS CONNECTIVITY Options Apple Watch Android Wear Microsoft Band

Access through Apple HealthKit Access through Band SDK

ANDROID CONNECTIVITY Options Android Wear Microsoft Band

Access through Google Fit API Access through Band SDK

WINDOWS PHONE CONNECTIVITY Options Microsoft Band

Access through the Band SDK available through the Xamarin Component Store

ACCESSING HEALTH INFOApple HealthKit

All Recorded Fitness Information Workouts/Actvity/Vitals/more Device Information through HealthKit

Queryable API iOS10 Includes HL7 CDA

Google Fit Sensors API Recording API History API Sessions API (aka Workouts)

HEALTHKIT API You’ll need permission first to use HealthKit

You need explicit permissions for what you want to read/write

All the information needs to then be accessed through the HealthKit APIs

HEALTHKIT REQUESTING PERMISSIONpublic static Task ValidateAuthorization (){    var source = new TaskCompletionSource<bool> ();    var heartRateId = HKQuantityTypeIdentifierKey.HeartRate;    var heartRateType = HKObjectType.GetQuantityType (heartRateId);    var typesToWrite = new NSSet ();    var typesToRead = new NSSet (new NSObject[] {         heartRateType    });

    healthKitStore.RequestAuthorizationToShare (        typesToWrite,         typesToRead,         (success, errors) => {            source.SetResult (true);        });    return source.Task;}

HEALTH KIT COLLECTING INFORMATIONpublic static Task<double> ReadCurrentHeightInMetres(){    var tcs = new TaskCompletionSource<double>();    var heightType = HKQuantityTypeIdentifierKey.Height;    var heightUnit = HKUnit.Meter;    var sort = new NSSortDescriptor(HKSample.SortIdentifierStartDate, false);    var sampleType = HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.Height);    var query = new HKSampleQuery(sampleType, null, 1, new NSSortDescriptor[] { sort },                                           (q, data, error) =>{                                                if (error == null)                                                {                                                    var amount = (data[0] as HKQuantitySample).Quantity.GetDoubleValue(heightUnit);                                                    tcs.TrySetResult(amount);                                                } else {                                                    tcs.TrySetException(new NSErrorException(error));                                                }});    healthKitStore.ExecuteQuery(query);    return tcs.Task;}

public static Task<HKWorkout[]> GetPreviousWorkouts(HKWorkoutActivityType workoutType,  nuint maxWorkoutCount){    var tcs = new TaskCompletionSource<HKWorkout[]> ();

    var predicate = HKQuery.GetPredicateForWorkouts (workoutType);    var sort = new NSSortDescriptor (HKSample.SortIdentifierStartDate, false);

    var sampleQuery = new HKSampleQuery (        HKWorkoutType.GetWorkoutType (),        predicate,         maxWorkoutCount,         new NSSortDescriptor[] { sort },         (q, data, error) => {            if (error != null)                tcs.SetException (new NSErrorException (error));            else {                var result = new List<HKWorkout> ();                foreach (HKWorkout workout in data)                    result.Add (workout);                                tcs.SetResult (result.ToArray ());            }        }    );

    healthKitStore.ExecuteQuery (sampleQuery);

    return tcs.Task;}

GOOGLE FIT API Require the use of the Google API Libraries in order to collect information

Needs the appropriate NuGet packages

Use the Add Google Services option

GOOGLE FIT CONNECTING THE CLIENT Same as other Google API Services Register the access against the service

async Task BuildFitnessClient (){    var clientConnectionCallback = new ClientConnectionCallback ();    clientConnectionCallback.OnConnectedImpl = async () => {        DataSet dataSet = InsertFitnessData ();

        Log.Info (TAG, "Inserting the dataset in the History API");        var insertStatus = (Statuses)FitnessClass.HistoryApi.InsertData (mClient, dataSet) .Await (1, TimeUnit.Minutes);

        if (!insertStatus.IsSuccess) {            Log.Info (TAG, "There was a problem inserting the dataset.");            return;        }

        Log.Info (TAG, "Data insert was successful!");

        DataReadRequest readRequest = QueryFitnessData ();

        var dataReadResult = await FitnessClass.HistoryApi.ReadDataAsync (mClient, readRequest);

        PrintData (dataReadResult);    };

    // Create the Google API Client    mClient = new GoogleApiClient.Builder (this)        .AddApi (FitnessClass.HISTORY_API)        .AddScope (new Scope (Scopes.FitnessActivityReadWrite))        .AddConnectionCallbacks (clientConnectionCallback)        .AddOnConnectionFailedListener (result => {            Log.Info (TAG, "Connection failed. Cause: " + result);            if (!result.HasResolution) {                // Show the localized error dialog                GooglePlayServicesUtil.GetErrorDialog (result.ErrorCode, this, 0).Show ();                return;            }            // The failure has a resolution. Resolve it.            // Called typically when the app is not yet authorized, and an            // authorization dialog is displayed to the user.            if (!authInProgress) {                try {                    Log.Info (TAG, "Attempting to resolve failed connection");                    authInProgress = true;                    result.StartResolutionForResult (this, REQUEST_OAUTH);                } catch (IntentSender.SendIntentException e) {                    Log.Error (TAG, "Exception while starting resolution activity", e);                }            }        }).Build ();}

GOOGLE FIT GETTING WORKOUT INFORMATION

DataReadRequest QueryFitnessData (){    // Setting a start and end date using a range of 1 week before this moment.    DateTime endTime = DateTime.Now;    DateTime startTime = endTime.Subtract (TimeSpan.FromDays (7));    long endTimeElapsed = GetMsSinceEpochAsLong (endTime);    long startTimeElapsed = GetMsSinceEpochAsLong (startTime);

    var readRequest = new DataReadRequest.Builder ()        .Aggregate (DataType.TypeStepCountDelta, DataType.AggregateStepCountDelta)        .BucketByTime (1, TimeUnit.Days)        .SetTimeRange (startTimeElapsed, endTimeElapsed, TimeUnit.Milliseconds)        .Build ();

    return readRequest;}

MICROSOFT BAND Has a great range of capabilities: Create Custom Tiles Send Messages to the Device Read Sensor Information (Accelerometer)

MICROSOFT BAND (ACCELEROMETER)

var accelerometer = client.SensorManager.CreateAccelerometerSensor ();accelerometer.ReadingChanged += (_, e) => {    var data = e.SensorReading;    AccelerometerDataText.Text = string.Format (        "Accel Data: X={0:0.00} Y={1:0.00} Z={2:0.00}",         data.AccelerationX, data.AccelerationY, data.AccelerationZ);}; accelerometer.StartReadings ();

SO MANY SENSORS ON THE BAND

CREATING WEARABLE EXPERIENCES Apple Watch, Google

Wear, Microsoft Band

APPLE WATCH You need to create an iOS App first and the watch app is added as an extension to your project.

Keep in mind there are significant changes coming for the Apple Watch with version 3.0. Much of the documentation will still be valid but there are new capabilities such as Background modes coming very soon.

You still use the designer experience to design your user interfaces. You liase with your device through the extension.

DEMO – CREATE THE APPLE WATCH PROJECT Glance is for quick summaries of information, it’s the quick squiz at the info

Notification – like a notification that you can send through to the device

Adds the WatchKitApp and WatchKitExtension projects

Use the Run As on the WatchKit extension to show the later details

COMMUNICATION BETWEEN THE EXT. & APP

WKInterfaceController.OpenParentApplication (new NSDictionary (), (replyInfo, error) => { if(error != null) { Console.WriteLine (error); return; } Console.WriteLine ("parent app responded"); });

COMMUNICATION BETWEEN THE EXT. & APP

public override void HandleWatchKitExtensionRequest (UIApplication application, NSDictionary userInfo, Action<NSDictionary> reply) { var status = 2; reply (new NSDictionary ( "count", NSNumber.FromInt32 ((int)status), "value2", new NSString("some-info") )); }

USEFUL LINKS – APPLE WATCH https://developer.xamarin.com/videos/cross-platform/your-first-apple-watch-kit-app-with-xamarin/

https://developer.xamarin.com/guides/ios/watch/working-with/ https://developer.xamarin.com/guides/ios/watch/intro-to-watchkit/ https://developer.xamarin.com/guides/ios/watch/getting-started/ https://developer.xamarin.com/guides/ios/watch/platform-features/introduction-to-watchos3/background-tasks/ (watchOS3)

https://developer.xamarin.com/samples/monotouch/WatchKit/GpsWatch/

USEFUL LINKS - HEALTHKIT https://developer.xamarin.com/samples/monotouch/ios8/IntroToHealthKit/

https://developer.xamarin.com/guides/ios/platform_features/introduction_to_healthkit/

https://developer.xamarin.com/samples/monotouch/ios8/Fit/ https://developer.xamarin.com/guides/ios/watch/platform-features/introduction-to-watchos3/additional-framework-changes/

https://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios9/additional-framework-changes/

https://developer.xamarin.com/guides/ios/platform_features/introduction-to-ios10/additional-framework-changes/

USEFUL LINKS – ANDROID WEAR https://developer.xamarin.com/guides/android/wear/intro-to-wear/ https://developer.xamarin.com/guides/android/wear/ https://developer.xamarin.com/guides/android/wear/working-with/ https://developer.xamarin.com/samples/monodroid/wear/WearTest/ https://developer.xamarin.com/guides/android/wear/controls/ https://github.com/xamarin/monodroid-samples/tree/master/wear/JumpingJack

USEFUL LINKS – GOOGLE FIT https://www.google.com/fit/ https://evolve.xamarin.com/session/56ec3f5f790aae283cca2796

USEFUL LINKS – MICROSOFT BAND https://components.xamarin.com/view/microsoft-band-sdk https://components.xamarin.com/gettingstarted/microsoft-band-sdk https://blog.xamarin.com/microsoft-band-sdk-now-available/ https://github.com/mattleibow/Microsoft-Band-SDK-Bindings/tree/master/Demos/Microsoft.Band.Sample

Q&A Glenn Stephens@glenntstephens