Skip to content
Snippets Groups Projects
Commit d46b0514 authored by René Schöne's avatar René Schöne
Browse files

Use model name to identify phone and wearable.

parent aa206b0f
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
package de.tudresden.inf.st.sensorsharing.common;
import java.nio.charset.Charset;
public class Constants {
public static final String BASE_KEY = "/wearable/sensors/";
public static final int BASE_KEY_LENGTH = BASE_KEY.length();
public static final String SUB_KEY_BRIGHTNESS = "brightness";
public static final String KEY_BRIGHTNESS = BASE_KEY + SUB_KEY_BRIGHTNESS;
public static final String SUB_KEY_LINEAR_ACCELERATION = "acceleration";
public static final String KEY_LINEAR_ACCELERATION = BASE_KEY + SUB_KEY_LINEAR_ACCELERATION;
public static final String SUB_KEY_ROTATION_VECTOR = "rotation";
public static final String KEY_ROTATION_VECTOR = BASE_KEY + SUB_KEY_ROTATION_VECTOR;
public static final String SUB_KEY_NAME = "name";
public static final String KEY_NAME = BASE_KEY + SUB_KEY_NAME;
public static final Charset MQTT_CHARSET = Charset.forName("UTF-8");
}
......@@ -5,6 +5,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
......@@ -37,8 +38,10 @@ import java.util.Locale;
import static de.tudresden.inf.st.sensorsharing.common.Constants.BASE_KEY;
import static de.tudresden.inf.st.sensorsharing.common.Constants.BASE_KEY_LENGTH;
import static de.tudresden.inf.st.sensorsharing.common.Constants.MQTT_CHARSET;
import static de.tudresden.inf.st.sensorsharing.common.Constants.SUB_KEY_BRIGHTNESS;
import static de.tudresden.inf.st.sensorsharing.common.Constants.SUB_KEY_LINEAR_ACCELERATION;
import static de.tudresden.inf.st.sensorsharing.common.Constants.SUB_KEY_NAME;
import static de.tudresden.inf.st.sensorsharing.common.Constants.SUB_KEY_ROTATION_VECTOR;
public class MainActivity extends AppCompatActivity implements
......@@ -52,7 +55,6 @@ public class MainActivity extends AppCompatActivity implements
private static final String MQTT_TOPIC_POLAR_ROTATION = MQTT_TOPIC_POLAR_BASE + "rotation";
private static final String MQTT_TOPIC_SAMSUNG = "sensors/samsung/brightness";
private static final int MQTT_DEFAULT_QOS = 0;
private static final Charset MQTT_CHARSET = Charset.forName("UTF-8");
/** Delay in milliseconds after which the wearable is assumed to be offline */
public static final int DELAY_WEARABLE_ASSUMED_OFFLINE = 30000;
......@@ -72,6 +74,8 @@ public class MainActivity extends AppCompatActivity implements
System.setProperty("log.tag." + TAG, "INFO");
setName();
// setup sensor manager and light sensor
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager == null) {
......@@ -99,6 +103,11 @@ public class MainActivity extends AppCompatActivity implements
handler.post(runnable);
}
private void setName() {
TextView ownName = findViewById(R.id.value_own_name);
ownName.setText(Build.MODEL);
}
public void connectMqtt(View view) {
// disconnect if there is a connected client
if (mqttAndroidClient != null && mqttAndroidClient.isConnected()) {
......@@ -203,7 +212,7 @@ public class MainActivity extends AppCompatActivity implements
final Date contactTime = new Date();
lastMessageFromWearable = contactTime.getTime();
final String updatedValue;
String topic;
String topic = null;
final TextView textViewToUpdate;
switch (path.substring(BASE_KEY_LENGTH)) {
......@@ -229,8 +238,13 @@ public class MainActivity extends AppCompatActivity implements
textViewToUpdate = findViewById(R.id.value_wear_rotation);
topic = MQTT_TOPIC_POLAR_ROTATION;
break;
case SUB_KEY_NAME:
updatedValue = new String(messageEvent.getData(), MQTT_CHARSET);
textViewToUpdate = findViewById(R.id.value_wear_name);
break;
default:
throw new IllegalArgumentException("Unknown path: " + path);
Log.w(TAG, "Unknown path: " + path);
return;
}
runOnUiThread(new Runnable() {
@Override
......@@ -242,9 +256,11 @@ public class MainActivity extends AppCompatActivity implements
}
});
if (topic != null) {
sendUpdateOfWearable(topic, updatedValue);
}
}
}
private String formatByteFloatArray(ByteBuffer buffer) {
StringBuilder sb = new StringBuilder();
......
......@@ -6,6 +6,48 @@
android:layout_height="match_parent"
tools:context="de.tudresden.inf.st.sensorsharing.MainActivity">
<TextView
android:id="@+id/label_wear_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/text_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
<TextView
android:id="@+id/value_wear_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/label_wear_name"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
<TextView
android:id="@+id/label_own_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/separator_own" />
<TextView
android:id="@+id/value_own_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/label_own_name"
app:layout_constraintTop_toBottomOf="@+id/separator_own" />
<TextView
android:id="@+id/separator_own"
style="?android:attr/listSeparatorTextViewStyle"
......@@ -65,7 +107,7 @@
android:text="@string/text_own_brightness"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/separator_own" />
app:layout_constraintTop_toBottomOf="@+id/label_own_name" />
<TextView
android:id="@+id/label_wear_brightness"
......@@ -75,7 +117,7 @@
android:layout_marginTop="8dp"
android:text="@string/text_wear_brightness"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
app:layout_constraintTop_toBottomOf="@+id/label_wear_name" />
<TextView
android:id="@+id/label_wear_acceleration"
......@@ -105,7 +147,7 @@
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/label_own_brightness"
app:layout_constraintTop_toBottomOf="@+id/separator_own" />
app:layout_constraintTop_toBottomOf="@+id/label_own_name" />
<TextView
android:id="@+id/value_wear_brightness"
......@@ -115,7 +157,7 @@
android:layout_marginTop="8dp"
android:text="@android:string/unknownName"
app:layout_constraintStart_toEndOf="@+id/label_wear_brightness"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
app:layout_constraintTop_toBottomOf="@+id/label_wear_name" />
<TextView
android:id="@+id/value_wear_acceleration"
......
<resources>
<string name="app_name">SensorSharing</string>
<string name="text_name">Name</string>
<string name="text_own_brightness">Own Brightness</string>
<string name="text_wear_brightness">Wear Brightness</string>
<string name="text_wear_acceleration">Wear Acceleration</string>
......
......@@ -5,6 +5,7 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;
......@@ -24,7 +25,9 @@ import java.util.Locale;
import static de.tudresden.inf.st.sensorsharing.common.Constants.KEY_BRIGHTNESS;
import static de.tudresden.inf.st.sensorsharing.common.Constants.KEY_LINEAR_ACCELERATION;
import static de.tudresden.inf.st.sensorsharing.common.Constants.KEY_NAME;
import static de.tudresden.inf.st.sensorsharing.common.Constants.KEY_ROTATION_VECTOR;
import static de.tudresden.inf.st.sensorsharing.common.Constants.MQTT_CHARSET;
public class MainActivity extends WearableActivity implements SensorEventListener {
......@@ -37,6 +40,7 @@ public class MainActivity extends WearableActivity implements SensorEventListene
private float[] mLinearAccelerationValues;
private Sensor mRotationVector;
private float[] mRotationVectorValues;
private boolean sendName = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -132,6 +136,11 @@ public class MainActivity extends WearableActivity implements SensorEventListene
final byte[] accelerationData = floatArray2ByteArray(mLinearAccelerationValues);
final byte[] rotationData = floatArray2ByteArray(mRotationVectorValues);
for (Node node : nodes) {
if (sendName) {
final byte[] nameData = Build.MODEL.getBytes(MQTT_CHARSET);
messageClient.sendMessage(node.getId(), KEY_NAME, nameData);
sendName = false;
}
messageClient.sendMessage(node.getId(), KEY_BRIGHTNESS, brightnessData);
messageClient.sendMessage(node.getId(), KEY_LINEAR_ACCELERATION, accelerationData);
messageClient.sendMessage(node.getId(), KEY_ROTATION_VECTOR, rotationData);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment