Skip to content
This repository was archived by the owner on Jun 12, 2025. It is now read-only.

Commit b563a51

Browse files
committed
Send NFC data as Strings.
- Fix for issue #18
1 parent 7b23505 commit b563a51

File tree

11 files changed

+327
-287
lines changed

11 files changed

+327
-287
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ dependencies {
2828
compile "com.github.hoijui.JavaOSC:javaosc-core:javaosc-0.4"
2929
compile 'org.apmem.tools:layouts:1.9@aar'
3030
compile 'com.android.support:support-v4:23.1.1'
31-
compile 'com.github.SensorApps:Common:e4697fb701'
31+
compile 'com.github.SensorApps:Common:9494ac1590'
3232
}

app/src/main/java/org/sensors2/osc/dispatch/Bundling.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* Created by thomas on 31.03.15.
55
*/
66
public final class Bundling {
7-
public static final String DIMENSIONS = "dimensions";
8-
public static final String SENSOR_TYPE = "sensorType";
9-
public static final String OSC_PREFIX = "oscPrefix";
10-
public static final String NAME = "name";
11-
public static final String INDEX = "index";
12-
public static final String SENSOR_NAME = "sensorName";
13-
public static final String SENSOR_RANGE = "sensorRange";
14-
public static final String RESOLUTION = "resolution";
15-
public static final String VALUE= "value";
16-
public static final String OSC_PARAMETER = "oscParameter";
17-
}
7+
public static final String DIMENSIONS = "dimensions";
8+
public static final String SENSOR_TYPE = "sensorType";
9+
public static final String OSC_PREFIX = "oscPrefix";
10+
public static final String NAME = "name";
11+
public static final String INDEX = "index";
12+
public static final String SENSOR_NAME = "sensorName";
13+
public static final String SENSOR_RANGE = "sensorRange";
14+
public static final String RESOLUTION = "resolution";
15+
public static final String VALUES = "values";
16+
public static final String STRING_VALUE = "string_value";
17+
public static final String OSC_PARAMETER = "oscParameter";
18+
}

app/src/main/java/org/sensors2/osc/dispatch/OscCommunication.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,28 @@
66
* Created by thomas on 31.03.15.
77
*/
88
public class OscCommunication extends HandlerThread {
9-
private OscHandler handler;
9+
private OscHandler handler;
1010

11-
public OscCommunication(String name) {
12-
super(name);
13-
}
11+
public OscCommunication(String name) {
12+
super(name);
13+
}
1414

15-
public OscCommunication(String name, int priority) {
16-
super(name, priority);
17-
}
15+
public OscCommunication(String name, int priority) {
16+
super(name, priority);
17+
}
1818

19-
@Override
20-
public void run()
21-
{
22-
super.run();
23-
handler = null;
24-
}
19+
@Override
20+
public void run() {
21+
super.run();
22+
handler = null;
23+
}
2524

26-
@Override
27-
protected void onLooperPrepared()
28-
{
29-
handler = new OscHandler(this.getLooper());
30-
}
25+
@Override
26+
protected void onLooperPrepared() {
27+
handler = new OscHandler(this.getLooper());
28+
}
3129

32-
public OscHandler getOscHandler()
33-
{
34-
return handler;
35-
}
30+
public OscHandler getOscHandler() {
31+
return handler;
32+
}
3633
}

