| Reply | « Previous Thread | Next Thread » |
|
Hi everybody!
I'm actually trying to write a very simple program to communicate a message PC-->Mobile, something like a simple chat. I've implemented the connection, and it seems to work properly, 'cause when the application starts, a message arrive to mobile but it's something like "B@f828ed68"...I don't know, it seems to be a memory address...Surely it isn't the message I would like to read!:-D Am I wrong in using output/input streams, maybe? Please, HELP ME!!! Here's the code I found in internet: SERVER SIDE (PC) ----------- import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnectionNotifier; public class btj2seserver { static String ServiceUrl = "btspp://localhost:10203040607040A1B1C1DE100;name=SPP"; public static void main(String[] args) { System.out.println("server is running..."); try { // create a server connection StreamConnectionNotifier notifier =(StreamConnectionNotifier) Connector.open(ServiceUrl); System.out.println("Created server connection."); // accept client connections StreamConnection connection = notifier.acceptAndOpen(); System.out.println("Client connection accepted."); // prepare to send/receive data byte buffer[] = new byte[100]; String msg = "hello there, client"; InputStream is = connection.openInputStream(); OutputStream os = connection.openOutputStream(); System.out.println("Opened in/out streams."); // send data to the client os.write(msg.getBytes()); System.out.println("Message is flying."); // read data from client is.read(buffer); System.out.println("Message is coming."); connection.close(); } catch(IOException e) { e.printStackTrace(); } } } CLIENT SIDE (MOBILE) ----------- import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.bluetooth.BluetoothStateException; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.LocalDevice; import javax.bluetooth.ServiceRecord; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class Client extends MIDlet implements Runnable, CommandListener { StreamConnection conn; public void run(){ //make a form and show the user "Locating the server" Form form = new Form("Client"); Display.getDisplay(this).setCurrent(form); form.append("Locating server..."); try{ //set up the bluetooth connection: LocalDevice local = LocalDevice.getLocalDevice(); form.append("Local dev OK:" + local.getFriendlyName() + ":" + local.getBluetoothAddress()); DiscoveryAgent agent = local.getDiscoveryAgent(); form.append("Discovery Agent OK..."); String connString = agent.selectService( new UUID("10203040607040A1B1C1DE100", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); System.out.println(connString); if(connString != null){ try { conn = (StreamConnection) Connector.open(connString); form.append("Connection OK..."); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { byte buffer[] = new byte[100]; String msg = "hello there, server"; InputStream is = conn.openInputStream(); OutputStream os = conn.openOutputStream(); // send data to the server os.write(msg.getBytes()); // read data from the server is.read(buffer); form.append(buffer.toString()); conn.close(); } catch(IOException e) { e.printStackTrace(); } } else { //show the user an error if it occurs form.append("Unable to locate service"); } } catch (BluetoothStateException e){ //show the user an error if it occurs form.append("Problem in the connection"); } } public void startApp() throws MIDletStateChangeException{ new Thread(this).start(); } public void pauseApp(){} public void destroyApp(boolean unconditional){} public void commandAction(Command c, Displayable d){ notifyDestroyed(); } void destroyImpl() { } } |
|
An update:
I've changed the code below to send only an integer from mobile to pc. When I print to screen the return value is -1. This mean that there's no incoming data in the stream...Well, I really don't know... Maybe I first have to do something like an handshake to reach the exact time in wich the data is sent...I mean, maybe the stream is empty because, when I sent the message, the server is not yet ready to get it...any ideas? PLEASE I NEED HELP!!!!!![]() |
|
String data = new String(buffer, "UTF-8");
form.append(data); On you server, you should use os.write(msg.getBytes("UTF-8"));, too. Anyway for the start, I commend to use the DataInput and DataOutput streams and their integer and string features. Does that help? |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|