You Are Here:

Community: Developer Discussion Boards

#1 Old Talking Problems while communicating between PC and mobile - 2008-05-12, 13:40

Join Date: May 2008
Posts: 2
bacchdani
Offline
Registered User
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() {

}

}
Reply With Quote

#2 Old Re: Problems while communicating between PC and mobile - 2008-05-12, 14:28

Join Date: May 2008
Posts: 2
bacchdani
Offline
Registered User
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!!!!!
Reply With Quote

#3 Old 2008-05-13, 15:26

Join Date: Mar 2003
Posts: 2,617
traud
Offline
Super Contributor
Quote:
Originally Posted by bacchdani View Post
form.append(buffer.toString());
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 With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia