SMS In Morse

Alles over spraak.
Bericht
Auteur
on3phc
Berichten: 10
Lid geworden op: 04 okt 2005, 14:10
Locatie: Mechelen

SMS In Morse

#1 Bericht door on3phc »

/* Adaptation to vibrate an incoming sms to a port in morse
Needed : MIDP version 2
Instructions :
- learn a bit about the Java Micro Edition (e.g. by the tutor)
- replace the class SMSReceive in the the SMSDemo by this one
- in WTK, build and run and prepare to Receive a message
- send with Utilities WMA an email to port 50000
- when receiving the backlights of the buttons should flash in morse
- for use and vibrating on a real gsm
- use the (renewed) SMS Demo on the sending gsm
- replace flasBlacklight by vibrate in the code
- you can change the duration of the vibrations and the silences in the code

This is foodware.
Use this example as you like.
Wouldn't a gsm metronome be great. Or combined with text to speech
for the blind. Or with semaphores and so on.
Maybe you can send me the improvements, so that I can learn from it.
I don't have a MIDP2 gsm, so this program is not really tested. In fact, it
has not been tested at all.
I will have a look at the Microsoft Mobile OS to see if they have the same
restrictions as the Java phones. Because I would like to let all smses vibrate
and be kept in the Inbox.
If you cannot deploy it on your mobile, I am willing to do it for vegatarian
food and alcoholic drinks.

PhilippeNadaZeroSpamCasteleyn@hotmail.com
2800 Flanders
*/

/*
* @(#)SMSReceive.java 1.9 03/06/22
*
* Copyright (c) 1999-2003 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/

package example.sms;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

/**
* An example MIDlet displays text from an SMS MessageConnection
*/
public class SMSReceive
extends MIDlet
implements CommandListener, Runnable, MessageListener {

/** user interface command for indicating Exit request. */
Command exitCommand = new Command("Exit", Command.EXIT, 2);
/** user interface command for indicating Reply request */
Command replyCommand = new Command("Reply", Command.OK, 1);
/** user interface text box for the contents of the fetched URL. */
Alert content;
/** current display. */
Display display;
/** instance of a thread for asynchronous networking and user interface. */
Thread thread;
/** Connections detected at start up. */
String[] connections;
/** Flag to signal end of processing. */
boolean done;
/** The port on which we listen for SMS messages */
String smsPort;
/** SMS message connection for inbound text messages. */
MessageConnection smsconn;
/** Current message read from the network. */
Message msg;
/** Address of the message's sender */
String senderAddress;
/** Alert that is displayed when replying */
Alert sendingMessageAlert;
/** Prompts for and sends the text reply */
SMSSender sender;
/** The screen to display when we return from being paused */
Displayable resumeScreen;


int kort = 500;
int lang = 1000;
int stilKort = 1000;
int stilLang = 2000;

/**
* Initialize the MIDlet with the current display object and
* graphical components.
*/
public SMSReceive() {
smsPort = getAppProperty("SMS-Port");

display = Display.getDisplay(this);
content = new Alert("SMS Receive");
content.setTimeout(Alert.FOREVER);
content.addCommand(exitCommand);
content.setCommandListener(this);
content.setString("Receiving...can vibrate" + display.vibrate(1));
content.setString("Receiving...can flashBacklight"
+ display.flashBacklight(1));
sendingMessageAlert = new Alert("SMS", null, null, AlertType.INFO);
sendingMessageAlert.setTimeout(5000);
sendingMessageAlert.setCommandListener(this);

sender = new SMSSender(smsPort, display, content, sendingMessageAlert);

resumeScreen = content;
}

/**
* Start creates the thread to do the MessageConnection receive
* text.
* It should return immediately to keep the dispatcher
* from hanging.
*/
public void startApp() {
/** SMS connection to be read. */
String smsConnection = "sms://:" + smsPort;
/** Open the message connection. */
if (smsconn == null) {
try {
smsconn = (MessageConnection) Connector.open(smsConnection);
smsconn.setMessageListener(this);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}

/** Initialize the text if we were started manually. */
connections = PushRegistry.listConnections(true);
if (connections == null || connections.length == 0) {
content.setString("Waiting for SMS on port " + smsPort + "...");
}
done = false;
thread = new Thread(this);
thread.start();

display.setCurrent(resumeScreen);
}

/**
* Notification that a message arrived.
* @param conn the connection with messages available
*/
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
done = false;
thread = new Thread(this);
thread.start();
}
}

/** Message reading thread. */
public void run() {


int kort = 500;
int lang = 1000;
int stilKort = 1000;
int stilLang = 2000;
String receivedMessage = null;
/** Check for sms connection. */
try {
msg = smsconn.receive();
if (msg != null) {
senderAddress = msg.getAddress();
content.setTitle("From: " + senderAddress);
if (msg instanceof TextMessage) {
receivedMessage = ( (TextMessage) msg).getPayloadText();
content.setString(receivedMessage);
for (int i=0 ; i < receivedMessage.length() ; i++) {
sein(receivedMessage.substring(i,i+1)) ;
karakterPauze() ;
}

}
else {
StringBuffer buf = new StringBuffer();
byte[] data = ( (BinaryMessage) msg).getPayloadData();
for (int i = 0; i < data.length; i++) {
int intData = (int) data & 0xFF;
if (intData < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intData));
buf.append(' ');
}
content.setString(buf.toString() + "cast");
}
content.addCommand(replyCommand);
display.setCurrent(content);

}
}
catch (IOException e) {

}
}

