Reputation: 457
I'm trying to detect start/stop motion of an Android device in a moving car. Towards this, I'm trying to use the Significant Motion Sensor on my Google Nexus 7 (2012) tablet (Jellybean). The reference code I used is this.
However, the code never detects Significant motion and SensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION);
always returns null in the code.
Could you please suggest what I'm missing? Any other way to detect start/stop motion of Android Device in a moving car using other sensors?
Upvotes: 2
Views: 1568
Reputation: 102376
Could you please suggest what I'm missing?
The tablet probably does not supply the sensor. I've found many low end tablets only provide an accelerometer. Sensor rich devices supply 5 or 6 sensors, but I don't recall coming across the Significant Motion sensor.
You can use the following code to dump the sensors from JNI. I'm not sure if you will get the same results from Java. I use this code to sample sensors and seed a random number generator, so I've looked at them on a number of devices. The line of interest is int type = ASensor_getType(sensor);
.
ASensorManager* sensorManager = ASensorManager_getInstance();
int n = ASensorManager_getSensorList(sensorManager, &sensorArray);
for (int i = 0; i < n; i++) {
const ASensor* sensor = sensorArray[i];
if (sensor == NULL) continue;
const char* name = ASensor_getName(sensor);
int type = ASensor_getType(sensor);
const char* vendor = ASensor_getVendor(sensor);
int min_delay = ASensor_getMinDelay(sensor);
float resolution = ASensor_getResolution(sensor);
LOG_DEBUG("SensorArray: %s (%s) %d %d %f", name, vendor, type,
min_delay, resolution);
}
After you call int type = ASensor_getType(sensor);
, you can decode the type with the following function.
static const char* SensorTypeToName(int sensorType) {
switch (sensorType) {
/* <ndk root>/.../sensor.h */
case ASENSOR_TYPE_ACCELEROMETER: /* 1 */
return "Accelerometer";
case ASENSOR_TYPE_MAGNETIC_FIELD: /* 2 */
return "Magnetic field";
case ASENSOR_TYPE_GYROSCOPE: /* 4 */
return "Gyroscope";
case ASENSOR_TYPE_LIGHT: /* 5 */
return "Light";
case ASENSOR_TYPE_PROXIMITY: /* 8 */
return "Proximity";
/* http://developer.android.com/reference/android/hardware/Sensor.html */
case 0:
return "type 0";
case 3:
return "Orientation";
case 6:
return "Pressure";
case 7:
return "Temperature";
case 9:
return "Gravity";
case 10:
return "Linear acceleration";
case 11:
return "Rotation vector";
case 12:
return "Relative humidity";
case 13:
return "Ambient temperature";
case 14:
return "Uncalibrated magnetic field";
case 15:
return "Rotation vector";
case 16:
return "Uncalibrated gyroscope";
case 17:
return "Significant motion";
case 18:
return "type 18";
case 19:
return "Step counter";
case 20:
return "Geo-magnetic rotation vector";
case 21:
return "Heart rate";
default:
;
}
return "Unknown";
}
Upvotes: 2