List of usage examples for android.hardware Sensor getType
public int getType()
From source file:de.uni_weimar.benike.misex3.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // register for accelerometer events: mSensorManager = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE); for (Sensor sensor : mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER)) { if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) { mAccelerometerSensor = sensor; }/*from w w w .j a v a2 s . co m*/ } // if we can't access the accelerometer sensor then exit: if (mAccelerometerSensor == null) { Log.e(TAG, "Failed to attach to Accelerator Sensor."); Toast.makeText(this, "Error! Failed to create accelerometer sensor!", Toast.LENGTH_LONG).show(); cleanup(); } mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_UI); // setup the Accelerometer History plot: mAccelerometerPlot = (XYPlot) findViewById(R.id.accelerometerPlot); mFftPlot = (XYPlot) findViewById(R.id.fftPlot); mAccelerometerPlot.setRangeBoundaries(-25, 25, BoundaryMode.FIXED); mAccelerometerPlot.setDomainBoundaries(0, mWindowSize - 1, BoundaryMode.FIXED); mFftPlot.setRangeBoundaries(0, 250, BoundaryMode.FIXED); mFftPlot.setDomainBoundaries(0, mWindowSize / 2, BoundaryMode.AUTO); mFftPlot.setDomainStep(XYStepMode.SUBDIVIDE, 10); //mFftPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 50); mAccelerometerXSeries = new SimpleXYSeries("X"); mAccelerometerXSeries.useImplicitXVals(); mAccelerometerYSeries = new SimpleXYSeries("Y"); mAccelerometerYSeries.useImplicitXVals(); mAccelerometerZSeries = new SimpleXYSeries("Z"); mAccelerometerZSeries.useImplicitXVals(); mAccelerometerMSeries = new SimpleXYSeries("magnitude"); mAccelerometerMSeries.useImplicitXVals(); mFftSeries = new SimpleXYSeries("FFT"); mFftSeries.useImplicitXVals(); mFftPlot.addSeries(mFftSeries, new LineAndPointFormatter(Color.rgb(0, 0, 0), null, null, null)); mAccelerometerPlot.addSeries(mAccelerometerXSeries, new LineAndPointFormatter(Color.rgb(100, 100, 200), null, null, null)); mAccelerometerPlot.addSeries(mAccelerometerYSeries, new LineAndPointFormatter(Color.rgb(100, 200, 100), null, null, null)); mAccelerometerPlot.addSeries(mAccelerometerZSeries, new LineAndPointFormatter(Color.rgb(200, 100, 100), null, null, null)); mAccelerometerPlot.addSeries(mAccelerometerMSeries, new LineAndPointFormatter(Color.rgb(0, 0, 0), null, null, null)); mAccelerometerPlot.setDomainStepValue(5); mAccelerometerPlot.setTicksPerRangeLabel(3); mAccelerometerPlot.setDomainLabel("Sample Index"); mAccelerometerPlot.getDomainLabelWidget().pack(); mAccelerometerPlot.setRangeLabel("m/s^2"); mAccelerometerPlot.getRangeLabelWidget().pack(); final PlotStatistics histStats = new PlotStatistics(1000, false); mAccelerometerPlot.addListener(histStats); // perform hardware accelerated rendering of the plots mAccelerometerPlot.setLayerType(View.LAYER_TYPE_NONE, null); mFftPlot.setLayerType(View.LAYER_TYPE_NONE, null); mFftPlot.setTicksPerRangeLabel(5); mFftPlot.setTicksPerDomainLabel(1); mSampleRateSeekBar = (SeekBar) findViewById(R.id.sampleRateSeekBar); mSampleRateSeekBar.setMax((SAMPLE_MAX_VALUE - SAMPLE_MIN_VALUE) / SAMPLE_STEP); mSampleRateSeekBar.setOnSeekBarChangeListener(this); mFftWindowSeekBar = (SeekBar) findViewById(R.id.fftWindowSeekBar); mFftWindowSeekBar.setMax((WINDOW_MAX_VALUE - WINDOW_MIN_VALUE) / WINDOW_STEP); mFftWindowSeekBar.setOnSeekBarChangeListener(this); mWindowSizeTextView = (TextView) findViewById(R.id.windowSizeTextView); // Perform FFT calculations in background thread mFft = new FFT(mWindowSize); Runnable r = new PerformFft(); mFftThread = new Thread(r); mFftThread.start(); mNotificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_help_outline_black_24dp).setContentTitle("MIS Ex3 Activity Recognizer") .setContentText("Trying to guess your activity").setOngoing(true); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mNotificationBuilder.setContentIntent(resultPendingIntent); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(mNotificationId, mNotificationBuilder.build()); Runnable detectActivity = new DetectActivity(); mDetectActivityThread = new Thread(detectActivity); mDetectActivityThread.start(); }
From source file:com.nextgis.firereporter.SendReportActivity.java
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.report);// w w w . ja v a 2 s .c o m //add fragment FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); frCompass = (CompassFragment) getSupportFragmentManager().findFragmentByTag("COMPASS"); if (frCompass == null) { frCompass = new CompassFragment(); fragmentTransaction.add(R.id.compass_fragment_container, frCompass, "COMPASS").commit(); } getSupportFragmentManager().executePendingTransactions(); getSupportActionBar().setHomeButtonEnabled(true); edLatitude = (EditText) findViewById(R.id.edLatitude); edLongitude = (EditText) findViewById(R.id.edLongitude); edAzimuth = (EditText) findViewById(R.id.edAzimuth); edDistance = (EditText) findViewById(R.id.edDistance); edComment = (EditText) findViewById(R.id.edComment); edDistance.setText("100"); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (mLocationManager != null) { for (String aProvider : mLocationManager.getProviders(false)) { if (aProvider.equals(LocationManager.GPS_PROVIDER)) { gpsAvailable = true; } } if (gpsAvailable) { Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { float lat = (float) (location.getLatitude()); float lon = (float) (location.getLongitude()); edLatitude.setText(Float.toString(lat)); edLongitude.setText(Float.toString(lon)); } else { edLatitude.setText(getString(R.string.noLocation)); edLongitude.setText(getString(R.string.noLocation)); } mLocationListener = new LocationListener() { public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { case LocationProvider.OUT_OF_SERVICE: break; case LocationProvider.TEMPORARILY_UNAVAILABLE: break; case LocationProvider.AVAILABLE: break; } } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } public void onLocationChanged(Location location) { float lat = (float) (location.getLatitude()); float lon = (float) (location.getLongitude()); edLatitude.setText(Float.toString(lat)); edLongitude.setText(Float.toString(lon)); // FIXME: also need to calculate declination? } }; // location listener } else { edLatitude.setText(getString(R.string.noGPS)); edLongitude.setText(getString(R.string.noGPS)); edLatitude.setEnabled(false); edLongitude.setEnabled(false); } } mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); boolean accelerometerAvailable = false; boolean magnetometerAvailable = false; for (Sensor aSensor : mSensorManager.getSensorList(Sensor.TYPE_ALL)) { if (aSensor.getType() == Sensor.TYPE_ACCELEROMETER) { accelerometerAvailable = true; } else if (aSensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { magnetometerAvailable = true; } } compassAvailable = accelerometerAvailable && magnetometerAvailable; if (compassAvailable) { //frCompass = (CompassFragment) getSupportFragmentManager().findFragmentByTag("COMPASS"); if (frCompass != null) { frCompass.SetAzimuthCtrl(edAzimuth); } } else { edAzimuth.setText(getString(R.string.noCompass)); edAzimuth.setEnabled(false); } }
From source file:org.openchaos.android.fooping.service.PingService.java
@Override protected void onHandleIntent(Intent intent) { String clientID = prefs.getString("ClientID", "unknown"); long ts = System.currentTimeMillis(); Log.d(tag, "onHandleIntent()"); // always send ping if (true) {// w ww. j a va2s . c o m try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "ping"); json.put("ts", ts); sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } // http://developer.android.com/training/monitoring-device-state/battery-monitoring.html // http://developer.android.com/reference/android/os/BatteryManager.html if (prefs.getBoolean("UseBattery", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "battery"); json.put("ts", ts); Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); if (batteryStatus != null) { JSONObject bat_data = new JSONObject(); int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if (level >= 0 && scale > 0) { bat_data.put("pct", roundValue(((double) level / (double) scale) * 100, 2)); } else { Log.w(tag, "Battery level unknown"); bat_data.put("pct", -1); } bat_data.put("health", batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)); bat_data.put("status", batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1)); bat_data.put("plug", batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)); bat_data.put("volt", batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1)); bat_data.put("temp", batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)); bat_data.put("tech", batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)); // bat_data.put("present", batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)); json.put("battery", bat_data); } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } // http://developer.android.com/guide/topics/location/strategies.html // http://developer.android.com/reference/android/location/LocationManager.html if (prefs.getBoolean("UseGPS", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "loc_gps"); json.put("ts", ts); if (lm == null) { lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } Location last_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (last_loc != null) { JSONObject loc_data = new JSONObject(); loc_data.put("ts", last_loc.getTime()); loc_data.put("lat", last_loc.getLatitude()); loc_data.put("lon", last_loc.getLongitude()); if (last_loc.hasAltitude()) loc_data.put("alt", roundValue(last_loc.getAltitude(), 4)); if (last_loc.hasAccuracy()) loc_data.put("acc", roundValue(last_loc.getAccuracy(), 4)); if (last_loc.hasSpeed()) loc_data.put("speed", roundValue(last_loc.getSpeed(), 4)); if (last_loc.hasBearing()) loc_data.put("bearing", roundValue(last_loc.getBearing(), 4)); json.put("loc_gps", loc_data); } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } if (prefs.getBoolean("UseNetwork", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "loc_net"); json.put("ts", ts); if (lm == null) { lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); } Location last_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (last_loc != null) { JSONObject loc_data = new JSONObject(); loc_data.put("ts", last_loc.getTime()); loc_data.put("lat", last_loc.getLatitude()); loc_data.put("lon", last_loc.getLongitude()); if (last_loc.hasAltitude()) loc_data.put("alt", roundValue(last_loc.getAltitude(), 4)); if (last_loc.hasAccuracy()) loc_data.put("acc", roundValue(last_loc.getAccuracy(), 4)); if (last_loc.hasSpeed()) loc_data.put("speed", roundValue(last_loc.getSpeed(), 4)); if (last_loc.hasBearing()) loc_data.put("bearing", roundValue(last_loc.getBearing(), 4)); json.put("loc_net", loc_data); } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } // http://developer.android.com/reference/android/net/wifi/WifiManager.html if (prefs.getBoolean("UseWIFI", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "wifi"); json.put("ts", ts); if (wm == null) { wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); } List<ScanResult> wifiScan = wm.getScanResults(); if (wifiScan != null) { JSONArray wifi_list = new JSONArray(); for (ScanResult wifi : wifiScan) { JSONObject wifi_data = new JSONObject(); wifi_data.put("BSSID", wifi.BSSID); wifi_data.put("SSID", wifi.SSID); wifi_data.put("freq", wifi.frequency); wifi_data.put("level", wifi.level); // wifi_data.put("cap", wifi.capabilities); // wifi_data.put("ts", wifi.timestamp); wifi_list.put(wifi_data); } json.put("wifi", wifi_list); } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } // TODO: cannot poll sensors. register receiver to cache sensor data // http://developer.android.com/guide/topics/sensors/sensors_overview.html // http://developer.android.com/reference/android/hardware/SensorManager.html if (prefs.getBoolean("UseSensors", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "sensors"); json.put("ts", ts); if (sm == null) { sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL); if (sensors != null) { JSONArray sensor_list = new JSONArray(); for (Sensor sensor : sensors) { JSONObject sensor_info = new JSONObject(); sensor_info.put("name", sensor.getName()); sensor_info.put("type", sensor.getType()); sensor_info.put("vendor", sensor.getVendor()); sensor_info.put("version", sensor.getVersion()); sensor_info.put("power", roundValue(sensor.getPower(), 4)); sensor_info.put("resolution", roundValue(sensor.getResolution(), 4)); sensor_info.put("range", roundValue(sensor.getMaximumRange(), 4)); sensor_list.put(sensor_info); } json.put("sensors", sensor_list); } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } // http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html // http://developer.android.com/reference/android/net/ConnectivityManager.html if (prefs.getBoolean("UseConn", false)) { try { JSONObject json = new JSONObject(); json.put("client", clientID); json.put("type", "conn"); json.put("ts", ts); if (cm == null) { cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); } // TODO: add active/all preferences below UseConn if (prefs.getBoolean("UseConnActive", true)) { NetworkInfo net = cm.getActiveNetworkInfo(); if (net != null) { JSONObject net_data = new JSONObject(); net_data.put("type", net.getTypeName()); net_data.put("subtype", net.getSubtypeName()); net_data.put("connected", net.isConnected()); net_data.put("available", net.isAvailable()); net_data.put("roaming", net.isRoaming()); net_data.put("failover", net.isFailover()); if (net.getReason() != null) net_data.put("reason", net.getReason()); if (net.getExtraInfo() != null) net_data.put("extra", net.getExtraInfo()); json.put("conn_active", net_data); } } if (prefs.getBoolean("UseConnAll", false)) { NetworkInfo[] nets = cm.getAllNetworkInfo(); if (nets != null) { JSONArray net_list = new JSONArray(); for (NetworkInfo net : nets) { JSONObject net_data = new JSONObject(); net_data.put("type", net.getTypeName()); net_data.put("subtype", net.getSubtypeName()); net_data.put("connected", net.isConnected()); net_data.put("available", net.isAvailable()); net_data.put("roaming", net.isRoaming()); net_data.put("failover", net.isFailover()); if (net.getReason() != null) net_data.put("reason", net.getReason()); if (net.getExtraInfo() != null) net_data.put("extra", net.getExtraInfo()); net_list.put(net_data); } json.put("conn_all", net_list); } } sendMessage(json); } catch (Exception e) { Log.e(tag, e.toString()); e.printStackTrace(); } } if (!PingServiceReceiver.completeWakefulIntent(intent)) { Log.w(tag, "completeWakefulIntent() failed. no active wake lock?"); } }
From source file:org.mixare.MixViewActivity.java
public void onAccuracyChanged(Sensor sensor, int accuracy) { if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE && getMixViewData().getCompassErrorDisplayed() == 0) { for (int i = 0; i < 2; i++) { markerRenderer.getContext().getNotificationManager() .addNotification("Compass data unreliable. Please recalibrate compass."); }// w w w . j ava 2s . c o m getMixViewData().setCompassErrorDisplayed(getMixViewData().getCompassErrorDisplayed() + 1); } }
From source file:io.authme.sdk.widget.LockPatternView.java
@Override public void onSensorChanged(SensorEvent event) { if (collectSensor) { Sensor sensor = event.sensor; if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) { mGravity = event.values;/*from ww w . jav a2 s. c o m*/ Accelerometer sensordata = new Accelerometer(mGravity[0], mGravity[1], mGravity[2], event.timestamp); accelList.add(sensordata); } if (sensor.getType() == Sensor.TYPE_GRAVITY) { mGravity = event.values; Accelerometer sensordata = new Accelerometer(mGravity[0], mGravity[1], mGravity[2], event.timestamp); accelList.add(sensordata); } if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { mGeomagnetic = event.values; Magnetic magnetic = new Magnetic(mGeomagnetic[0], mGeomagnetic[1], mGeomagnetic[2], event.timestamp); magnetics.add(magnetic); } if (sensor.getType() == Sensor.TYPE_GYROSCOPE) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; Gyroscope gyro = new Gyroscope(x, y, z, event.timestamp); gyrolist.add(gyro); } if (mGravity != null && mGeomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientation[] = new float[3]; SensorManager.getOrientation(R, orientation); Orientation orientationObj = new Orientation(orientation[0], orientation[1], orientation[2], event.timestamp); orientationArrayList.add(orientationObj); } } } }
From source file:com.dragon4.owo.ar_trace.ARCore.MixView.java
public void onAccuracyChanged(Sensor sensor, int accuracy) { // ? ? ? ? /* w w w. j a v a 2 s. com*/ if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE && compassErrorDisplayed == 0) { for (int i = 0; i < 2; i++) { // ?? . ? Toast.makeText(mixContext, "Compass data unreliable. Please recalibrate compass.", Toast.LENGTH_LONG).show(); } compassErrorDisplayed++; } }
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 w ww.j a v a2 s. c o m 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; } }