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

Initial commit.

parents
Branches
No related tags found
No related merge requests found
package de.tudresden.inf.st.sensorsharing;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.wearable.activity.WearableActivity;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataClient;
import com.google.android.gms.wearable.MessageClient;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class MainActivity extends WearableActivity implements SensorEventListener {
private static final String WEAR_SENSORS = "/wearable/sensors";
private static final String KEY_BRIGHTNESS = "brightness";
private SensorManager mSensorManager;
private Sensor mLight;
private float mLightValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setup sensor manager and light sensor
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager == null) {
TextView textView = findViewById(R.id.value_my_brightness);
textView.setText(R.string.error_sensormanager_null);
return;
}
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// Enables Always-on
setAmbientEnabled();
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
mLightValue = sensorEvent.values[0];
TextView textView = findViewById(R.id.value_my_brightness);
textView.setText(String.valueOf(mLightValue));
// TODO here we should send the new value to the handheld
sendCurrentValueToAllConnectedNodes();
}
private void sendCurrentValueToAllConnectedNodes() {
Task<List<Node>> nodesTask = Wearable.getNodeClient(this).getConnectedNodes();
final MessageClient messageClient = Wearable.getMessageClient(this);
final byte[] data = ByteBuffer.allocate(4).putFloat(mLightValue).array();
nodesTask.addOnSuccessListener(new OnSuccessListener<List<Node>>() {
@Override
public void onSuccess(List<Node> nodes) {
for (Node node : nodes) {
messageClient.sendMessage(node.getId(), WEAR_SENSORS, data);
}
}
});
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
// ignore accuracy changes
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_grey"
android:padding="@dimen/box_inset_layout_padding"
tools:context="de.tudresden.inf.st.sensorsharing.MainActivity"
tools:deviceIds="wear">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/inner_frame_layout_padding"
app:boxedEdges="all">
<TextView
android:id="@+id/value_my_brightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</android.support.wear.widget.BoxInsetLayout>
wear/src/main/res/mipmap-hdpi/ic_launcher.png

3.34 KiB

wear/src/main/res/mipmap-mdpi/ic_launcher.png

2.15 KiB

wear/src/main/res/mipmap-xhdpi/ic_launcher.png

4.73 KiB

wear/src/main/res/mipmap-xxhdpi/ic_launcher.png

7.54 KiB

<resources>
<string name="hello_world">Hello Round World!</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
Because the window insets on round devices are larger than 15dp, this padding only applies
to square screens.
-->
<dimen name="box_inset_layout_padding">0dp</dimen>
<!--
This padding applies to both square and round screens. The total padding between the buttons
and the window insets is box_inset_layout_padding (above variable) on square screens and
inner_frame_layout_padding (below variable) on round screens.
-->
<dimen name="inner_frame_layout_padding">5dp</dimen>
</resources>
<resources>
<string name="app_name">SensorSharing</string>
<!--
This string is used for square devices and overridden by hello_world in
values-round/strings.xml for round devices.
-->
<string name="hello_world">Hello Square World!</string>
<string name="text_my_brightness">My brightness</string>
<string name="error_sensormanager_null">Could not fetch sensor manager</string>
</resources>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment