Client-Server and Bluetooth

R

Richard Lavoie

Hallo everybody,

I'm trying to write 2 midlets that can communicate over bluetooth. I'm
new to this. I compliled my 2 application to jad-files and emulate
them with he nokia developper's suite.
My problem is that the applications does not see eachother. Can anyone
help me.
Thanks in advance.

Richard

/********************** Server *******************************/

/*
* BluetoothMIDletServer.java
*
* Created on 2. Juni 2004, 12:06
*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.bluetooth.*;

public class BluetoothMIDletServer extends MIDlet implements Runnable,
CommandListener {

Form mainForm = new Form("Poker");
private Command exitCommand;
private Command weiterCommand;

public BluetoothMIDletServer(){
exitCommand = new Command("exit", Command.EXIT, 1);
weiterCommand = new Command("weiter", Command.EXIT, 2);
}

public void startApp() throws MIDletStateChangeException{

mainForm.addCommand(exitCommand);
mainForm.addCommand(weiterCommand);
mainForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainForm);
new Thread(this).start();

}

public void pauseApp(){}
public void destroyApp(boolean unconditional){}


public void run(){

final UUID uuid = new
UUID("123456789012345678901234567890AB", false);

mainForm.setTitle("Server");

L2CAPConnectionNotifier server = null;
String message = "";
byte[] data = null;
int length;

try {
LocalDevice local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
} catch (BluetoothStateException e) {
System.err.println("Failed to start service");
System.err.println("BluetoothStateException: " +
e.getMessage());
return;
}

try {
server = (L2CAPConnectionNotifier)Connector.open(
"btl2cap://localhost:102030405060708090A0B0C0D0E0F010");
} catch (IOException e) {
System.err.println("Failed to start service");
System.err.println("IOException: " + e.getMessage());
return;
}

System.out.println("Starting L2CAP Print Server");

while (!(message.equals("Stop Server"))) {

message = "";
L2CAPConnection conn = null;
System.out.println("Versuche Nachricht zu bekommen!");
try {
conn = server.acceptAndOpen();

length = conn.getReceiveMTU();
data = new byte[length];

length = conn.receive(data);
System.out.println("Laenge ist: " + length);
while (length != -1) {
message += new String(data, 0, length);

try {
length = conn.receive(data);
} catch (IOException e) {
break;
}
}

System.out.println(message);

} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
} finally {
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
}

try {
server.close();
} catch (IOException e) {

}
}
public void commandAction (Command c, Displayable d){
if (c == exitCommand){
destroyApp(true);
notifyDestroyed();
}
else
run();
}
}




/********************** Client *******************************/

/*
* client.java
*
* Created on 2. Juni 2004, 12:04
*/

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.bluetooth.*;

public class BluetoothMIDletClient extends MIDlet implements Runnable,
CommandListener {

Form mainForm = new Form("Client");
private Command exitCommand;

public BluetoothMIDletClient(){
exitCommand = new Command("Exit", Command.EXIT, 1);
}

public void startApp() throws MIDletStateChangeException{

mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainForm);
new Thread(this).start();

}

public void pauseApp(){}
public void destroyApp(boolean unconditional){}


public void run(){
mainForm.setTitle("Client");

try {
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent agent = local.getDiscoveryAgent();
String connString = agent.selectService(
new UUID("123456789012345678901234567890AB",
false),
ServiceRecord.NOAUTHENTICATE_NOENCRYPT,
false);
System.out.println("String connection: "+connString);
if (connString != null){
try{
System.out.println("connString ist nicht
null");
StreamConnection conn = (StreamConnection)
Connector.open(connString);

OutputStream out = conn.openOutputStream();
out.write("Sende diese Test
message!\n".getBytes());
out.close();
conn.close();

mainForm.append("send!");
System.out.println("send");
}
catch (IOException e){
mainForm.append("IOException:" +
e.getMessage());
}

}
else{

mainForm.append("can't find server!\n");

}
}catch (BluetoothStateException e){
mainForm.append("BluetoothStateException: ");
mainForm.append(e.getMessage());
}

}
public void commandAction (Command c, Displayable d){
if (c == exitCommand){
destroyApp(true);
notifyDestroyed();
}
}
}
 
L

Lukasz Huculak

Richard Lavoie said:
Hallo everybody,

I'm trying to write 2 midlets that can communicate over bluetooth. I'm
new to this. I compliled my 2 application to jad-files and emulate
them with he nokia developper's suite.
My problem is that the applications does not see eachother. Can anyone
help me.
Thanks in advance.

Richard

I suggest to use DiscoveryListener that will listen for events from
DiscoveryAgent inquiry. If you need remote device address try get it
in method DiscoveryListener.deviceDiscovered().

In fact I never used UUID class - I still wonder what is that for?...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top