Example usage for android.hardware Sensor TYPE_GRAVITY

List of usage examples for android.hardware Sensor TYPE_GRAVITY

Introduction

In this page you can find the example usage for android.hardware Sensor TYPE_GRAVITY.

Prototype

int TYPE_GRAVITY

To view the source code for android.hardware Sensor TYPE_GRAVITY.

Click Source Link

Document

A constant describing a gravity sensor type.

Usage

From source file:com.example.sensingapp.SensingApp.java

public void recordSensingInfo(SensorData senData) {
    String sRecordLine;/*from   w  w w.  ja  va 2  s . c  o m*/
    String sTimeField;
    Date dtCurDate;
    int i;
    long lStartTime = 0;
    long lCurrentTime = 0;
    SimpleDateFormat spdRecordTime, spdCurDateTime;
    final String DATE_FORMAT = "yyyyMMddHHmmss";
    final String DATE_FORMAT_S = "yyMMddHHmmssSSS"; //"yyyyMMddHHmmssSSS"
    int nSensorReadingType = SENSOR_EVENT_NULL;
    int nSensorDataType;

    if (m_blnRecordStatus == false) { //Stopped
        return;
    }

    dtCurDate = new Date();

    // Timestamp for the record
    spdRecordTime = new SimpleDateFormat(DATE_FORMAT_S);
    sTimeField = spdRecordTime.format(dtCurDate);

    nSensorDataType = senData.getSensorDataType();

    if (nSensorDataType == DATA_TYPE_SENSOR) {
        SensorEvent event;

        event = senData.getSensorEvent();

        synchronized (this) {
            switch (event.sensor.getType()) {

            case Sensor.TYPE_ACCELEROMETER:
                //X, Y, Z
                if (m_blnAcclEnabled) {
                    m_sAccl = Float.toString(event.values[0]) + "," + Float.toString(event.values[1]) + ","
                            + Float.toString(event.values[2]) + ",";

                    nSensorReadingType = SENSOR_EVENT_ACCL;
                }

                //                   if (m_blnOrientEnabled) {
                //                      m_arrfAcclValues = event.values.clone();
                //                      
                //                      if (calculateOrientation()) {
                //                         //Azimuth (rotation around z-axis); Pitch (rotation around x-axis), Roll (rotation around y-axis)
                //                         m_sOrient = Float.toString(m_arrfOrientValues[0]) + "," + 
                //                                  Float.toString(m_arrfOrientValues[1]) + "," + 
                //                                  Float.toString(m_arrfOrientValues[2]) + ",";
                //                         
                //                         nSensorReadingType = SENSOR_EVENT_ORIENT;
                //                         
                //                      }
                //                   }
                break;

            case Sensor.TYPE_LINEAR_ACCELERATION:
                //X,Y,Z
                if (m_blnLinearAcclEnabled) {
                    m_sLinearAccl = Float.toString(event.values[0]) + "," + Float.toString(event.values[1])
                            + "," + Float.toString(event.values[2]) + ",";

                    nSensorReadingType = SENSOR_EVENT_LINEAR_ACCL;
                }

                break;

            case Sensor.TYPE_GRAVITY:
                //X,Y,Z
                if (m_blnGravityEnabled) {
                    m_sGravity = Float.toString(event.values[0]) + "," + Float.toString(event.values[1]) + ","
                            + Float.toString(event.values[2]) + ",";

                    nSensorReadingType = SENSOR_EVENT_GRAVITY;
                }

                break;

            case Sensor.TYPE_GYROSCOPE:
                //X,Y,Z
                m_sGyro = Float.toString(event.values[0]) + "," + Float.toString(event.values[1]) + ","
                        + Float.toString(event.values[2]) + ",";
                nSensorReadingType = SENSOR_EVENT_GYRO;
                break;

            case Sensor.TYPE_MAGNETIC_FIELD:
                // Values are in micro-Tesla (uT) and measure the ambient magnetic field 
                if (m_blnMagnetEnabled) {
                    m_sMagnet = Float.toString(event.values[0]) + "," + Float.toString(event.values[1]) + ","
                            + Float.toString(event.values[2]) + ",";

                    nSensorReadingType = SENSOR_EVENT_MAGNET;
                }

                //                   if (m_blnOrientEnabled) {
                //                      m_arrfMagnetValues = event.values.clone();
                //                      
                //                      if (calculateOrientation()) {
                //                         //Azimuth (rotation around z-axis); Pitch (rotation around x-axis), Roll (rotation around y-axis)
                //                         m_sOrient = Float.toString(m_arrfOrientValues[0]) + "," + 
                //                                  Float.toString(m_arrfOrientValues[1]) + "," + 
                //                                  Float.toString(m_arrfOrientValues[2]) + ",";
                //                                                  
                //                         if (nSensorReadingType != SENSOR_EVENT_MAGNET) {
                //                            nSensorReadingType = SENSOR_EVENT_ORIENT;
                //                         }
                //                      }
                //                   }

                break;

            case Sensor.TYPE_ROTATION_VECTOR: //Added on 20150910
                if (m_blnOrientEnabled) {
                    float[] arrfRotVal = new float[3];
                    float[] arrfR = new float[9];
                    float[] arrfValues = new float[3];

                    try {
                        System.arraycopy(event.values, 0, arrfRotVal, 0, event.values.length);
                    } catch (IllegalArgumentException e) {
                        //Hardcode the size to handle a bug on Samsung devices
                        System.arraycopy(event.values, 0, arrfRotVal, 0, 3);
                    }

                    SensorManager.getRotationMatrixFromVector(arrfR, arrfRotVal);
                    SensorManager.getOrientation(arrfR, arrfValues);

                    m_arrfOrientValues[0] = (float) Math.toDegrees(arrfValues[0]);
                    m_arrfOrientValues[1] = (float) Math.toDegrees(arrfValues[1]);
                    m_arrfOrientValues[2] = (float) Math.toDegrees(arrfValues[2]);

                    if (m_arrfOrientValues[0] < 0) {
                        m_arrfOrientValues[0] = m_arrfOrientValues[0] + 360; // Make Azimuth 0 ~ 360
                    }

                    //                      //Azimuth (rotation around z-axis); Pitch (rotation around x-axis), Roll (rotation around y-axis)
                    m_sOrient = Float.toString(m_arrfOrientValues[0]) + ","
                            + Float.toString(m_arrfOrientValues[1]) + ","
                            + Float.toString(m_arrfOrientValues[2]) + ",";

                    //m_tvGpsUp.setText(m_sOrient); //Show orientation
                    nSensorReadingType = SENSOR_EVENT_ORIENT;
                }

                break;

            case Sensor.TYPE_LIGHT:
                // Ambient light level in SI lux units 
                m_sLight = Float.toString(event.values[0]) + ",";
                nSensorReadingType = SENSOR_EVENT_LIGHT;
                break;

            case Sensor.TYPE_PRESSURE:
                // Atmospheric pressure in hPa (millibar)
                m_sBarometer = Float.toString(event.values[0]) + ",";
                nSensorReadingType = SENSOR_EVENT_BAROMETER;
                break;

            }
        }
    } else if (nSensorDataType == DATA_TYPE_GPS) {
        Location locationGps;
        locationGps = senData.getGpsLocation();

        if (locationGps != null) {

            m_location = new Location(locationGps);

            //Change from double to float
            m_sGPS = Float.valueOf((float) (locationGps.getLatitude())).toString() + ","
                    + Float.valueOf((float) (locationGps.getLongitude())).toString() + ",";
            if (locationGps.hasAltitude()) {
                m_sGPS = m_sGPS + Float.valueOf((float) (locationGps.getAltitude())).toString() + ",";
                GeomagneticField geoField = new GeomagneticField(
                        Double.valueOf(locationGps.getLatitude()).floatValue(),
                        Double.valueOf(locationGps.getLongitude()).floatValue(),
                        Double.valueOf(locationGps.getAltitude()).floatValue(), System.currentTimeMillis());
                // Append Declination, in Degree
                m_sGPS = m_sGPS + Float.valueOf((float) (geoField.getDeclination())).toString() + ","
                        + Float.valueOf((float) (geoField.getInclination())).toString() + ",";
            } else {
                m_sGPS = m_sGPS + ",,,";
                //m_sGPS = m_sGPS + ",";
            }

            //New add 201408270009
            if (locationGps.hasSpeed()) {
                m_sGPS = m_sGPS + Float.valueOf((float) (locationGps.getSpeed())).toString() + ",";
            } else {
                m_sGPS = m_sGPS + ",";
            }

            if (locationGps.hasBearing()) {
                m_sGPS = m_sGPS + Float.valueOf((float) (locationGps.getBearing())).toString() + ",";
            } else {
                m_sGPS = m_sGPS + ",";
            }

            nSensorReadingType = SENSOR_EVENT_GPS;

            m_blnGpsUp = true;
            show_screen5_GpsUp();
        } else {
            m_blnGpsUp = false;
            show_screen5_GpsUp();
        }
    } else if (nSensorDataType == DATA_TYPE_MIC) {
        double fSoundLevelDb;
        fSoundLevelDb = senData.getSoundLevelDb();
        m_sSouldLevel = new BigDecimal(fSoundLevelDb).setScale(0, BigDecimal.ROUND_HALF_UP) + ",";

        nSensorReadingType = SENSOR_EVENT_MIC;

    } else if (nSensorDataType == DATA_TYPE_CELLULAR) {
        int nCellId;
        nCellId = senData.getCellId();
        m_sCellId = Integer.valueOf(nCellId).toString() + ",";
        nSensorReadingType = SENSOR_EVENT_CELLULAR;

    } else if (nSensorDataType == DATA_TYPE_WIFI) {
        List<WifiData> lstWifiData = senData.getListWifiData();
        int nWifiCnt = Math.min(WIFI_COUNT, lstWifiData.size());
        m_sWifi = "";
        for (i = 0; i < nWifiCnt; i++) {
            //m_sWifi = m_sWifi + lstWifiData.get(i).getSSID() + "," + lstWifiData.get(i).getBSSID() + "," + lstWifiData.get(i).getSignalLevel() + ",";
            m_sWifi = m_sWifi + lstWifiData.get(i).getBSSID() + "," + lstWifiData.get(i).getSignalLevel() + ",";

        }

        for (i = 1; i <= WIFI_COUNT - nWifiCnt; i++) {
            //m_sWifi = m_sWifi + ",,,";
            m_sWifi = m_sWifi + ",,";
        }

        nSensorReadingType = SENSOR_EVENT_WIFI;
    }

    if (nSensorReadingType == SENSOR_EVENT_NULL) {
        return;
    }

    sRecordLine = sTimeField + ",";

    if (m_blnNoLabel == false) {
        sRecordLine = sRecordLine + m_sCurrentLabel + ",";
    }

    sRecordLine = sRecordLine + Integer.valueOf(nSensorReadingType) + ",";

    //New: Every field always there
    //Field in each line:
    /*
     *  1) Timestamp
     *  2) Label
     *  3) SensingEventType
     *  4-6) Accl
     *  7-9) Linear Accl
     *  10-12) Gravity
     *  13-15) Gyro
     *  16-18) Orientation
     *  19-21) Magnet
     *  22) Light
     *  23) Barometer
     *  24) Sould Level (Decibel)
     *  25) Cell ID
     *  26-32) GPS (Lat, Long, Alt, Declination, Inclination, Speed, Bearing)
     *  33-72) WiFi (<BSSID, Level>) 
     */
    //      sRecordLine = sRecordLine  + m_sAccl + m_sGyro + m_sOrient + m_sMagnet + 
    //                           m_sLight + m_sBarometer +  
    //                           m_sSouldLevel + m_sCellId +
    //                           m_sGPS + m_sWifi;

    sRecordLine = sRecordLine + m_sAccl + m_sLinearAccl + m_sGravity + m_sGyro + m_sOrient + m_sMagnet
            + m_sLight + m_sBarometer + m_sSouldLevel + m_sCellId + m_sGPS + m_sWifi;

    ////////////////////////////
    //      String sAngle = calculateRot(m_sAccl, m_sGravity);
    //      String sarrAngle[] = sAngle.split(",");
    //      String sShow = sarrAngle[0] + "\n" + sarrAngle[1];

    //      String sShow = "";

    //      if (m_sGravity.length() > 3) {
    //         String sarrAngle[] = m_sGravity.split(",");
    //         double fX = Double.valueOf(sarrAngle[0]).doubleValue();
    //         double fY = Double.valueOf(sarrAngle[1]).doubleValue();
    //         double fZ = Double.valueOf(sarrAngle[2]).doubleValue();
    //         
    //         double fTotal = Math.sqrt(fX*fX + fY*fY + fZ*fZ);
    //         
    //         double fAngleZ = Math.acos(fZ/fTotal)/Math.PI*180;
    //         double fAngleY = 90 - Math.acos(fY/fTotal)/Math.PI*180;
    //         double fAngleX = 90 - Math.acos(fX/fTotal)/Math.PI*180;
    //         
    //         sShow = "X:  " +  fAngleX + "\n";
    //         sShow = sShow + "Y:  " +  fAngleY + "\n";
    //         sShow = sShow + "Z:  " +  fAngleZ;
    //         
    //                     
    //      }

    //      if (m_sGravity.length() > 3) {
    //         String sarrAngle[] = m_sGravity.split(",");
    //         double fX = Double.valueOf(sarrAngle[0]).doubleValue();
    //         double fY = Double.valueOf(sarrAngle[1]).doubleValue();
    //         double fZ = Double.valueOf(sarrAngle[2]).doubleValue();
    //         
    //         int nSymbol = 0;
    //         if (fX < 0)  {
    //            sShow = sShow + "- X" + "\n";
    //         } else if (fX > 0) {
    //            sShow = sShow + "+ X" + "\n";
    //         }
    //         
    //         if (fY < 0)  {
    //            sShow = sShow + "- Y" + "\n";
    //         } else if (fY > 0) {
    //            sShow = sShow + "+ Y" + "\n";
    //         }
    //         
    //         if (fZ < 0)  {
    //            sShow = sShow + "- Z";
    //         } else if (fZ > 0) {
    //            sShow = sShow + "+ Z";
    //         }
    //                     
    //      }
    //      
    //      if (m_sGyro.length() > 3) {
    //         String sarrAngle[] = m_sGyro.split(",");
    //         double fX = Double.valueOf(sarrAngle[0]).doubleValue();
    //         double fY = Double.valueOf(sarrAngle[1]).doubleValue();
    //         double fZ = Double.valueOf(sarrAngle[2]).doubleValue();
    //         
    //         int nSymbol = 0;
    //         if (fX < 0)  {
    //            nSymbol = -1;
    //         } else if (fX > 0) {
    //            nSymbol = 1;
    //         }
    //         
    //         if (fY < 0)  {
    //            nSymbol = nSymbol + (-1);
    //         } else if (fY > 0) {
    //            nSymbol = nSymbol + 1;
    //         }
    //         
    //         if (fZ < 0)  {
    //            nSymbol = nSymbol + (-1);
    //         } else if (fZ > 0) {
    //            nSymbol = nSymbol + 1;
    //         }
    //            
    //         if (nSymbol < 0) {
    //            nSymbol = -1;
    //         } else if (nSymbol > 0) {
    //            nSymbol = 1;
    //         }
    //         
    //         sShow = sShow + "\n\n" + nSymbol + "";
    //      }

    //      m_tvSensingInfo.setText(sShow);
    ////////////////////////////

    sRecordLine = sRecordLine + System.getProperty("line.separator");

    if (m_fwSensorRecord != null) {
        //Write information into file
        //Compose information into recordLine
        try {
            m_fwSensorRecord.write(sRecordLine);
        } catch (IOException e) {

        }
    }

}

