This page was last modified 16:46, 19 March 2008.
Talk:Bluetooth Chat HelloWorld
From Forum Nokia Wiki
I have a problem
Hello I've runned the code in my NetBeans and I have a problem. I'm not capable to send more than one message and I've tried with con.Close() at Server and at Client. Could you help me please?
// FOR THE SERVER
public void run(){
System.out.println("Starting server - please wait...");
try {
//notifier = null;
//con = null;
local_device = LocalDevice.getLocalDevice();
System.out.println("SERVER: " + local_device.toString());
DiscoveryAgent disc_agent = local_device.getDiscoveryAgent();
System.out.println("SERVER: " + disc_agent.toString());
local_device.setDiscoverable(DiscoveryAgent.LIAC);
String service_UUID = "6";
deviceName = local_device.getFriendlyName();
String url = "btl2cap://localhost:" + service_UUID + ";name=" + deviceName;
System.out.println("SERVER: " + url);
//if (midlet.connectedServer == false){
notifier = (L2CAPConnectionNotifier)Connector.open(url);
// con = (L2CAPConnection)Connector.open(url);
//con = (L2CAPConnection)notifier.acceptAndOpen();
//midlet.connectedServer = true;
//}
// L2CAPConnectionNotifier notifier = (L2CAPConnectionNotifier)Connector.open(url);
// con = notifier.acceptAndOpen();
while (listening) {
con = (L2CAPConnection)notifier.acceptAndOpen();
if (con.ready()){
System.out.println("READY!");
//midlet.connectedServer = true;
byte[] b = new byte[1000];
//
System.out.println("1B " + b.toString());
//
con.receive(b);
//
System.out.println("2B " + b.toString());
//
String s = new String(b, 0, b.length);
//System.out.println("Recieved from client: " + s.trim());
strText = midlet.get_txfSend().getString();
mysend(strText);
//midlet.get_listInbox().append(s.trim(),null);
//listening=false;
// notifier.close(); // notifier = null; // con = null;
}
else
{
// con = notifier.acceptAndOpen();
System.out.println("NO ha entrado en el if");
}
con.close();
}
}catch(BluetoothStateException e){System.out.println(e);} catch(IOException f){System.out.println(f);}
}
// FOR THE CLIENT
public void run() {
System.out.println("Starting client - please wait...");
try {
//con = null;
LocalDevice local_device = LocalDevice.getLocalDevice();
System.out.println("CLIENT: " + local_device.toString());
DiscoveryAgent disc_agent = local_device.getDiscoveryAgent();
System.out.println("CLIENT: " + disc_agent.toString());
local_device.setDiscoverable(DiscoveryAgent.LIAC);
inq_listener = new InquiryListener();
synchronized(inq_listener) {
disc_agent.startInquiry(DiscoveryAgent.LIAC, inq_listener);
try {inq_listener.wait(); }
catch(InterruptedException e){}
}
Enumeration devices = inq_listener.cached_devices.elements();
System.out.println("CLIENT: " + devices.toString());
UUID[] u = new UUID[]{new UUID( "6", false )};
int attrbs[] = { 0x0100 };
serv_listener = new ServiceListener();
while( devices.hasMoreElements() ) {
synchronized(serv_listener) {
disc_agent.searchServices(attrbs, u, (RemoteDevice)devices.nextElement(), serv_listener);
try {serv_listener.wait();} catch(InterruptedException e){}
}
}
} catch (BluetoothStateException e) {System.out.println(e);}
if (serv_listener.service!=null){
try {
String url;
url = serv_listener.service.getConnectionURL(0, false);
System.out.println("CLIENT: " + url.toString());
deviceName = LocalDevice.getLocalDevice().getFriendlyName();
System.out.println("CLIENT: " + deviceName.toString());
// if (midlet.connectedClient ==false){ // con = (L2CAPConnection) Connector.open( url ); // con.send("".getBytes()); // }
con = (L2CAPConnection) Connector.open( url );
con.send("".getBytes());
byte[] b = new byte[1000];
while (listening) {
if (con.ready()){
// midlet.connectedClient = true;
con.receive(b);
System.out.println(b.toString());
String s = new String(b, 0, b.length);
System.out.println("Received from server: " + s.trim());
//listening = false;
System.out.println("CLIENT: " + s.trim());
midlet.get_listInbox().append(s.trim(),null);
}
}
con.close();
} catch (IOException g) {System.out.println(g);}
}
}
The documentation is different from the code
The first comments says that the file is called BtoothMidlet but the real name is BtoothChat.
/*
* BtoothMidlet.java * * Created on April 18, 2007, 12:39 AM */
package wiki.nokia.example;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
/**
* * @author Luis Albinati (luis.albinati@gmail.com) */
public class BtoothChat extends MIDlet
