×
Namespaces

Variants
Actions

Talk:Bluetooth Chat HelloWorld

From Nokia Developer Wiki
Jump to: navigation, search

Contents

I have a problem

Hello I've run 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){
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("Received 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( "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){
// 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

problem on send message more than 2 time

Hi i run this code but i have problem , i`m not able to send message more than 2 time ! after 2 time it`s not work ! can any body help me ? thamks

Mulyawan - need help

could u help me? i want to read some data via bluetooth connection on mobile phone (J2ME). the data transmitted using bluetooth module serial port profile (SPP).

how can i read the data?

mulyawan 12:19, 3 November 2011 (EET)

Hamishwillee - Mulyawan - this was written in 2007

Hi @Mulyawan

This article was written in 2007 and it is quite possible the author is no longer reviewing it (although their email is in the code so you could email them directly). Unless your question is directly related to the article and code in question, I suggest you instead post your query on the Java ME discussion boards as this will increase the number of people who see it: http://www.developer.nokia.com/Community/Discussion/forumdisplay.php?3-Mobile-Java

Regards

Hamish

hamishwillee 05:49, 4 November 2011 (EET)

 

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×