app/src/main/java/org/sensors2/osc/dispatch/OscDispatcher.java

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,92 @@
1616
* Created by thomas on 07.11.14.
1717
*/
1818
public class OscDispatcher implements DataDispatcher {
19-
private List<SensorConfiguration> sensorConfigurations = new ArrayList<SensorConfiguration>();
20-
private OscCommunication communication;
21-
private float[] gravity;
22-
private float[] geomagnetic;
23-
private SensorManager sensorManager;
19+
private List<SensorConfiguration> sensorConfigurations = new ArrayList<SensorConfiguration>();
20+
private OscCommunication communication;
21+
private float[] gravity;
22+
private float[] geomagnetic;
23+
private SensorManager sensorManager;
2424

25-
public OscDispatcher() {
26-
communication = new OscCommunication("OSC dispatcher thread", Thread.MIN_PRIORITY);
27-
communication.start();
28-
}
25+
public OscDispatcher() {
26+
communication = new OscCommunication("OSC dispatcher thread", Thread.MIN_PRIORITY);
27+
communication.start();
28+
}
2929

30-
public void addSensorConfiguration(SensorConfiguration sensorConfiguration) {
31-
this.sensorConfigurations.add(sensorConfiguration);
32-
}
30+
public void addSensorConfiguration(SensorConfiguration sensorConfiguration) {
31+
this.sensorConfigurations.add(sensorConfiguration);
32+
}
3333

34-
@Override
35-
public void dispatch(Measurement sensorData) {
36-
for (SensorConfiguration sensorConfiguration : this.sensorConfigurations) {
37-
if (sensorConfiguration.getSensorType() == sensorData.getSensorType()) {
38-
trySend(sensorConfiguration, sensorData.getValues());
39-
}
40-
if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION){
41-
// Fake orientation
42-
if (sensorData.getSensorType() != Sensor.TYPE_ACCELEROMETER && sensorData.getSensorType() != Sensor.TYPE_MAGNETIC_FIELD){
43-
continue;
44-
}
45-
if (sensorData.getSensorType() == Sensor.TYPE_ACCELEROMETER){
46-
this.gravity = sensorData.getValues();
47-
}
34+
@Override
35+
public void dispatch(Measurement sensorData) {
36+
for (SensorConfiguration sensorConfiguration : this.sensorConfigurations) {
37+
if (sensorConfiguration.getSensorType() == sensorData.getSensorType()) {
38+
if (sensorData.getValues() != null) {
39+
trySend(sensorConfiguration, sensorData.getValues());
40+
} else {
41+
trySend(sensorConfiguration, sensorData.getStringValue());
42+
}
43+
}
44+
if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION || sensorConfiguration.getSensorType() == Parameters.INCLINATION) {
45+
// Fake orientation
46+
if (sensorData.getSensorType() != Sensor.TYPE_ACCELEROMETER && sensorData.getSensorType() != Sensor.TYPE_MAGNETIC_FIELD) {
47+
continue;
48+
}
49+
if (sensorData.getSensorType() == Sensor.TYPE_ACCELEROMETER) {
50+
this.gravity = sensorData.getValues();
51+
}
4852

49-
if (sensorData.getSensorType() == Sensor.TYPE_MAGNETIC_FIELD){
50-
this.geomagnetic = sensorData.getValues();
51-
}
52-
if (this.gravity != null && this.geomagnetic != null){
53-
float rotation[] = new float[9];
54-
float inclination[] = new float[9];
53+
if (sensorData.getSensorType() == Sensor.TYPE_MAGNETIC_FIELD) {
54+
this.geomagnetic = sensorData.getValues();
55+
}
56+
if (this.gravity != null && this.geomagnetic != null) {
57+
float rotationMatrix[] = new float[9];
58+
float inclinationMatrix[] = new float[9];
5559

56-
boolean success = this.sensorManager.getRotationMatrix(rotation, inclination, this.gravity, this.geomagnetic);
57-
if (success) {
58-
float orientation[] = new float[3];
59-
this.sensorManager.getOrientation(rotation, orientation);
60-
this.trySend(sensorConfiguration, orientation);
61-
}
62-
}
63-
}
64-
}
65-
}
60+
boolean success = this.sensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, this.gravity, this.geomagnetic);
61+
if (success) {
62+
if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION) {
63+
float orientation[] = new float[3];
64+
this.sensorManager.getOrientation(rotationMatrix, orientation);
65+
this.trySend(sensorConfiguration, orientation);
66+
}
67+
if (sensorConfiguration.getSensorType() == Parameters.INCLINATION) {
68+
float inclination[] = new float[1];
69+
inclination[0] = this.sensorManager.getInclination(inclinationMatrix);
70+
this.trySend(sensorConfiguration, inclination);
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
6677

67-
private void trySend(SensorConfiguration sensorConfiguration, float[] values) {
68-
if (!sensorConfiguration.sendingNeeded(values)) {
69-
return;
70-
}
71-
Message message = new Message();
72-
Bundle data = new Bundle();
73-
data.putFloatArray(Bundling.VALUE, values);
74-
data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam());
75-
message.setData(data);
76-
OscHandler handler = communication.getOscHandler();
77-
handler.sendMessage(message);
78-
}
78+
private void trySend(SensorConfiguration sensorConfiguration, float[] values) {
79+
if (!sensorConfiguration.sendingNeeded(values)) {
80+
return;
81+
}
82+
Message message = new Message();
83+
Bundle data = new Bundle();
84+
data.putFloatArray(Bundling.VALUES, values);
85+
data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam());
86+
message.setData(data);
87+
OscHandler handler = communication.getOscHandler();
88+
handler.sendMessage(message);
89+
}
7990

80-
public void setSensorManager(SensorManager sensorManager) {
81-
this.sensorManager = sensorManager;
82-
}
91+
private void trySend(SensorConfiguration sensorConfiguration, String value) {
92+
if (!sensorConfiguration.sendingNeeded(new float[0])) {
93+
return;
94+
}
95+
Message message = new Message();
96+
Bundle data = new Bundle();
97+
data.putString(Bundling.STRING_VALUE, value);
98+
data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam());
99+
message.setData(data);
100+
OscHandler handler = communication.getOscHandler();
101+
handler.sendMessage(message);
102+
}
103+
104+
public void setSensorManager(SensorManager sensorManager) {
105+
this.sensorManager = sensorManager;
106+
}
83107
}

