Added pedometer
This commit is contained in:
parent
1ecd0e0cf0
commit
21146a11fd
44 changed files with 991 additions and 2 deletions
9
MoCha/Assets/Pedometer.meta
Normal file
9
MoCha/Assets/Pedometer.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7a73552b2c9c547dc8b9a92c15744bd8
|
||||
folderAsset: yes
|
||||
timeCreated: 1497547079
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Editor.meta
Normal file
9
MoCha/Assets/Pedometer/Editor.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 188f5c9ce0ac2439499a73685331a44d
|
||||
folderAsset: yes
|
||||
timeCreated: 1497553151
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
53
MoCha/Assets/Pedometer/Editor/PedometerEditor.cs
Normal file
53
MoCha/Assets/Pedometer/Editor/PedometerEditor.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Utilities {
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEditor.Build;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
#if UNITY_IOS
|
||||
using UnityEditor.Callbacks;
|
||||
using UnityEditor.iOS.Xcode;
|
||||
#endif
|
||||
|
||||
public class PedometerEditor : IPreprocessBuild, IPostprocessBuild, IOrderedCallback {
|
||||
|
||||
private string AndroidPlugins {get {return Path.Combine(Environment.CurrentDirectory, "Assets/Plugins/Android");}}
|
||||
int IOrderedCallback.callbackOrder {get {return 0;}}
|
||||
|
||||
private const string
|
||||
MotionUsageKey = @"NSMotionUsageDescription",
|
||||
MotionUsageDescription = @"Allow this app to use the pedometer."; // Change this as necessary
|
||||
|
||||
void IPreprocessBuild.OnPreprocessBuild(BuildTarget target, string path) {
|
||||
#if UNITY_ANDROID
|
||||
// Create the Android plugins directory
|
||||
Directory.CreateDirectory(AndroidPlugins);
|
||||
// Copy the manifest into it // This is the only place where Unity picks up manifests, so we have to copy into it
|
||||
File.Copy(Path.Combine(Environment.CurrentDirectory, "Assets/Pedometer/Plugins/Android/AndroidManifest.xml"), Path.Combine(AndroidPlugins, "AndroidManifest.xml"));
|
||||
#elif UNITY_IOS
|
||||
// Get the property list
|
||||
string plistPath = path + "/Info.plist";
|
||||
PlistDocument plist = new PlistDocument();
|
||||
// Read it
|
||||
plist.ReadFromString(File.ReadAllText(plistPath));
|
||||
PlistElementDict rootDictionary = plist.root;
|
||||
// Add the motion usage description
|
||||
rootDictionary.SetString(MotionUsageKey, MotionUsageDescription);
|
||||
File.WriteAllText(plistPath, plist.WriteToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
void IPostprocessBuild.OnPostprocessBuild (BuildTarget target, string path) {
|
||||
#if UNITY_ANDROID
|
||||
// Delete the Android manifest from Plugins/Android
|
||||
File.Delete(Path.Combine(AndroidPlugins, "AndroidManifest.xml"));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
12
MoCha/Assets/Pedometer/Editor/PedometerEditor.cs.meta
Normal file
12
MoCha/Assets/Pedometer/Editor/PedometerEditor.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dec45390b23614bf898574d3ef1a2278
|
||||
timeCreated: 1497553151
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 16f5885743c8a4311b5892c1f5bc06ad
|
||||
folderAsset: yes
|
||||
timeCreated: 1497486100
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins/Android.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins/Android.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f118efd25bcfb44008cbb6a4bd8d38aa
|
||||
folderAsset: yes
|
||||
timeCreated: 1497486100
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
13
MoCha/Assets/Pedometer/Plugins/Android/AndroidManifest.xml
Normal file
13
MoCha/Assets/Pedometer/Plugins/Android/AndroidManifest.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yusufolokoba.pedometer">
|
||||
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
|
||||
<activity android:name="com.yusufolokoba.pedometer.PedometerActivity"
|
||||
android:label="@string/app_name"
|
||||
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6d12e0df1fc5342fca4a7a81d9a17a2d
|
||||
timeCreated: 1497486100
|
||||
licenseType: Pro
|
||||
TextScriptImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
16
MoCha/Assets/Pedometer/Plugins/Android/Makefile
Normal file
16
MoCha/Assets/Pedometer/Plugins/Android/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
#
|
||||
# Pedometer
|
||||
# Copyright (c) 2017 Yusuf Olokoba
|
||||
#
|
||||
|
||||
SDKLIB := /Users/yusuf/Documents/ADT/SDK/platforms/android-23/android.jar # Replace with your path to the Android base class library
|
||||
UNITYLIB := /Applications/Unity/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar # Replace with your path to Unity's classes.jar
|
||||
SRC := PedometerActivity
|
||||
|
||||
build:
|
||||
@echo "Building..."
|
||||
javac -source 1.6 -target 1.6 "$(SRC).java" -bootclasspath $(SDKLIB) -classpath $(UNITYLIB) -d .
|
||||
jar cf "$(SRC).jar" com
|
||||
@rm -rf com
|
||||
@echo "Completed"
|
||||
@echo "------------------------------------------------------ "
|
8
MoCha/Assets/Pedometer/Plugins/Android/Makefile.meta
Normal file
8
MoCha/Assets/Pedometer/Plugins/Android/Makefile.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 100f626688ea24b02a075800806d5cfc
|
||||
timeCreated: 1497486100
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
BIN
MoCha/Assets/Pedometer/Plugins/Android/PedometerActivity.jar
Normal file
BIN
MoCha/Assets/Pedometer/Plugins/Android/PedometerActivity.jar
Normal file
Binary file not shown.
|
@ -0,0 +1,33 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 35031be768e094966bcd410de225237c
|
||||
timeCreated: 1497556782
|
||||
licenseType: Pro
|
||||
PluginImporter:
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
platformData:
|
||||
data:
|
||||
first:
|
||||
Android: Android
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
data:
|
||||
first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
data:
|
||||
first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,69 @@
|
|||
package com.yusufolokoba.pedometer;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.util.Log;
|
||||
import com.unity3d.player.UnityPlayer;
|
||||
import com.unity3d.player.UnityPlayerActivity;
|
||||
|
||||
/**
|
||||
* Pedometer
|
||||
* Created by Yusuf on 06/14/17.
|
||||
*/
|
||||
public class PedometerActivity extends UnityPlayerActivity implements SensorEventListener {
|
||||
|
||||
private Sensor sensor;
|
||||
private SensorManager manager;
|
||||
|
||||
//region --Client API--
|
||||
|
||||
public void initialize () {
|
||||
// Get sensor manager
|
||||
manager = manager == null ? (SensorManager)getSystemService(Context.SENSOR_SERVICE) : manager;
|
||||
// Get sensor
|
||||
if ((sensor = manager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)) == null) {
|
||||
Log.e("Unity", "Pedometer Error: Failed to acquire step counter sensor");
|
||||
return;
|
||||
}
|
||||
// Start listening
|
||||
manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
|
||||
// Log
|
||||
Log.d("Unity", "Pedometer: Initialized Android backend");
|
||||
}
|
||||
|
||||
public void release () {
|
||||
// Stop listening
|
||||
manager.unregisterListener(this);
|
||||
// Dereference
|
||||
sensor = null;
|
||||
// Log
|
||||
Log.d("Unity", "Pedometer: Released Android backend");
|
||||
}
|
||||
|
||||
public boolean isSupported () {
|
||||
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER);
|
||||
}
|
||||
//endregion
|
||||
|
||||
|
||||
//region --Callbacks--
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged (Sensor sensor, int accuracy) {}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged (SensorEvent event) {
|
||||
// Extract data
|
||||
final double
|
||||
STEP2METERS = 0.715d,
|
||||
steps = event.values[0],
|
||||
distance = steps * STEP2METERS;
|
||||
// Send to Unity
|
||||
UnityPlayer.UnitySendMessage("Pedometer", "OnEvent", String.format("%d:%f", (int)steps, distance));
|
||||
}
|
||||
//endregion
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 691a262e9a06c47579c25a6838a5d097
|
||||
timeCreated: 1497547079
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins/Managed.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins/Managed.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f76f71a93492f4ccd84fe17da32466a5
|
||||
folderAsset: yes
|
||||
timeCreated: 1497488311
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
33
MoCha/Assets/Pedometer/Plugins/Managed/IPedometer.cs
Normal file
33
MoCha/Assets/Pedometer/Plugins/Managed/IPedometer.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Platforms {
|
||||
|
||||
public interface IPedometer {
|
||||
|
||||
#region --Properties--
|
||||
/// <summary>
|
||||
/// Event used to propagate step events
|
||||
/// </summary>
|
||||
event StepCallback OnStep;
|
||||
/// <summary>
|
||||
/// Is this implementation supported by the current platform?
|
||||
/// </summary>
|
||||
bool IsSupported {get;}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Client API--
|
||||
/// <summary>
|
||||
/// Initialize this Pedometer implementation
|
||||
/// </summary>
|
||||
IPedometer Initialize ();
|
||||
/// <summary>
|
||||
/// Teardown this Pedometer implementation and release any resources
|
||||
/// </summary>
|
||||
void Release ();
|
||||
#endregion
|
||||
}
|
||||
}
|
12
MoCha/Assets/Pedometer/Plugins/Managed/IPedometer.cs.meta
Normal file
12
MoCha/Assets/Pedometer/Plugins/Managed/IPedometer.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 88d6047c336bd4e2b96e973d7f5e2cb6
|
||||
timeCreated: 1497547083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
94
MoCha/Assets/Pedometer/Plugins/Managed/Pedometer.cs
Normal file
94
MoCha/Assets/Pedometer/Plugins/Managed/Pedometer.cs
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU {
|
||||
|
||||
using Platforms;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public sealed class Pedometer : IDisposable {
|
||||
|
||||
#region --Properties--
|
||||
|
||||
/// <summary>
|
||||
/// How many updates has this pedometer received? Useful for calculating pedometer precision
|
||||
/// </summary>
|
||||
public int updateCount {get; private set;}
|
||||
|
||||
/// <summary>
|
||||
/// Pedometer implementation for the current device. Do not use unless you know what you are doing
|
||||
/// </summary>
|
||||
public static IPedometer Implementation {
|
||||
get {
|
||||
return _Implementation = _Implementation ?? new IPedometer[] {
|
||||
new PedometerAndroid(),
|
||||
new PedometeriOS(),
|
||||
new PedometerGPS() // Always supported, uses GPS (so highly inaccurate)
|
||||
}.First(impl => impl.IsSupported).Initialize();
|
||||
}
|
||||
}
|
||||
private static IPedometer _Implementation;
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Op vars--
|
||||
private int initialSteps; // Some step counters count from device boot, so subtract the initial count we get
|
||||
private double initialDistance;
|
||||
private readonly StepCallback callback;
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Ctor--
|
||||
|
||||
/// <summary>
|
||||
/// Create a new pedometer and start listening for updates
|
||||
/// </summary>
|
||||
public Pedometer (StepCallback callback) {
|
||||
// Save the callback
|
||||
this.callback = callback;
|
||||
// Register callback
|
||||
Implementation.OnStep += OnStep;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Operations--
|
||||
|
||||
/// <summary>
|
||||
/// Stop listening for pedometer updates and dispose the object
|
||||
/// </summary>
|
||||
public void Dispose () {
|
||||
// Unregister callback
|
||||
Implementation.OnStep -= OnStep;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release Pedometer and all of its resources
|
||||
/// </summary>
|
||||
public static void Release () {
|
||||
if (_Implementation == null) return;
|
||||
// Release and dereference
|
||||
_Implementation.Release();
|
||||
_Implementation = null;
|
||||
}
|
||||
|
||||
private void OnStep (int steps, double distance) { // DEPLOY // UpdateCount post increment
|
||||
// Set initials and increment update count
|
||||
initialSteps = updateCount++ == 0 ? steps : initialSteps;
|
||||
initialDistance = steps == initialSteps ? distance : initialDistance;
|
||||
// If this is not the first step, then invoke the callback
|
||||
if (steps != initialSteps) if (callback != null) callback(steps - initialSteps, distance - initialDistance);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A delegate used to pass pedometer information
|
||||
/// </summary>
|
||||
/// <param name="steps">Number of steps taken</param>
|
||||
/// <param name="distance">Distance walked in meters</param>
|
||||
public delegate void StepCallback (int steps, double distance);
|
||||
}
|
12
MoCha/Assets/Pedometer/Plugins/Managed/Pedometer.cs.meta
Normal file
12
MoCha/Assets/Pedometer/Plugins/Managed/Pedometer.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c21bada5b411643bbac7a50e3505bfb4
|
||||
timeCreated: 1497547083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins/Managed/Platforms.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins/Managed/Platforms.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 977b4cd8abd1a471daca27683d9cca3a
|
||||
folderAsset: yes
|
||||
timeCreated: 1497488311
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Platforms {
|
||||
|
||||
using UnityEngine;
|
||||
using Utilities;
|
||||
|
||||
public sealed class PedometerAndroid : IPedometer {
|
||||
|
||||
#region --Properties--
|
||||
|
||||
public event StepCallback OnStep {
|
||||
add {
|
||||
PedometerHelper.Instance.OnStep += value;
|
||||
} remove {
|
||||
PedometerHelper.Instance.OnStep -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSupported {
|
||||
get {
|
||||
#if !UNITY_ANDROID || UNITY_EDITOR
|
||||
return false;
|
||||
#endif
|
||||
#pragma warning disable 0162
|
||||
// Get a reference to PedometerActivity
|
||||
using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) pedometer = player.GetStatic<AndroidJavaObject>("currentActivity");
|
||||
// Check if supported
|
||||
return pedometer.Call<bool>("isSupported");
|
||||
#pragma warning restore 0162
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Op vars--
|
||||
private AndroidJavaObject pedometer;
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Client API--
|
||||
|
||||
public IPedometer Initialize () {
|
||||
// Initialize pedometer natively
|
||||
pedometer.Call("initialize");
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Release () {
|
||||
// Release pedometer natively
|
||||
pedometer.Call("release");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a4ea9668c61c34ca1bdb687aa038db95
|
||||
timeCreated: 1497547449
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
103
MoCha/Assets/Pedometer/Plugins/Managed/Platforms/PedometerGPS.cs
Normal file
103
MoCha/Assets/Pedometer/Plugins/Managed/Platforms/PedometerGPS.cs
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Platforms {
|
||||
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Utilities;
|
||||
|
||||
public sealed class PedometerGPS : IPedometer {
|
||||
|
||||
#region --Properties--
|
||||
public event StepCallback OnStep;
|
||||
public bool IsSupported {get {return true;}}
|
||||
public bool Running {get; private set;}
|
||||
private const double
|
||||
LocationTimeout = 20f,
|
||||
EarthRadius = 6378.137d, // Meters
|
||||
DegreesToRadians = Math.PI / 180d,
|
||||
StepsToMeters = 0.715f;
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Client API--
|
||||
|
||||
public IPedometer Initialize () {
|
||||
// Check that the user has granted permissions
|
||||
if (!Input.location.isEnabledByUser) Debug.LogError("Pedometer Error: GPS backend requires GPS permissions");
|
||||
// Initialize location services
|
||||
else PedometerHelper.Instance.Dispatch(Update());
|
||||
// Log
|
||||
Debug.Log("Pedometer: Initialized GPS backend");
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Release () {
|
||||
// Stop running
|
||||
Running = false;
|
||||
// Log
|
||||
Debug.Log("Pedometer: Released GPS backend");
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Operations--
|
||||
|
||||
private IEnumerator Update () {
|
||||
// Start location service
|
||||
Input.location.Start(0.5f, 3f);
|
||||
// Wait until service initializes
|
||||
var time = Time.realtimeSinceStartup;
|
||||
yield return new WaitWhile(() => Input.location.status == LocationServiceStatus.Initializing && Time.realtimeSinceStartup - time < LocationTimeout);
|
||||
// Check that service initialized
|
||||
if (Input.location.status != LocationServiceStatus.Running) {
|
||||
Debug.LogError("Pedometer Error: Failed to initialize location service with status: "+Input.location.status);
|
||||
yield break;
|
||||
}
|
||||
// Get current location
|
||||
var lastData = Input.location.lastData;
|
||||
var distance = 0.0d;
|
||||
Running = true;
|
||||
// Update location
|
||||
while (Running) {
|
||||
// State checking
|
||||
if (Input.location.status != LocationServiceStatus.Running) yield break;
|
||||
// Check that the data was updated
|
||||
if (Math.Abs(Input.location.lastData.timestamp - lastData.timestamp) < 1e-10) goto end;
|
||||
// Accumulate haversine distance
|
||||
var currentData = Input.location.lastData;
|
||||
distance += Distance(lastData.latitude, lastData.longitude, currentData.latitude, currentData.longitude);
|
||||
// Invoke event
|
||||
if (OnStep != null) OnStep((int)(distance / StepsToMeters), distance);
|
||||
// Update data
|
||||
lastData = currentData;
|
||||
// Next frame
|
||||
end: yield return null;
|
||||
}
|
||||
// Stop location service
|
||||
Input.location.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the haversine distance between two latitude-longitude pairs
|
||||
/// Implemented from: https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates
|
||||
/// </summary>
|
||||
private static double Distance (double lat1, double lon1, double lat2, double lon2) {
|
||||
double
|
||||
dlat = (lat2 - lat1) * DegreesToRadians,
|
||||
dlong = (lon2 - lon1) * DegreesToRadians,
|
||||
a = Math.Pow(Math.Sin(dlat / 2.0), 2) +
|
||||
Math.Cos(lat1 * DegreesToRadians) *
|
||||
Math.Cos(lat2 * DegreesToRadians) *
|
||||
Math.Pow(Math.Sin(dlong / 2.0), 2),
|
||||
c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a)),
|
||||
distance = EarthRadius * c;
|
||||
return distance;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 35cee1988de7f4d63b77211cc91fb2c4
|
||||
timeCreated: 1497635191
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Platforms {
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using Utilities;
|
||||
|
||||
public sealed class PedometeriOS : IPedometer {
|
||||
|
||||
#region --Properties--
|
||||
|
||||
public event StepCallback OnStep {
|
||||
add {
|
||||
PedometerHelper.Instance.OnStep += value;
|
||||
} remove {
|
||||
PedometerHelper.Instance.OnStep -= value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSupported {
|
||||
get {
|
||||
#if !UNITY_IOS || UNITY_EDITOR
|
||||
return false;
|
||||
#endif
|
||||
#pragma warning disable 0162
|
||||
return PDIsSupported();
|
||||
#pragma warning restore 0162
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Client API--
|
||||
|
||||
public IPedometer Initialize () {
|
||||
// Initialize pedometer natively
|
||||
PDInitialize();
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Release () {
|
||||
// Release pedometer natively
|
||||
PDRelease();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Bridge--
|
||||
|
||||
#if UNITY_IOS
|
||||
[DllImport("__Internal")]
|
||||
private static extern void PDInitialize ();
|
||||
[DllImport("__Internal")]
|
||||
private static extern void PDRelease ();
|
||||
[DllImport("__Internal")]
|
||||
private static extern bool PDIsSupported ();
|
||||
|
||||
#else // Keep IL2CPP happy
|
||||
private static void PDInitialize () {}
|
||||
private static void PDRelease () {}
|
||||
private static bool PDIsSupported () {return false;}
|
||||
#endif
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 70a78ba5eb6f443e6acf356ebd08f735
|
||||
timeCreated: 1497547449
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins/Managed/Utilities.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins/Managed/Utilities.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ee053aa1ec5124ef392cdea2fdae0609
|
||||
folderAsset: yes
|
||||
timeCreated: 1497549080
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Utilities {
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// Helper class used for dispatching coroutines and receiving native events raised by UnitySendMessage
|
||||
/// </summary>
|
||||
public class PedometerHelper : MonoBehaviour {
|
||||
|
||||
|
||||
#region --Properties--
|
||||
public event StepCallback OnStep;
|
||||
public static readonly PedometerHelper Instance;
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Client API--
|
||||
|
||||
/// <summary>
|
||||
/// Dispatch a coroutine to be invoked
|
||||
/// </summary>
|
||||
/// <returns>The queued coroutine</returns>
|
||||
public Coroutine Dispatch (IEnumerator routine) {
|
||||
return StartCoroutine(routine);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event used by native implementations to report pedometer events
|
||||
/// </summary>
|
||||
private void OnEvent (string data) {
|
||||
// Schema: "steps:distance"
|
||||
int steps; double distance; string[] tokens = data.Split(':');
|
||||
// Parse
|
||||
if (OnStep == null || !int.TryParse(tokens[0], out steps) || !double.TryParse(tokens[1], out distance)) return;
|
||||
// Raise event
|
||||
OnStep(steps, distance);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region --Initialization--
|
||||
|
||||
static PedometerHelper () {
|
||||
// Create the singleton
|
||||
Instance = new GameObject("Pedometer").AddComponent<PedometerHelper>();
|
||||
}
|
||||
|
||||
private void Awake () {
|
||||
// Preserve across scenes
|
||||
DontDestroyOnLoad(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9304dcc86ca774a288d7b682a7e9c429
|
||||
timeCreated: 1497549080
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
MoCha/Assets/Pedometer/Plugins/iOS.meta
Normal file
9
MoCha/Assets/Pedometer/Plugins/iOS.meta
Normal file
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6eb84ce3919f84b27ba30affccff2479
|
||||
folderAsset: yes
|
||||
timeCreated: 1497486100
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
42
MoCha/Assets/Pedometer/Plugins/iOS/Pedometer.mm
Normal file
42
MoCha/Assets/Pedometer/Plugins/iOS/Pedometer.mm
Normal file
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// Pedometer.mm
|
||||
// Pedometer
|
||||
//
|
||||
// Created by Yusuf on 06/15/17.
|
||||
// Copyright (c) 2017 Yusuf Olokoba
|
||||
//
|
||||
|
||||
#import <CoreMotion/CoreMotion.h>
|
||||
|
||||
#define BRIDGE extern "C"
|
||||
#define STEP2METERS 0.715
|
||||
|
||||
static CMPedometer* pedometer;
|
||||
|
||||
BRIDGE void PDInitialize () {
|
||||
// Create an instance
|
||||
pedometer = [CMPedometer new];
|
||||
// Start updates
|
||||
[pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData* data, NSError* error) {
|
||||
// Extract data
|
||||
int steps = data.numberOfSteps.intValue;
|
||||
double distance = CMPedometer.isDistanceAvailable ? data.distance.doubleValue : steps * STEP2METERS;
|
||||
// Send to Unity
|
||||
UnitySendMessage("Pedometer", "OnEvent", [[NSString stringWithFormat:@"%i:%f", steps, distance] UTF8String]);
|
||||
}];
|
||||
// Log
|
||||
NSLog(@"%s", "Pedometer: Initialized iOS backend");
|
||||
}
|
||||
|
||||
BRIDGE void PDRelease () {
|
||||
// Release and dereference
|
||||
if (pedometer) [pedometer stopPedometerUpdates];
|
||||
pedometer = nil;
|
||||
// Log
|
||||
NSLog(@"%s", "Pedometer: Released iOS backend");
|
||||
}
|
||||
|
||||
BRIDGE bool PDIsSupported () {
|
||||
// Check if step counting is available
|
||||
return CMPedometer.isStepCountingAvailable;
|
||||
}
|
39
MoCha/Assets/Pedometer/Plugins/iOS/Pedometer.mm.meta
Normal file
39
MoCha/Assets/Pedometer/Plugins/iOS/Pedometer.mm.meta
Normal file
|
@ -0,0 +1,39 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0c8960212d3114db98deabda72834807
|
||||
timeCreated: 1497547087
|
||||
licenseType: Pro
|
||||
PluginImporter:
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
isPreloaded: 0
|
||||
isOverridable: 0
|
||||
platformData:
|
||||
data:
|
||||
first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 0
|
||||
settings: {}
|
||||
data:
|
||||
first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
data:
|
||||
first:
|
||||
iPhone: iOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
data:
|
||||
first:
|
||||
tvOS: tvOS
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
42
MoCha/Assets/Pedometer/README.md
Normal file
42
MoCha/Assets/Pedometer/README.md
Normal file
|
@ -0,0 +1,42 @@
|
|||
# Pedometer API
|
||||
Pedometer is a native pedometer API for the Unity game engine. The API provides sensor information about step count and distance covered (in meters). It is a minimalist API with native backends on iOS and Android. There is also a backend that uses GPS in the case that a hardware pedometer is not available for use. Download the unitypackage [here]().
|
||||
|
||||
## Pedometer
|
||||
To use the API, simply create a `Pedometer` instance:
|
||||
```csharp
|
||||
// Create a pedometer
|
||||
// The OnStep callback will be invoked each time a step is detected
|
||||
var pedometer = new Pedometer(OnStep);
|
||||
|
||||
void OnStep (int steps, double distance) {
|
||||
// Display the values
|
||||
stepText.text = steps.ToString();
|
||||
// Display distance in feet
|
||||
distanceText.text = (distance * 3.28084).ToString("F2") + " ft";
|
||||
}
|
||||
```
|
||||
|
||||
When you are done detecting steps from a `Pedometer` instance, you must dispose it:
|
||||
```csharp
|
||||
// Dispose of this pedometer instance
|
||||
pedometer.Dispose();
|
||||
```
|
||||
|
||||
Because the API uses native resources, you may wish to release these resources (like stopping the step sensor to save power). To do so, use the `Release` function:
|
||||
```csharp
|
||||
// Release Pedometer's native resources
|
||||
Pedometer.Release();
|
||||
```
|
||||
|
||||
## Architecture
|
||||
The Pedometer API is modeled after the NatCam API, comprising of a unified front end and several platform-specific backend implementations. Each backend implementation must implement the `IPedometer` interface. These backend implementations are responsible for managing native resources and sensor events. On Android, Pedometer uses `SensorManager` whereas on iOS, Pedometer uses `CMPedometer`.
|
||||
|
||||
## Modifications
|
||||
To make modifications, simply edit the sources in Pedometer>Plugins. The `Android` directory contains the Android source, a Makefile, and an AndroidManifest.xml (which must not be renamed or moved). To build your Android changes, modify the Makefile with paths specific to your installations then run `make` in the `Android` directory. The java source will be compiled into a .jar which will be included in the application binary.
|
||||
|
||||
On iOS, simply make changes you want. No need to build anything; the Unity build process will copy the Objective-C++ sources to the XCode project.
|
||||
|
||||
Please contribute your changes back to the repository! Since Pedometer is open source, its development must be driven by the community.
|
||||
|
||||
## Credits
|
||||
- [Yusuf Olokoba](mailto:olokobayusuf@gmail.com)
|
8
MoCha/Assets/Pedometer/README.md.meta
Normal file
8
MoCha/Assets/Pedometer/README.md.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ce546a2d42ba34e34a05f4ef8c708d74
|
||||
timeCreated: 1497488311
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Binary file not shown.
BIN
MoCha/Assets/Scenes/StepCounter.unity
Normal file
BIN
MoCha/Assets/Scenes/StepCounter.unity
Normal file
Binary file not shown.
8
MoCha/Assets/Scenes/StepCounter.unity.meta
Normal file
8
MoCha/Assets/Scenes/StepCounter.unity.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e5fcab55ab6a24d8eb5dd6198b597019
|
||||
timeCreated: 1497532216
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
MoCha/Assets/Scripts/SceneLoader.cs
Normal file
11
MoCha/Assets/Scripts/SceneLoader.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
|
||||
public class SceneLoader : MonoBehaviour {
|
||||
|
||||
public void LoadScene(int scene)
|
||||
{
|
||||
SceneManager.LoadScene(scene);
|
||||
}
|
||||
}
|
13
MoCha/Assets/Scripts/SceneLoader.cs.meta
Normal file
13
MoCha/Assets/Scripts/SceneLoader.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 39ce724310132854a9cfdd3e31737ae7
|
||||
timeCreated: 1523377353
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -1,8 +1,7 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class SpinningCube : MonoBehaviour
|
||||
{
|
||||
public class SpinningCube : MonoBehaviour {
|
||||
public float m_Speed = 20f;
|
||||
|
||||
private Vector3 m_RotationDirection = Vector3.up;
|
||||
|
|
35
MoCha/Assets/Scripts/StepCounter.cs
Normal file
35
MoCha/Assets/Scripts/StepCounter.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Pedometer
|
||||
* Copyright (c) 2017 Yusuf Olokoba
|
||||
*/
|
||||
|
||||
namespace PedometerU.Tests {
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class StepCounter : MonoBehaviour {
|
||||
|
||||
public Text stepText, distanceText;
|
||||
private Pedometer pedometer;
|
||||
|
||||
private void Start () {
|
||||
// Create a new pedometer
|
||||
pedometer = new Pedometer(OnStep);
|
||||
// Reset UI
|
||||
OnStep(0, 0);
|
||||
}
|
||||
|
||||
private void OnStep (int steps, double distance) {
|
||||
// Display the values // Distance in feet
|
||||
stepText.text = steps.ToString();
|
||||
distanceText.text = (distance * 3.28084).ToString("F2") + " ft";
|
||||
}
|
||||
|
||||
private void OnDisable () {
|
||||
// Release the pedometer
|
||||
pedometer.Dispose();
|
||||
pedometer = null;
|
||||
}
|
||||
}
|
||||
}
|
12
MoCha/Assets/Scripts/StepCounter.cs.meta
Normal file
12
MoCha/Assets/Scripts/StepCounter.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 21f5752f378f34804a2850c37523d8ba
|
||||
timeCreated: 1497533908
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Binary file not shown.
Binary file not shown.
Reference in a new issue