Skip to content

Commit 9a40a1f

Browse files
author
Andreas Göransson
committed
Updated the methods do work more in line with the arduino client. Still havent made the connect+sub messages in one go, logic for parsing incomming messages is implemented - but untested!
1 parent e5632b6 commit 9a40a1f

File tree

12 files changed

+245
-28
lines changed

12 files changed

+245
-28
lines changed

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="gen"/>
55
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6-
<classpathentry kind="output" path="bin"/>
6+
<classpathentry kind="output" path="bin/classes"/>
77
</classpath>

bin/pubsub_android.jar

6.54 KB
Binary file not shown.

example/.classpath

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="src" path="gen"/>
55
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
66
<classpathentry kind="src" path="pubsub-android_src"/>
7-
<classpathentry kind="output" path="bin"/>
7+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
8+
<classpathentry kind="output" path="bin/classes"/>
89
</classpath>
3.87 KB
Loading
1.5 KB
Loading
2.15 KB
Loading
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# This file must be checked in Version Control Systems.
55
#
66
# To customize properties used by the Ant build system use,
7-
# "build.properties", and override values to adapt the script to your
7+
# "ant.properties", and override values to adapt the script to your
88
# project structure.
99

10+
android.library.reference.1=..
1011
# Project target.
1112
target=android-4
12-
android.library.reference.1=..

example/res/layout/main.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@
99
android:layout_height="wrap_content"
1010
android:text="@string/hello"
1111
/>
12+
13+
<Button
14+
android:id="@+id/button1"
15+
android:layout_width="fill_parent"
16+
android:layout_height="wrap_content"
17+
android:layout_margin="5dp"
18+
android:text="Publish a message" />
19+
1220
</LinearLayout>

example/src/pubsub/io/android/example/Pubsub_exampleActivity.java

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,81 @@
11
package pubsub.io.android.example;
22

3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
36
import pubsub.io.android.Pubsub;
47
import android.app.Activity;
58
import android.content.ComponentName;
69
import android.content.Context;
710
import android.content.Intent;
811
import android.content.ServiceConnection;
12+
import android.hardware.Sensor;
13+
import android.hardware.SensorEvent;
14+
import android.hardware.SensorEventListener;
15+
import android.hardware.SensorManager;
916
import android.os.Bundle;
1017
import android.os.Handler;
1118
import android.os.IBinder;
1219
import android.os.Message;
20+
import android.util.Log;
21+
import android.view.View;
22+
import android.view.View.OnClickListener;
23+
import android.widget.Button;
1324

1425
/**
1526
* Example pubsub.io - Android client.
1627
*
17-
* @author Andreas Göransson
28+
* @author Andreas G�ransson
1829
*
1930
*/
20-
public class Pubsub_exampleActivity extends Activity {
31+
public class Pubsub_exampleActivity extends Activity implements
32+
SensorEventListener {
33+
34+
protected static final String TAG = "Pubsub_exampleActivity";
35+
36+
protected static final int MY_HANDLER = 1241;
2137

2238
// Pubsub object
2339
private Pubsub mPubsub;
2440

41+
private SensorManager mSensorManager;
42+
private Sensor mAccelerometer;
43+
2544
/** Called when the activity is first created. */
2645
@Override
2746
public void onCreate(Bundle savedInstanceState) {
2847
super.onCreate(savedInstanceState);
2948
setContentView(R.layout.main);
49+
50+
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
51+
mAccelerometer = mSensorManager
52+
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
53+
54+
Button publish = (Button) findViewById(R.id.button1);
55+
publish.setOnClickListener(new OnClickListener() {
56+
57+
@Override
58+
public void onClick(View v) {
59+
60+
JSONObject doc = new JSONObject();
61+
try {
62+
doc.put("where", "android");
63+
} catch (JSONException e) {
64+
// TODO Auto-generated catch block
65+
e.printStackTrace();
66+
}
67+
mPubsub.publish(doc);
68+
}
69+
});
3070
}
3171

3272
@Override
3373
protected void onPause() {
3474
// Disconnect from the service
3575
unbindService(serviceConnection);
3676

77+
mSensorManager.unregisterListener(this);
78+
3779
super.onPause();
3880
}
3981

@@ -44,19 +86,32 @@ protected void onResume() {
4486
startService(intent);
4587
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
4688

89+
mSensorManager.registerListener(this, mAccelerometer,
90+
SensorManager.SENSOR_DELAY_NORMAL);
91+
4792
super.onResume();
4893
}
4994

5095
private ServiceConnection serviceConnection = new ServiceConnection() {
96+
5197
@Override
5298
public void onServiceConnected(ComponentName className, IBinder service) {
5399
mPubsub = ((Pubsub.LocalBinder) service).getService();
54100
mPubsub.setHandler(mHandler);
55-
mPubsub.connect();
101+
mPubsub.connect("10.3.3.11", "10547");
102+
103+
// In other clients the "connect" and the "sub" method are done at
104+
// the same time... this should probably be changed!
105+
mPubsub.sub("android");
106+
107+
// Subscribe to everything?
108+
JSONObject json_filter = new JSONObject();
109+
mPubsub.subscribe(json_filter, MY_HANDLER);
56110
}
57111

58112
@Override
59113
public void onServiceDisconnected(ComponentName className) {
114+
mPubsub.disconnect();
60115
mPubsub = null;
61116
mPubsub.setHandler(null);
62117
}
@@ -78,7 +133,34 @@ public void handleMessage(Message msg) {
78133
case Pubsub.ERROR:
79134
// TODO get error message...
80135
break;
136+
case MY_HANDLER:
137+
Log.i(TAG, "MY_HANDLER message: " + (String) msg.obj);
138+
break;
81139
}
82140
}
83141
};
142+
143+
@Override
144+
public void onAccuracyChanged(Sensor arg0, int arg1) {
145+
}
146+
147+
@Override
148+
public void onSensorChanged(SensorEvent event) {
149+
// We're just making sure that
150+
if (mPubsub != null && mPubsub.isSubscribed()) {
151+
float[] vals = event.values;
152+
153+
JSONObject doc = new JSONObject();
154+
try {
155+
doc.put("x", vals[0]);
156+
doc.put("y", vals[1]);
157+
doc.put("z", vals[2]);
158+
} catch (JSONException e) {
159+
// TODO Auto-generated catch block
160+
e.printStackTrace();
161+
}
162+
163+
// mPubsub.publish(doc);
164+
}
165+
}
84166
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# This file must be checked in Version Control Systems.
55
#
66
# To customize properties used by the Ant build system use,
7-
# "build.properties", and override values to adapt the script to your
7+
# "ant.properties", and override values to adapt the script to your
88
# project structure.
99

10+
android.library=true
1011
# Project target.
1112
target=android-4
12-
android.library=true

0 commit comments

Comments
 (0)