Hello, I have a problem. I'm developing a J2ME application to send messages via bluetooth and I can't send more than one. I don't know why!! if you can help me please. Answer me or contact at joseaferrando@hotmail.com. The code is based on
http://wiki.forum.nokia.com/index.php/Bluetooth_Chat_HelloWorld
//FOR 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 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);} } }
//THANK YOU
Log in to post a comment.
Hello, I have a problem. I'm developing a J2ME application to send messages via bluetooth and I can't send more than one. I don't know why!! if you can help me please. Answer me or contact at joseaferrando@hotmail.com. The code is based on
http://wiki.forum.nokia.com/index.php/Bluetooth_Chat_HelloWorld
//FOR 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 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);}
}
}
//THANK YOU