Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ExampleBehaviour/SensorBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ void Update ()
{
#if UNITY_ANDROID
if (plugin != null) {
float[] sensorValue = plugin.Call<float[]>("getSensorValues", "accelerometer");
if (sensorValue != null) {
Debug.Log("sensorValue:" + string.Join(",", new List<float>(sensorValue).ConvertAll(i => i.ToString()).ToArray()));
}
if (plugin.Call<bool>("hasSensor", "accelerometer") == true) {
float[] sensorValue = plugin.Call<float[]>("getSensorValues", "accelerometer");
if (sensorValue != null) {
Debug.Log("sensorValue:" + string.Join(",", new List<float>(sensorValue).ConvertAll(i => i.ToString()).ToArray()));
}
}
}
#endif
}
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ This makes enable more frequently method calling than game FPS.
}
```

Verify if a sensor exist
```c#
#if UNITY_ANDROID
if (plugin.Call<bool>("hasSensor", "accelerometer") == true) {
Debug.log("accelerometer exist");
}
#endif
```

Available Sensors
----
* accelerometer
Expand Down
Binary file modified library/release/AndroidSensorPlugin.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ public static UnitySensorPlugin getInstance() {
return result;
}

/**
* Get if the sensor is valid
* Called by Unity
*
* @param sensorKind sensor kind @see {@link Sensors}
* @return true if the sensor exist
*/

public boolean hasSensor(String sensorKind) {

Sensors specifiedSensor;
try {
specifiedSensor = Sensors.valueOf(sensorKind);
} catch (IllegalArgumentException e) {
// valueOf failed
Log.e(getClass().getName(), "Bad sensor type: " + sensorKind + ", available types: " + Arrays.toString(Sensors.values()));
return false;
}

if( sensorManager.getDefaultSensor( specifiedSensor.getSensorType() ) == null) {
return false;
}

return true;
}

/**
* Terminates instance
* Must be called on App's last use.
Expand Down Expand Up @@ -104,6 +130,22 @@ public void startSensorListening(String sensorKind) {
addSensorEventListener(sensorKind, null, null);
}

/**
* Stop to listen sensor
* Called by Unity
*
* @param sensorKind sensor kind @see {@link Sensors}
* @return true if the listener is destroyed
*/
public boolean stopSensorListening(String sensorKind) {
String sensorKindConverted = convertSensorKind(sensorKind);
if(eventListeners.containsKey(sensorKindConverted)) {
eventListeners.remove(sensorKindConverted);
return true;
}
return false;
}

/**
* Add sensor listener with callback
* Called by Unity
Expand Down