我一直在玩Xamarin和Google Fit一段时间,我试图制作一个简单的计步器应用程序。我不能让听众工作。据我了解,每次采取步骤时都会调用OnDataPointListener
的{{1}}方法。用我的代码我不会发现任何事情。我做错了什么?
以下是我使用的代码(缩写为仅包含相关部分)
MainActivity.cs
OnDataPoint()
BuildFitnessClient
GoogleApiClient mClient;
OnDataPointListener _mListener;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
if (bundle != null)
{
authInProgress = bundle.GetBoolean(AUTH_PENDING);
}
_viewPager = FindViewById<ViewPager>(Resource.Id.container);
_viewPager.Adapter = new FragmentAdapter(SupportFragmentManager);
BuildFitnessClient();
}
ClientConnectionCallback.cs
void BuildFitnessClient()
{
var clientConnectionCallback = new ClientConnectionCallback();
clientConnectionCallback.OnConnectedImpl = async () => await FindFitnessDataSources();
mClient = new GoogleApiClient.Builder(this)
.AddApi(FitnessClass.SENSORS_API)
.AddScope(FitnessClass.ScopeActivityReadWrite)
.AddScope(FitnessClass.ScopeBodyReadWrite)
.AddScope(FitnessClass.ScopeLocationRead)
.AddConnectionCallbacks(clientConnectionCallback)
.AddOnConnectionFailedListener((ConnectionResult result) =>
{
if (!result.HasResolution)
{
// Show the localized error dialog
#pragma warning disable CS0618 // Type or member is obsolete
GooglePlayServicesUtil.GetErrorDialog(result.ErrorCode, this, 0).Show();
#pragma warning restore CS0618 // Type or member is obsolete
return;
}
if (!authInProgress)
{
try
{
authInProgress = true;
result.StartResolutionForResult(this, REQUEST_OAUTH);
}
catch (IntentSender.SendIntentException e)
{
}
}
}).Build();
}
private async Task FindFitnessDataSources()
{
var dataSourcesResult = await FitnessClass.SensorsApi.FindDataSourcesAsync(mClient, new DataSourcesRequest.Builder()
.SetDataSourceTypes(DataSource.TypeDerived)
.SetDataTypes(DataType.TypeStepCountDelta)
.Build());
foreach (DataSource dataSource in dataSourcesResult.DataSources)
{
if (_mListener == null)
{
await RegisterFitnessDataListener(dataSource, dataSource.DataType);
}
}
}
private async Task RegisterFitnessDataListener(DataSource dataSource, DataType dataType)
{
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(1, TimeUnit.Seconds)
.Build(), _mListener);
if (status.IsSuccess)
{
Toast.MakeText(this, "Listener connected", ToastLength.Short).Show();
}
else
{
}
}
IOnDataPointListener
internal class ClientConnectionCallback : Java.Lang.Object, GoogleApiClient.IConnectionCallbacks
{
public Action OnConnectedImpl { get; set; }
public void OnConnected(Android.OS.Bundle connectionHint)
{
OnConnectedImpl();
}
public void OnConnectionSuspended(int cause)
{
if (cause == GoogleApiClient.ConnectionCallbacks.CauseNetworkLost)
{
Log.Info("Health365", "Connection lost. Cause: Network Lost.");
}
else if (cause == GoogleApiClient.ConnectionCallbacks.CauseServiceDisconnected)
{
Log.Info("Health365", "Connection lost. Reason: Service Disconnected");
}
}
}