diff options
author | Nathan Lasseter <nathan@4574.co.uk> | 2013-05-22 17:23:10 +0100 |
---|---|---|
committer | Nathan Lasseter <nathan@4574.co.uk> | 2013-05-22 17:23:10 +0100 |
commit | 5d14f1cc96a7ac749cdcba72a14d5f2825df37a9 (patch) | |
tree | 8127f94cf3d0bc79f937f64ba991f1f50f974049 | |
parent | cc8ace92f17c8e5aef7d68ff316e5cd038cd36b5 (diff) |
Moved MqttCallback to new class. Added text boxes & connect/disconnect button.
-rw-r--r-- | res/layout/start.xml | 50 | ||||
-rw-r--r-- | res/menu/start.xml | 6 | ||||
-rw-r--r-- | res/values/strings.xml | 8 | ||||
-rw-r--r-- | src/user4574/texttransport/MqttMsgCallback.java | 25 | ||||
-rw-r--r-- | src/user4574/texttransport/Start.java | 81 |
5 files changed, 141 insertions, 29 deletions
diff --git a/res/layout/start.xml b/res/layout/start.xml index fc6335d..d0f7d30 100644 --- a/res/layout/start.xml +++ b/res/layout/start.xml @@ -8,9 +8,55 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Start" > - <TextView + <RadioGroup + android:id="@+id/protocol" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/hello_world" /> + android:layout_alignParentLeft="true" + android:layout_alignParentTop="true" + android:orientation="horizontal" > + + <RadioButton + android:id="@+id/tcp" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:checked="true" + android:text="@string/tcp" /> + + <RadioButton + android:id="@+id/ssl" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/ssl" /> + + </RadioGroup> + + <EditText + android:id="@+id/uri" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_alignParentLeft="true" + android:layout_below="@+id/protocol" + android:ems="10" + android:hint="@string/uri" + android:inputType="textUri" /> + + <EditText + android:id="@+id/port" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentLeft="true" + android:layout_below="@+id/uri" + android:ems="10" + android:hint="@string/port" + android:inputType="number" /> + + <Button + android:id="@+id/connectbutton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentLeft="true" + android:layout_below="@+id/port" + android:text="@string/connect" /> </RelativeLayout>
\ No newline at end of file diff --git a/res/menu/start.xml b/res/menu/start.xml index e6dd2f2..9a14074 100644 --- a/res/menu/start.xml +++ b/res/menu/start.xml @@ -1,8 +1,2 @@ <menu xmlns:android="http://schemas.android.com/apk/res/android" > - - <item - android:id="@+id/action_settings" - android:orderInCategory="100" - android:title="@string/action_settings"/> - </menu>
\ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 08091d3..2c832b0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2,7 +2,11 @@ <resources> <string name="app_name">Text Transport</string> - <string name="action_settings">Settings</string> - <string name="hello_world">Running!</string> + <string name="connect">Connect</string> + <string name="disconnect">Disconnect</string> + <string name="uri">Server URI</string> + <string name="tcp">TCP</string> + <string name="ssl">SSL</string> + <string name="port">Port</string> </resources>
\ No newline at end of file diff --git a/src/user4574/texttransport/MqttMsgCallback.java b/src/user4574/texttransport/MqttMsgCallback.java new file mode 100644 index 0000000..3755260 --- /dev/null +++ b/src/user4574/texttransport/MqttMsgCallback.java @@ -0,0 +1,25 @@ +package user4574.texttransport; + +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.MqttTopic; + +import android.telephony.SmsManager; + +public class MqttMsgCallback implements MqttCallback { + + SmsManager sms; + + public MqttMsgCallback (SmsManager sms) { + this.sms = sms; + } + + public void connectionLost(Throwable exception) {} + public void deliveryComplete(MqttDeliveryToken token) {} + public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception { + String[] topicarr = topic.getName().split("/"); + sms.sendTextMessage(topicarr[2], null, message.toString(), null, null); + } + +} diff --git a/src/user4574/texttransport/Start.java b/src/user4574/texttransport/Start.java index 03ea032..127a4e8 100644 --- a/src/user4574/texttransport/Start.java +++ b/src/user4574/texttransport/Start.java @@ -1,41 +1,84 @@ package user4574.texttransport; -import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; -import org.eclipse.paho.client.mqttv3.MqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttException; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.eclipse.paho.client.mqttv3.MqttTopic; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.EditText; +import android.widget.RadioGroup; -public class Start extends Activity implements MqttCallback { +public class Start extends Activity { public static MqttClient client; private SmsManager sms = SmsManager.getDefault(); + private Button conn; + private EditText ent; + private EditText port; + private RadioGroup rg; + private String uri = ""; + protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.start); - try { - client = new MqttClient("tcp://192.168.0.222:1883", MqttClient.generateClientId(), new AndroidPersistence()); - client.setCallback(this); - client.connect(); - client.subscribe("/texttransport/+/send"); - } catch (MqttException e) { - e.printStackTrace(); - } + conn = (Button) findViewById(R.id.connectbutton); + conn.setEnabled(false); + conn.setOnClickListener(new ConnectListener()); - setContentView(R.layout.start); + ent = (EditText) findViewById(R.id.uri); + ent.addTextChangedListener(new URIWatcher()); + + port = (EditText) findViewById(R.id.port); + + rg = (RadioGroup) findViewById(R.id.protocol); } - public void connectionLost(Throwable exception) {} - public void deliveryComplete(MqttDeliveryToken token) {} - public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception { - String[] topicarr = topic.getName().split("/"); - sms.sendTextMessage(topicarr[2], null, message.toString(), null, null); + class ConnectListener implements OnClickListener { + public void onClick(View arg0) { + if (client != null && client.isConnected()) { + try { + client.disconnect(); + ent.setEnabled(true); + conn.setText(R.string.connect); + } catch (MqttException e) { + e.printStackTrace(); + } + } else if (!uri.equals("")) { + String server = (rg.getCheckedRadioButtonId() == R.id.ssl) ? "ssl" : "tcp"; + server += "://" + uri; + if(!port.getText().toString().equals("")) + server += ":" + port.getText().toString(); + try { + client = new MqttClient(server, MqttClient.generateClientId(), new AndroidPersistence()); + client.setCallback(new MqttMsgCallback(sms)); + client.connect(); + client.subscribe("/texttransport/+/send"); + ent.setEnabled(false); + conn.setText(R.string.disconnect); + } catch (MqttException e) { + e.printStackTrace(); + } + } + } } + class URIWatcher implements TextWatcher { + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + public void onTextChanged(CharSequence s, int start, int before, int count) {} + public void afterTextChanged(Editable s) { + uri = s.toString(); + if (uri.equals("")) + conn.setEnabled(false); + else + conn.setEnabled(true); + } + } }
\ No newline at end of file |