From source file:com.example.sensingapp.SensingApp.java

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    switch (sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        m_sSensorAccuracy = "1,"; //Accl
        break;//from  www . j  a v a  2s .com

    case Sensor.TYPE_LINEAR_ACCELERATION:
        m_sSensorAccuracy = "2,"; //LinearAccl
        break;

    case Sensor.TYPE_GYROSCOPE:
        m_sSensorAccuracy = "3,"; //Gyro
        break;

    case Sensor.TYPE_MAGNETIC_FIELD:
        m_sSensorAccuracy = "4,"; //Magnet
        break;

    case Sensor.TYPE_GRAVITY:
        m_sSensorAccuracy = "5,"; //Gravity      
        break;

    case Sensor.TYPE_PROXIMITY:
        m_sSensorAccuracy = "6,"; //Proxi
        break;

    case Sensor.TYPE_LIGHT:
        m_sSensorAccuracy = "7,"; //Light
        break;

    case Sensor.TYPE_PRESSURE:
        m_sSensorAccuracy = "8,"; //Barometer
        break;

    case Sensor.TYPE_RELATIVE_HUMIDITY:
        m_sSensorAccuracy = "9,"; //Humidity
        break;

    case Sensor.TYPE_AMBIENT_TEMPERATURE:
        m_sSensorAccuracy = "10,"; //Temperature
        break;

    default:
        m_sSensorAccuracy = "11,"; //Other
    }

    switch (accuracy) {
    case SensorManager.SENSOR_STATUS_ACCURACY_HIGH:
        m_sSensorAccuracy = m_sSensorAccuracy + "1,"; //H
        break;

    case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM:
        m_sSensorAccuracy = m_sSensorAccuracy + "2,"; //M
        break;

    case SensorManager.SENSOR_STATUS_ACCURACY_LOW:
        m_sSensorAccuracy = m_sSensorAccuracy + "3,"; //L
        break;

    case SensorManager.SENSOR_STATUS_UNRELIABLE:
        m_sSensorAccuracy = m_sSensorAccuracy + "4,"; //U
        break;
    }
}