app/src/main/java/org/sensors2/osc/dispatch/OscHandler.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,35 @@
1616
*/
1717
public class OscHandler extends Handler {
1818

19-
public OscHandler(Looper myLooper) {
20-
super(myLooper);
21-
}
22-
@Override
23-
public void handleMessage(Message message) {
24-
Bundle data = message.getData();
25-
float[] value = data.getFloatArray(Bundling.VALUE);
26-
String oscParameter = data.getString(Bundling.OSC_PARAMETER);
27-
OscConfiguration configuration = OscConfiguration.getInstance();
19+
public OscHandler(Looper myLooper) {
20+
super(myLooper);
21+
}
2822

29-
if (configuration == null || configuration.getOscPort() == null) {
30-
return;
31-
}
32-
List<Object> changes = new ArrayList<Object>();
33-
for (float singleValue : value){
34-
changes.add(singleValue);
35-
}
36-
OSCMessage oscMessage = new OSCMessage("/" + oscParameter, changes);
37-
try {
38-
configuration.getOscPort().send(oscMessage);
39-
} catch (IOException e) {
40-
e.printStackTrace();
41-
}
42-
}
23+
@Override
24+
public void handleMessage(Message message) {
25+
Bundle data = message.getData();
26+
float[] values = data.getFloatArray(Bundling.VALUES);
27+
String stringValue = data.getString(Bundling.STRING_VALUE);
28+
String oscParameter = data.getString(Bundling.OSC_PARAMETER);
29+
OscConfiguration configuration = OscConfiguration.getInstance();
30+
31+
if (configuration == null || configuration.getOscPort() == null) {
32+
return;
33+
}
34+
List<Object> changes = new ArrayList<Object>();
35+
if (values != null) {
36+
for (float value : values) {
37+
changes.add(value);
38+
}
39+
}
40+
if (stringValue != null) {
41+
changes.add(stringValue);
42+
}
43+
OSCMessage oscMessage = new OSCMessage("/" + oscParameter, changes);
44+
try {
45+
configuration.getOscPort().send(oscMessage);
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
}
4350
}

app/src/main/java/org/sensors2/osc/dispatch/SensorConfiguration.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@
66
* Created by thomas on 11.11.14.
77
*/
88
public class SensorConfiguration {
9-
private boolean send;
10-
private int sensorType;
11-
private String oscParam;
12-
private float[] currentValues = new float[Parameters.MAX_DIMENSIONS];
13-
private boolean sendDuplicates;
14-
15-
public SensorConfiguration() {
16-
}
17-
18-
public boolean sendingNeeded(float[] values) {
19-
if (!this.send) {
20-
return false;
21-
}
22-
if (sendDuplicates) {
23-
return true;
24-
}
25-
boolean differenceDetected = false;
26-
for (int i = 0; i < values.length && i < Parameters.MAX_DIMENSIONS; i++) {
27-
if (Math.abs(values[i] - this.currentValues[i]) != 0){
28-
differenceDetected = true;
29-
}
30-
this.currentValues[i] = values[i];
31-
}
32-
return differenceDetected;
33-
}
34-
35-
public void setSend(boolean send) {
36-
this.send = send;
37-
}
38-
39-
public void setSendDuplicates(boolean sendDuplicates) {
40-
this.sendDuplicates = sendDuplicates;
41-
}
42-
43-
public int getSensorType() {
44-
return this.sensorType;
45-
}
46-
47-
public String getOscParam() {
48-
return this.oscParam;
49-
}
50-
51-
public void setOscParam(String oscParam) {
52-
this.oscParam = oscParam;
53-
}
54-
55-
public void setSensorType(int sensorType) {
56-
this.sensorType = sensorType;
57-
}
9+
private boolean send;
10+
private int sensorType;
11+
private String oscParam;
12+
private float[] currentValues = new float[Parameters.MAX_DIMENSIONS];
13+
private boolean sendDuplicates;
14+
15+
public SensorConfiguration() {
16+
}
17+
18+
public boolean sendingNeeded(float[] values) {
19+
if (!this.send) {
20+
return false;
21+
}
22+
if (sendDuplicates) {
23+
return true;
24+
}
25+
boolean differenceDetected = false;
26+
for (int i = 0; i < values.length && i < Parameters.MAX_DIMENSIONS; i++) {
27+
if (Math.abs(values[i] - this.currentValues[i]) != 0) {
28+
differenceDetected = true;
29+
}
30+
this.currentValues[i] = values[i];
31+
}
32+
return differenceDetected;
33+
}
34+
35+
public void setSend(boolean send) {
36+
this.send = send;
37+
}
38+
39+
public void setSendDuplicates(boolean sendDuplicates) {
40+
this.sendDuplicates = sendDuplicates;
41+
}
42+
43+
public int getSensorType() {
44+
return this.sensorType;
45+
}
46+
47+
public String getOscParam() {
48+
return this.oscParam;
49+
}
50+
51+
public void setOscParam(String oscParam) {
52+
this.oscParam = oscParam;
53+
}
54+
55+
public void setSensorType(int sensorType) {
56+
this.sensorType = sensorType;
57+
}
5858
}

0 commit comments

Comments
 (0)