/**
* Pause signals the thread to stop by clearing the thread field.
* If stopped before done with the iterations it will
* be restarted from scratch later.
*/
public void pauseApp() {
done = true;
thread = null;
resumeScreen = display.getCurrent();
}

/**
* Destroy must cleanup everything. The thread is signaled
* to stop and no result is produced.
* @param unconditional true if a forced shutdown was requested
*/
public void destroyApp(boolean unconditional) {
done = true;
thread = null;
if (smsconn != null) {
try {
smsconn.close();
}
catch (IOException e) {
// Ignore any errors on shutdown
}
}
}

/**
* Respond to commands, including exit
* @param c user interface command requested
* @param s screen object initiating the request
*/
public void commandAction(Command c, Displayable s) {
try {
if (c == exitCommand || c == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
}
else if (c == replyCommand) {
reply();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* Allow the user to reply to the received message
*/
private void reply() {
// remove the leading "sms://" for diplaying the destination address
String address = senderAddress.substring(6);
String statusMessage = "Sending message to " + address + "...";
sendingMessageAlert.setString(statusMessage);
sender.promptAndSend(senderAddress);
}
// maybe an array would be better for extensibility
private void sein(String karakter) {
if (karakter.toUpperCase().equals("A")) {
di() ; da() ; ; } //einde if
else if (karakter.toUpperCase().equals("B")) {
da() ; di(); di(); di(); ; }
else if (karakter.toUpperCase().equals("C")) {
da() ; di(); da(); di(); ; }
else if (karakter.toUpperCase().equals("D")) {
da() ; di(); di(); ; }
else if (karakter.toUpperCase().equals("E")) {
di() ; ; }
else if (karakter.toUpperCase().equals("F")) {
di() ; di(); da(); di(); ; }
else if (karakter.toUpperCase().equals("G")) {
da() ; da(); di(); ; }
else if (karakter.toUpperCase().equals("H")) {
di() ; di(); di(); di(); ; }
else if (karakter.toUpperCase().equals("I")) {
di() ; di(); ; }
else if (karakter.toUpperCase().equals("J")) {
di() ; da(); da(); da(); ; }
else if (karakter.toUpperCase().equals("K")) {
da() ; di(); da(); ; }
else if (karakter.toUpperCase().equals("L")) {
di() ; da(); di(); di(); ; }
else if (karakter.toUpperCase().equals("M")) {
da() ; da(); ; }
else if (karakter.toUpperCase().equals("N")) {
da() ; di(); ; }
else if (karakter.toUpperCase().equals("O")) {
da() ; da(); da(); ; }
else if (karakter.toUpperCase().equals("P")) {
di() ; da(); da(); di(); ; }
else if (karakter.toUpperCase().equals("Q")) {
da() ; da(); di(); da(); ; }
else if (karakter.toUpperCase().equals("R")) {
di() ; da(); di(); ; }
else if (karakter.toUpperCase().equals("S")) {
di() ; di(); di(); ; }
else if (karakter.toUpperCase().equals("T")) {
da() ; ; }
else if (karakter.toUpperCase().equals("U")) {
di(); di(); da(); ; }
else if (karakter.toUpperCase().equals("V")) {
di() ; di(); di(); da(); ; } //London calling
else if (karakter.toUpperCase().equals("W")) {
di() ; da(); da(); ; }
else if (karakter.toUpperCase().equals("X")) {
da() ; di(); di(); da(); ; }
else if (karakter.toUpperCase().equals("Y")) {
da() ; di(); da(); da(); ; }
else if (karakter.toUpperCase().equals("Z")) {
da() ; da(); di(); di(); ; }
// the numbers

else if (karakter.equals("0")) {
da() ; da(); da(); da(); da(); ; }
else if (karakter.equals("1")) {
di() ; da(); da(); da(); da(); ; }
else if (karakter.equals("2")) {
di() ; di(); da(); da(); da(); ; }
else if (karakter.equals("3")) {
di() ; di(); di(); da(); da(); ; }
else if (karakter.equals("4")) {
di() ; di(); di(); di(); da(); ; }
else if (karakter.equals("5")) {
di() ; di(); di(); di(); di(); ; }
else if (karakter.equals("6")) {
da() ; di(); di(); di(); di(); ; }
else if (karakter.equals("7")) {
da() ; da(); di(); di(); di(); ; }
else if (karakter.equals("8")) {
da() ; da(); da(); di(); di(); ; }
else if (karakter.equals("9")) {
da() ; da(); da(); da(); di(); ; }


// here further the special characters such as (; you want to know
/* else if (karakter.toUpperCase().equals("(")) {
woordpauze() ; }
*/
// unknow character gives a longer pause
else {
karakterPauze() ; }

} //einde sein


private void di() {
display.flashBacklight(kort);
try {
Thread.sleep(stilKort);
}
catch (InterruptedException e) {}
}

private void da() {
display.flashBacklight(lang);
try {
Thread.sleep(stilKort);
}
catch (InterruptedException e) {}

}

private void karakterPauze() {
try {
Thread.sleep(stilLang);
}
catch (InterruptedException e) {}

}

}

Plaats reactie