Android, sensors, SQLite DB
https://developers.google.com/maps/documentation/android-api/
https://events.google.com/io2016/
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#0
https://d.android.com/guide/topics/sensors/sensors_overview.html
https://www.element14.com/community/servlet/JiveServlet/showImage/2-99301-190768/GrovePi-Grove+for+the+Raspberry+Pi+-+Grove+Sensors+(5).JPG
private SensorManager mSensorManager;
// ...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null){
// Success! There's a pressure sensor.
} else {
// Failure! No pressure sensor.
}https://d.android.com/guide/topics/sensors/sensors_overview.html
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle state) {
// ...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent e) {
// access e.values, float array
}
}Hva gjør en sensor?
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
// ...
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}<uses-feature android:name="android.hardware.sensor.accelerometer"
android:required="true" />Hvis appen er helt avhengig av en sensor
List alle sensorer uansett, men med android:required="false"
(Pass opp for AndroidAnnotations)
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html
Begynnelsen på IoT!
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="developer.android.com"
android:pathPrefix="/index.html" />
</intent-filter>
Starter Activity-en med et intent
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>https://developer.android.com/guide/topics/connectivity/nfc/nfc.html
String action = getIntent().getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareUltralight ultralight = MifareUltralight.get(tag);
// ...ultralight.connect();
byte[] payload = ultralight.readPages(8);
payloadTextView.setText(new String(payload));
https://play.google.com/store/apps/details?id=com.wakdev.wdnfc&hl=en
https://www.sqlite.org/datatype3.html
https://www.sqlite.org/datatype3.html
public class DbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "measurements";
private static final int DB_VERSION = 1;
public DbHelper(final Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(final SQLiteDatabase db) {
String createTablesSQL =
"create table light ("
+ "id integer primary key autoincrement,"
+ "value real,"
+ "accuracy integer);";
db.execSQL(createTablesSQL);
}
@Override
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
// do nothing
}
}public class Light {
private int id;
private float light;
private int accuracy;
// java stuff
}public class LightDataSource {
private SQLiteDatabase db;
private DbHelper dbHelper;
public LightDataSource(Context context) {
dbHelper = new DbHelper(context);
}public void saveLight(Light light) {
ContentValues contentValues = new ContentValues();
// table -> value
contentValues.put("value", light.getValue());
contentValues.put("accuracy", light.getAccuracy());
long id = db.insert("light", null, contentValues);
}public List<Light> getLightMeasurements() {
List<Light> lights = new ArrayList<>();
String[] columns = { "id", "value", "accuracy" };
Cursor cursor = db.query("light", columns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
lights.add(lightFromCursor(cursor));
cursor.moveToNext();
}
return lights;
}
private Light lightFromCursor(final Cursor cursor) {
Light light = new Light();
light.setId(cursor.getInt(0));
light.setValue(cursor.getFloat(1));
light.setAccuracy(cursor.getInt(2));
return light;
}