diff options
Diffstat (limited to 'src/user4574')
-rw-r--r-- | src/user4574/texttransport/MqttMsgCallback.java | 25 | ||||
-rw-r--r-- | src/user4574/texttransport/Start.java | 81 |
2 files changed, 87 insertions, 19 deletions
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 |