Hello i want to make a simple rfcomm connection and i get an error that i don't understand and don't know how to get around it.Hope you guys can help.Here is the source code,and also interesting is that on the wtk emulator it works fine but on my mobile phone(nokia 5230) it doesn't.The error i get is java.lang.classcastexception javax.microedition.lcdui.StringItem incompatible with javax.microedition.lcdui.TextField if it helps
Server code
And the client sideCode:public class EchoServer extends BluetoothMIDlet { private void displayConnectionString(Form f, StreamConnectionNotifier notifier) { try { // Retrieve the connection string to use to // connect to this server LocalDevice device = LocalDevice.getLocalDevice(); ServiceRecord record = device.getRecord(notifier); String connString = record.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); int index = connString.indexOf(";"); connString = connString.substring(0, index); // Display the connection string on the Form f.append(new StringItem("Connection String:", connString)); } catch (BluetoothStateException e) { f.append("BluetoothStateException: " + e.getMessage()); } } public void run() { // Create the output Form and set it to be the // current Displayable Form msgForm = new Form("Echo Server"); msgForm.addCommand(new Command("Exit", Command.EXIT, 1)); msgForm.setCommandListener(this); Display.getDisplay(this).setCurrent(msgForm); try { // Create the notifier object StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:"+ "123456789ABCDE;name=Echo Server"); //Display the connection string on the Form displayConnectionString(msgForm, notifier); for (;;) { StreamConnection conn = notifier.acceptAndOpen(); OutputStream output = conn.openOutputStream(); InputStream input = conn.openInputStream(); // Continued reading the input stream until the // stream is closed. Display the data on the // screen and write it to the output stream. byte [ ] data = new byte[10]; int length =0; while ((length = input.read(data)) != -1) { msgForm.append(new String(data, 0, length)); output.write(data, 0, length); output.flush(); } // Close the streams and the connection output.close(); input.close(); conn.close(); } } catch (IOException e) { msgForm.append("IOException: "+ e.getMessage()); } } }
Code:public class EchoClient extends BluetoothMIDlet { private Form connForm; /** * The Command used to Connect to the server. */ private Command connectCommand; /** * Called when the MIDlet is made active. This * method displays a Form that retrieves the * Bluetooth address and the channel ID of the * Echo Server. */ // The InputStream to receive data from the server. private InputStream input; // The OutputStream to send data to the server. private OutputStream output; // The connection to the server private StreamConnection conn; public EchoClient() { // TODO Auto-generated constructor stub } public void destroyApp(boolean unconditional) { // TODO Auto-generated method stub } public void pauseApp() { // TODO Auto-generated method stub } public void startApp() throws MIDletStateChangeException { // Create the Form. Add the Connect and Exit // Commands to the Form. connForm = new Form("Echo Client"); connectCommand = new Command("Connect", Command.OK, 1); connForm.addCommand(connectCommand); connForm.addCommand(new Command("Exit", Command.EXIT, 1)); connForm.setCommandListener(this); // Add the TextFields to retrieve the // Bluetooth address and channel // ID of the Echo Server TextField address = new TextField("Address", null, 12, TextField.ANY); connForm.append(address); TextField channel = new TextField("Channel", null, 2, TextField.NUMERIC); connForm.append(channel); Display.getDisplay(this).setCurrent(connForm); } public void commandAction(Command c, Displayable d){ switch (c.getCommandType()) { case Command.OK: // The Connect Command was selected so start a // thread to establish the connection to the server new Thread(this).start(); break; case Command.SCREEN: // The Send Command was selected so send the // message to the server int index = connForm.size() - 1; // If the last Item is a TextField, then no message // is currently being sent so send a Message. Item item = connForm.get(index); if (item instanceof TextField) { TextField field = (TextField)item; connForm.delete(index); // Start a thread to send the message to the // server and process the reply new Thread(new Message(field.getString() + "\n", input, output)).start(); } /* try{ output.write("Hello, World".getBytes()); } catch(ClassCastException e){ connForm.append("Error message "+e.getMessage()); } catch(IOException e){ connForm.append("Error message "+e.getMessage()); } */ break; case Command.EXIT:// The Exit Command was selected so destroy the // MIDlet try { input.close(); output.close(); conn.close(); } catch (Exception e) { } notifyDestroyed(); break; } } public void run() { String connString =getConnectionString(); connForm.append("Connecting to Server\n"); if (connectToServer(connString)) { connForm.append("Done"); // Remove the Connect Command and add the Send // Command to this Form connForm.removeCommand(connectCommand); Command sendCommand = new Command("Send", Command.SCREEN, 1); connForm.addCommand(sendCommand); // Add a TextField to the Form to retrieve the // text to send to the server from the user connForm.append(new TextField("Text to send", null, 20, TextField.ANY)); } } private String getConnectionString() { // Retrieve the TextFields from the Form TextField address = (TextField)connForm.get(0); TextField channel = (TextField)connForm.get(1); // Create the connection string StringBuffer temp = new StringBuffer("btspp://"); temp.append(address.getString()); temp.append(":"); temp.append(channel.getString()); // Remove the TextFields from the Form connForm.delete(0); connForm.delete(0); return temp.toString(); } private boolean connectToServer(String connString) { try { // Establish a connection to the server conn = (StreamConnection) Connector.open(connString); // Retrieve the input and output streams to //communicate with input = conn.openInputStream(); output = conn.openOutputStream(); return true; } catch (IOException e) { connForm.append("Connect failed (IOException: "); connForm.append(e.getMessage()); connForm.append(")\n"); return false; } } class Message implements Runnable { // The message to send to the server. private String theMessage; // The InputStream to read the reply from. private InputStream input; // The OutputStream to send the message to. private OutputStream output; /** * Creates a new Message to send to the server. * * @param msg the message to send * @param in the InputStream to read the reply from * @param out the stream to write the message to */ public Message(String msg, InputStream in, OutputStream out) { theMessage = msg; input = in; output = out; } /** * Sends the message to the server and reads the echo * in reply. The method adds the echo to the Form and * then adds a new TextField to the end of the Form. */ public void run() { try { // Send the message to the server. byte[] data = theMessage.getBytes(); output.write(data); output.flush(); // Read the reply and keep it in a StringBuffer // until the full reply is received. int fullLength = data.length; int length = input.read(data); fullLength -= length; StringBuffer buf = new StringBuffer(new String(data, 0, length)); while (fullLength > 0) { length = input.read(data); fullLength -= length; buf = buf.append(new String(data, 0, length)); } // Display the reply on the Form and remove the // final new line sent from the server connForm.append("\n"); String displayString = buf.toString(); displayString = displayString.substring(0, displayString.length() - 1); connForm.append(displayString); } catch (IOException e) { connForm.append("\nFailed to send message: " + e.getMessage()); } connForm.append(new TextField("Text to send", null, 20, TextField.ANY)); } } }

.The error i get is java.lang.classcastexception javax.microedition.lcdui.StringItem incompatible with javax.microedition.lcdui.TextField if it helps 
Reply With Quote
,and i need it like that anyway but thanks for your advice.


