Permalink
Browse files

polished of threading and messaging

  • Loading branch information...
1 parent 3754586 commit b83aa4c818762a6842154c29ee938ff4d120efd5 Shawn-in-Tokyo committed Jan 12, 2011
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lejos.android"
+ android:screenOrientation="portrait"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
@@ -5,67 +5,76 @@
import java.io.IOException;
import lejos.android.LeJOSDroid.CONN_TYPE;
-import lejos.pc.comm.NXTCommAndroid;
import lejos.pc.comm.NXTConnector;
+import android.os.Handler;
import android.os.Looper;
import android.util.Log;
public class BTSend extends Thread {
- static final String TAG = "BTSend";
- private NXTConnector conn;
- private LeJOSDroid mActivity;
+ static final String TAG = "BTSend";
+ private NXTConnector conn;
+ Handler mUIMessageHandler;
+ DataOutputStream dos;
+ DataInputStream dis;
- public BTSend(LeJOSDroid mActivity) {
- super();
- this.mActivity = mActivity;
- }
+ public BTSend(Handler mUIMessageHandler) {
- @Override
- public void run() {
- Log.d(TAG, "BTSend run");
- Looper.prepare();
- conn = mActivity.connect(CONN_TYPE.LEJOS_PACKET);
- NXTCommAndroid nca = (NXTCommAndroid) conn.getNXTComm();
- DataOutputStream dos = conn.getDataOut();
- DataInputStream dis = conn.getDataIn();
- int x;
- for (int i = 0; i < 100; i++) {
+ super();
+ this.mUIMessageHandler = mUIMessageHandler;
+ }
+
+ public void closeConnection() {
+ try {
+ Log.d(TAG, "BTSend run loop finished and closing");
- try {
- dos.writeInt((i * 30000));
- dos.flush();
- nca.sendMessageToUIThread("sent:" + i * 30000);
- yield();
- x = dis.readInt();
- if (x>0) {
-
- nca.displayToastOnUIThread("got: "+x);
+ dis.close();
+ dos.close();
+ conn.getNXTComm().close();
+ } catch (Exception e) {
+ } finally {
+ dis = null;
+ dos = null;
+ conn = null;
}
- Log.d(TAG, "sent:" + i * 30000 + " got:" + x);
- nca.sendMessageToUIThread("sent:" + i * 30000 + " got:" + x);
- yield();
- } catch (IOException e) {
- Log.e(TAG, "Error ... ", e);
+ }
- }
+ @Override
+ public void run() {
+ Log.d(TAG, "BTSend run");
+ Looper.prepare();
+
+ conn = LeJOSDroid.connect(CONN_TYPE.LEJOS_PACKET);
- }
+ dos = conn.getDataOut();
+ dis = conn.getDataIn();
+ int x;
+ for (int i = 0; i < 100; i++) {
+
+ try {
+ dos.writeInt((i * 30000));
+ dos.flush();
+ LeJOSDroid.sendMessageToUIThread("sent:" + i * 30000);
+ yield();
+ x = dis.readInt();
+ if (x > 0) {
+
+ LeJOSDroid.displayToastOnUIThread("got: " + x);
+ }
+ Log.d(TAG, "sent:" + i * 30000 + " got:" + x);
+ LeJOSDroid.sendMessageToUIThread("sent:" + i * 30000 + " got:" + x);
+ yield();
+ } catch (IOException e) {
+ Log.e(TAG, "Error ... ", e);
+
+ }
+
+ }
- try {
-Log.d(TAG, "BTSend run loop finished and closing");
- dis.close();
- dos.close();
- conn.close();
- } catch (Exception e) {
- } finally {
- dis = null;
- dos = null;
- conn = null;
+ closeConnection();
+ Looper.loop();
+ Looper.myLooper().quit();
+ LeJOSDroid.sendMessageToUIThread("");//clear
+ LeJOSDroid.displayToastOnUIThread("BTSend finished it's run");
}
- Looper.loop();
- Looper.myLooper().quit();
- nca.sendMessageToUIThread("");//clear
- nca.displayToastOnUIThread("BTSend finished it's run");
- }
}
Oops, something went wrong.

0 comments on commit b83aa4c

Please sign in to comment.