diff options
author | Nathan Lasseter <nathan@4574.co.uk> | 2013-05-22 15:34:00 +0100 |
---|---|---|
committer | Nathan Lasseter <nathan@4574.co.uk> | 2013-05-22 15:34:00 +0100 |
commit | cc8ace92f17c8e5aef7d68ff316e5cd038cd36b5 (patch) | |
tree | caad5218a1da276909c71a95c45725651b08d984 /src |
Initial Commit
Diffstat (limited to 'src')
-rw-r--r-- | src/user4574/texttransport/AndroidPersistence.java | 50 | ||||
-rw-r--r-- | src/user4574/texttransport/SMSReceiver.java | 26 | ||||
-rw-r--r-- | src/user4574/texttransport/Start.java | 41 |
3 files changed, 117 insertions, 0 deletions
diff --git a/src/user4574/texttransport/AndroidPersistence.java b/src/user4574/texttransport/AndroidPersistence.java new file mode 100644 index 0000000..66f9dd3 --- /dev/null +++ b/src/user4574/texttransport/AndroidPersistence.java @@ -0,0 +1,50 @@ +package user4574.texttransport; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Vector; + +import org.eclipse.paho.client.mqttv3.MqttClientPersistence; +import org.eclipse.paho.client.mqttv3.MqttPersistable; +import org.eclipse.paho.client.mqttv3.MqttPersistenceException; + +public class AndroidPersistence implements MqttClientPersistence { + + private HashMap<String, MqttPersistable> store; + + public AndroidPersistence() { + store = new HashMap<String, MqttPersistable>(); + } + + public void clear() throws MqttPersistenceException { + store.clear(); + } + + public void close() throws MqttPersistenceException { + store = null; + } + + public boolean containsKey(String arg0) throws MqttPersistenceException { + return store.containsKey(arg0); + } + + public MqttPersistable get(String arg0) throws MqttPersistenceException { + return store.get(arg0); + } + + public Enumeration<String> keys() throws MqttPersistenceException { + return new Vector<String>(store.keySet()).elements(); + } + + public void open(String arg0, String arg1) throws MqttPersistenceException { + store = new HashMap<String, MqttPersistable>(); + } + + public void put(String arg0, MqttPersistable arg1) throws MqttPersistenceException { + store.put(arg0, arg1); + } + + public void remove(String arg0) throws MqttPersistenceException { + store.remove(arg0); + } +} diff --git a/src/user4574/texttransport/SMSReceiver.java b/src/user4574/texttransport/SMSReceiver.java new file mode 100644 index 0000000..c313b0f --- /dev/null +++ b/src/user4574/texttransport/SMSReceiver.java @@ -0,0 +1,26 @@ +package user4574.texttransport; + +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttTopic; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.telephony.SmsMessage; + +public class SMSReceiver extends BroadcastReceiver { + public void onReceive(Context context, Intent intent) { + Bundle pdusBundle = intent.getExtras(); + Object[] pdus = (Object[]) pdusBundle.get("pdus"); + SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0]); + try { + if (Start.client != null) { + MqttTopic t = Start.client.getTopic("/texttransport/" + message.getOriginatingAddress().substring(1) + "/received"); + t.publish(message.getMessageBody().getBytes(), 2, false); + } + } catch (MqttException e) { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/src/user4574/texttransport/Start.java b/src/user4574/texttransport/Start.java new file mode 100644 index 0000000..03ea032 --- /dev/null +++ b/src/user4574/texttransport/Start.java @@ -0,0 +1,41 @@ +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; + +public class Start extends Activity implements MqttCallback { + + public static MqttClient client; + private SmsManager sms = SmsManager.getDefault(); + + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + 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(); + } + + setContentView(R.layout.start); + } + + 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); + } + +}
\ No newline at end of file |