我需要帮助才能使用蓝牙连接将 J2me midlet 程序连接到桌面上的 java 应用程序。
如何配置我的 midlet 应用程序以使用蓝牙连接到桌面应用程序?
设备支持 JSR 82 J2ME 蓝牙 API。这是我的 MIDLet 代码:
package client;
//imports
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class client extends MIDlet implements CommandListener {
//Variable declaration
String pass,user;
protected Display display;
protected Displayable displayable;
StreamConnection stream = null;
InputStream in = null;
OutputStream out = null;
Thread thread = null;
Form Login = null;
protected Form Message = null;
Form Transaction = null;
Form Broadcast = null;
Form Help = null;
TextField txtMessage = null;
TextField txtPhone = null;
TextField txtacc1 = null;
TextField txtacc2 = null;
TextField txtsk = null;
TextField txtamt = null;
TextField txtbc = null;
TextField txtuser = null;
TextField txtpw = null;
Command TransOk = null;
Command Back = null;
Command Ok = null;
Command SmsOk=null;
Command SmsDel = null;
Command BrdCstOk = null;
Command cmdsel = null;
Command cmdsel1 = null;
Ticker ticker = new Ticker("WELCOME CUSTOMER!");
byte message[] = new byte[200];
private String login[]={"Login"};
private String options[] = {"Inbox", "Write Message", "Transaction","Broadcast", "Help"};
//private String opt[] = { "" };
private List list = null, inbox = null, list1 = null;
public void client(){
}
public void startApp() {
display = Display.getDisplay(this);
//First Screen
list = new List("Secure SMS", List.IMPLICIT, login, null);
list.setTicker(ticker);
cmdsel = new Command("Select", Command.OK, 1);
list.addCommand(cmdsel);
list.setCommandListener(this);
display.setCurrent(list);
//Login Form
Login = new Form("login");
txtuser = new TextField("UserId", "", 10, TextField.NUMERIC);
txtpw = new TextField("Password", "", 10, TextField.PASSWORD);
Ok = new Command("Enter", Command.OK, 1);
Back = new Command("Back", Command.OK, 2);
Login.append(txtuser);
Login.append(txtpw);
Login.addCommand(Ok);
Login.setCommandListener(this);
//Options
list1 = new List("Secure SMS", List.IMPLICIT, options, null);
list1.setTicker(ticker);
cmdsel1 = new Command("Select", Command.OK, 1);
list1.addCommand(cmdsel1);
list1.setCommandListener(this);
//To Display Inbox Message
inbox = new List("Received SMS", List.IMPLICIT);
inbox.setTicker(ticker);
inbox.addCommand(Back);
SmsDel = new Command("Del", Command.OK, 2);
inbox.addCommand(SmsDel);
inbox.setCommandListener(this);
//Write Message Form
Message = new Form("Write Message");
txtMessage = new TextField("Message", "", 215, TextField.ANY);
txtPhone = new TextField("Mobile", "", 10, TextField.NUMERIC);
SmsOk = new Command("ok", Command.OK, 1);
Message.setTicker(ticker);
Message.append(txtMessage);
Message.append(txtPhone);
Message.addCommand(SmsOk);
Message.addCommand(Back);
Message.setCommandListener(this);
// TRANSACTION
Transaction = new Form("Transaction");
txtacc1 = new TextField("Transfer from A/C", "", 10, TextField.NUMERIC);
txtacc2 = new TextField("Transfer to A/C", "", 10, TextField.NUMERIC);
txtsk = new TextField("Trans pw", "", 10, TextField.PASSWORD);
txtamt = new TextField("Amount", "", 10, TextField.NUMERIC);
TransOk = new Command("Enter", Command.OK, 1);
Transaction.append(txtacc1);
Transaction.append(txtacc2);
Transaction.append(txtsk);
Transaction.append(txtamt);
Transaction.addCommand(TransOk);
Transaction.addCommand(Back);
Transaction.setCommandListener(this);
// BROADCAST
txtbc = new TextField("MESSAGE", "", 50, TextField.ANY);
BrdCstOk = new Command("ok", Command.OK, 1);
Broadcast = new Form("Broadcast");
Broadcast.append(txtbc);
Broadcast.addCommand(BrdCstOk);
Broadcast.addCommand(Back);
Broadcast.setCommandListener(this);
// HELP
Help = new Form("Help");
Help.append("1.NAVIGATION:\n");
Help.append("Use up,down arrow keys in the cell or in the key-board for selecting the textboxes/n");
Help.append("\n");
Help.append("2.CONTENT TYPING\n");
Help.append("Either type through cell buttons or by keybord after pressing enter key in the keyboard,press 'save' after typing\n");
Help.append("\n");
Help.append("3.INBOX:\n");
Help.append("Inbox enables you to view your messages\n");
Help.append("\n");
Help.append("4.WRITE MESSAGE:\n");
Help.append("Write message enables you to wrtie the message and send it to th number specified in 'mobile' textbox\n");
Help.append("\n");
Help.append("5.TRANSACTION:\n");
Help.append("Type your account no. in 'Account no1' and the account number to be credited in 'Account no2' ,your secret key in 'secret key' and the amount to be transfered in 'Amount',then click 'ok' button for the transaction \n");
Help.append("\n");
Help.append("6.BROADCAST:\n");
Help.append("Type your message and send it to everybody who are connected to the server\n");
Help.append("\n");
Help.addCommand(Back);
Help.setCommandListener(this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
display = null;
System.out.println("destroyapp");
displayable = null;
}
public void commandAction(Command c, Displayable d) {
// Login
if (c == cmdsel) {
if (list.getString(list.getSelectedIndex()).equals("Login")){
display.setCurrent(Login);
}
}
// Option Selection
if(c == cmdsel1){
if (list1.getString(list1.getSelectedIndex()).equals("Inbox")) {
try {
System.out.println("inside inbox");
out.flush();
out.write("Inbox;".getBytes());
byte b1[] = new byte[1024];
String buf, temp;
in.read(b1);
buf = new String(b1);
temp = SECURE.decrypt(buf);
System.out.println("temp" + temp);
if (temp.indexOf('.') != -1)
temp = temp.substring(0, temp.indexOf('.'));
if (temp.equals("No Message")) {
inbox.append("No Message", null);
} else {
try {
int i = 0, j = temp.indexOf(';');
int count = 1;
while (j != -1) {
inbox.append(temp.substring(i, j), null);
i = j + 1;
j = temp.indexOf(';', j + 1);
}
} catch (Exception e1) {
System.out.println("exp" + e1);
}
}
display.setCurrent(inbox);
} catch (Exception e) {
System.out.println("hai" + e);
}
}
if (list1.getString(list1.getSelectedIndex()).equals("Write Message")) {
display.setCurrent(Message);
}
if (list1.getString(list1.getSelectedIndex()).equals("Help")) {
display.setCurrent(Help);
}
if (list1.getString(list1.getSelectedIndex()).equals("Transaction")) {
display.setCurrent(Transaction);
}
if (list1.getString(list1.getSelectedIndex()).equals("Broadcast")) {
display.setCurrent(Broadcast);
}
}
// Message delete
if (c.equals(SmsDel)) {
try {
out.write(("Delete" + ";" + inbox.getString(inbox.getSelectedIndex())).getBytes());
for (int i = 0; i < inbox.size(); i++) {
inbox.delete(i);
}
} catch (Exception e) {
}
display.setCurrent(list1);
Alert currentAlert = new Alert("Message deleted");
currentAlert.setTimeout(2500);
currentAlert.setString("One Message is deleted");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
}
//login
if (c.equals(Ok)) {
pass= txtpw.getString();
user=txtuser.getString();
//Server Connection
try {
stream = (StreamConnection) Connector.open("socket://localhost:9595");
in = stream.openInputStream();
out = stream.openOutputStream();
byte b[] = new byte[50];
out.flush();
out.write(user.getBytes());
in.read(b);
} catch (Exception ex) {
System.out.println("Exeption " + ex);
}
if ((user.equals("")) || (user.length() != 10)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("your userid is wrong or not entered");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if (pass.equals("") || (pass.length() != 8)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Either your pw is wrong or not entered");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else {
try {
out.flush();
String s11, s22;
s11 = SECURE.encrypt(txtuser.getString());
s22 = SECURE.encrypt(txtpw.getString());
System.out.println("s11:"+s11);
System.out.println("s22:"+s22);
out.write(("login;" + s11 + ";" + s22).getBytes());
byte b11[] = new byte[100];
in.read(b11);
String ers1 = new String(b11);
System.out.println("ers"+ers1);
if (ers1.trim().equals("Error1")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("Sorry you are not authorized , your userid is wrong");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtuser.setString("");
txtpw.setString("");
}
else if (ers1.trim().equals("Error2")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("Sorry you are not authorized ,your password is wrong");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtuser.setString("");
txtpw.setString("");
}
else if (ers1.trim().equals("Error3")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("login messageYou haven't got enough funds to transfer");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtuser.setString("");
txtpw.setString("");
} else {
display.setCurrent(list1);
Alert cur = new Alert("Status");
cur.setTimeout(2500);
cur.setString("login performed successfully,check your password in inbox");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtuser.setString("");
txtpw.setString("");
}
} catch (Exception ex) {
System.out.println("Exeption " + ex);
}
}
}
if (c.equals(Back)) {
display.setCurrent(list1);
}
// sms
if (c.equals(SmsOk))
{
if (txtPhone.getString().equals(user)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Enter another number");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if ((txtMessage.getString()).equals("") || (txtMessage.getString().length() < 2)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Enter Message");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if ((txtPhone.getString()).equals("") || (txtPhone.getString().length() != 10)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("The number field is empty or your own number");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
txtPhone.setString("");
} else {
try {
out.flush();
out.write((SECURE.encrypt(txtMessage.getString()) + ";" +SECURE.encrypt(txtPhone.getString())).getBytes());
byte b1[] = new byte[100];
in.read(b1);
String ers = new String(b1);
display.setCurrent(list1);
Alert currentAlert = new Alert("Message sent");
currentAlert.setTimeout(2000);
currentAlert.setString("Message sent succesfully");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
txtPhone.setString("");
txtMessage.setString("");
} catch (Exception ex) {
System.out.println("Exeption " + ex);
}
}
}
// Transaction
if (c.equals(TransOk)) {
if ((txtacc1.getString()).equals("") || (txtacc1.getString().length() != 6)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Either your account no. is wrong or not entered");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if ((txtacc2.getString().equals("")) || (txtacc2.getString().length() != 6)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Either the account no. is wrong or not entered");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if ((txtsk.getString().equals("")) || (txtsk.getString().length() >= 4)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Either your secret key is wrong or not entered");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else if ((txtamt.getString()).equals("") || (txtamt.getString().length() < 2)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Enter Amount");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else {
try {
out.flush();
String s1, s2, s3, s4;
s1 = SECURE.encrypt(txtacc1.getString());
s2 = SECURE.encrypt(txtamt.getString());
s3 = SECURE.encrypt(txtsk.getString());
s4 = SECURE.encrypt(txtacc2.getString());
out.write(("trans;" + s1 + ";" + s2 + ";" + s3 + ";" + s4).getBytes());
byte b1[] = new byte[100];
in.read(b1);
String ers = new String(b1);
System.out.println("ers"+ers);
if (ers.trim().equals("Error1")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("Sorry you are not authorized ,either one the Account numbers or the secret key is wrong");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtacc1.setString("");
txtacc2.setString("");
txtsk.setString("");
txtamt.setString("");
}
else if (ers.trim().equals("Error2")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("MISUSE Cellnumber and Account number mismatch");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtacc1.setString("");
txtacc2.setString("");
txtsk.setString("");
txtamt.setString("");
}
else if (ers.trim().equals("Error3")) {
Alert cur = new Alert("Warning");
cur.setTimeout(3500);
cur.setString("You haven't got enough funds to transfer");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtacc1.setString("");
txtacc2.setString("");
txtsk.setString("");
txtamt.setString("");
} else if(ers.trim().equals("Success")) {
display.setCurrent(list1);
Alert cur = new Alert("Status");
cur.setTimeout(2500);
cur.setString("Amount transfer performed succesfully");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtacc1.setString("");
txtacc2.setString("");
txtsk.setString("");
txtamt.setString("");
}
} catch (Exception ex) {
System.out.println("Exeption " + ex);
}
}
}
// Broadcast
if (c.equals(BrdCstOk)) {
if (txtbc.getString().equals("") || (txtbc.getString().length() < 2)) {
Alert currentAlert = new Alert("Warning");
currentAlert.setTimeout(1500);
currentAlert.setString("Enter Message");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
} else {
try {
Alert currentAlert = new Alert("Message sent");
currentAlert.setTimeout(2000);
currentAlert.setString("Message sent succesfully");
currentAlert.setType(AlertType.INFO);
display.setCurrent(currentAlert);
out.flush();
out.write(("bc;" + SECURE.encrypt(txtbc.getString())+"!").getBytes());
byte b1[] = new byte[100];
in.read(b1);
txtbc.setString("");
display.setCurrent(list1);
Alert cur = new Alert("Status");
cur.setTimeout(2500);
cur.setString("Broadcast performed succesfully");
cur.setType(AlertType.INFO);
display.setCurrent(cur);
txtbc.setString("");
} catch (Exception ex) {
System.out.println("Exeption " + ex);
}
}
}
}