This is an mobile application to get the data from an SmartBand2 from Sony and then upload it to a website to display the information from everyone.
However, when trying to test the application so far, it crashes and I'm unsure why. It requests the access from the user properly, but then I get an exception unhandled message from Visual Studio but it says nothing. This is the code.
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Gms.Common.Apis;
using Android.Gms.Fitness.Request;
using Android.Gms.Common;
using Android.Gms.Fitness;
using Android.Gms.Fitness.Data;
using Android.Gms.Fitness.Result;
using Android.Util;
using Android.Content;
using System.Threading.Tasks;
using Java.Util.Concurrent;
namespace AndroidReader
{
[Activity(Label = "AndroidReader", MainLauncher = true)]
public class MainActivity : Activity
{
public const string TAG = "StepReader";
const int REQUEST_OAUTH = 1;
const string AUTH_PENDING = "auth_state_pending";
bool authInProgress;
GoogleApiClient mClient;
IOnDataPointListener mListener;
protected override void OnCreate(Bundle bundleInstance)
{
base.OnCreate(bundleInstance);
SetContentView(Resource.Layout.Main);
if (bundleInstance != null)
{
authInProgress = bundleInstance.GetBoolean(AUTH_PENDING);
}
BuildFitnessClient();
}
void BuildFitnessClient()
{
var clientConnectionCallback = new ClientConnectionCallback();
clientConnectionCallback.OnConnectedImpl = async () => await FindFitnessDataSources();
mClient = new GoogleApiClient.Builder(this)
.AddApi(FitnessClass.SENSORS_API)
.AddScope(new Scope(Scopes.FitnessLocationRead))
.AddConnectionCallbacks(clientConnectionCallback)
.AddOnConnectionFailedListener((ConnectionResult result) =>
{
Log.Info(TAG, "Connection failed. Cause: " + result);
if (!result.HasResolution)
{
// Show the localized error dialog
GoogleApiAvailability api = GoogleApiAvailability.Instance;
int resultCode = api.IsGooglePlayServicesAvailable(this);
return;
}
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();
}
class ClientConnectionCallback : Java.Lang.Object, GoogleApiClient.IConnectionCallbacks
{
public Action OnConnectedImpl { get; set; }
public void OnConnected(Bundle connectionHint)
{
Log.Info(TAG, "Connected!!!");
OnConnectedImpl();
}
public void OnConnectionSuspended(int cause)
{
if (cause == GoogleApiClient.ConnectionCallbacks.CauseNetworkLost)
{
Log.Info(TAG, "Connection lost. Cause: Network Lost.");
}
else if (cause == GoogleApiClient.ConnectionCallbacks.CauseServiceDisconnected)
{
Log.Info(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
protected override void OnStart()
{
base.OnStart();
Log.Info(TAG, "Connecting...");
mClient.Connect();
}
protected override void OnStop()
{
base.OnStop();
if (mClient.IsConnected)
{
mClient.Disconnect();
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == REQUEST_OAUTH)
{
authInProgress = false;
if (resultCode == Result.Ok)
{
if (!mClient.IsConnecting && !mClient.IsConnected)
{
mClient.Connect();
}
}
}
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
outState.PutBoolean(AUTH_PENDING, authInProgress);
}
async Task FindFitnessDataSources()
{
var dataSourcesResult = await FitnessClass.SensorsApi.FindDataSourcesAsync(mClient, new DataSourcesRequest.Builder()
.SetDataTypes(Android.Gms.Fitness.Data.DataType.TypeLocationSample)
.SetDataSourceTypes(DataSource.TypeRaw)
.Build());
Log.Info(TAG, "Result: " + dataSourcesResult.Status);
foreach (DataSource dataSource in dataSourcesResult.DataSources)
{
Log.Info(TAG, "Data source found: " + dataSource);
Log.Info(TAG, "Data Source type: " + dataSource.DataType.Name);
//Let's register a listener to receive Activity data!
if (dataSource.DataType == Android.Gms.Fitness.Data.DataType.TypeStepCountDelta && mListener == null)
{
Log.Info(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
await RegisterFitnessDataListener(dataSource, Android.Gms.Fitness.Data.DataType.TypeStepCountDelta);
}
}
}
async Task RegisterFitnessDataListener(DataSource dataSource, Android.Gms.Fitness.Data.DataType dataType)
{
// [START register_data_listener]
mListener = new OnDataPointListener();
var status = await FitnessClass.SensorsApi.AddAsync(mClient, new SensorRequest.Builder()
.SetDataSource(dataSource) // Optional but recommended for custom data sets.
.SetDataType(dataType) // Can't be omitted.
.SetSamplingRate(10, TimeUnit.Seconds)
.Build(),
mListener);
if (status.IsSuccess)
{
Log.Info(TAG, "Listener registered!");
}
else
{
Log.Info(TAG, "Listener not registered.");
}
}
class OnDataPointListener : Java.Lang.Object, IOnDataPointListener
{
public void OnDataPoint(DataPoint dataPoint)
{
foreach (var field in dataPoint.DataType.Fields)
{
Value val = dataPoint.GetValue(field);
Log.Info(TAG, "Detected DataPoint field: " + field.Name);
Log.Info(TAG, "Detected DataPoint value: " + val);
}
}
}
async Task UnregisterFitnessDataListener()
{
if (mListener == null)
{
return;
}
var status = await FitnessClass.SensorsApi.RemoveAsync(mClient, mListener);
if (status.IsSuccess)
{
Log.Info(TAG, "Listener was removed!");
}
else
{
Log.Info(TAG, "Listener was not removed.");
}
}
}
}