Hi all,
I'm new in J2me and I need help. I've programmed a little chat but it doesn't work. Maybe it's because of the mobile phones I used. I've already searched in the Internet but I haven't found anything. Here is the code of the searcher:
And the Server Code:Code:public class Searcher extends TimerTask implements DiscoveryListener { private LocalDevice localDevice = null; private DiscoveryAgent discoveryAgent = null; private String connectionURL = null; private RemoteDevice[] device = null; private ServiceRecord[] records = null; private boolean inquiryCompl = false; int count = 0; public void run() { try { StringItem sirun = new StringItem("Searcher: gestartet", null); results1.append(sirun); localDevice = LocalDevice.getLocalDevice(); discoveryAgent = localDevice.getDiscoveryAgent(); device = new RemoteDevice[10]; discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); count = 0; } catch (BluetoothStateException ex) { ex.printStackTrace(); } } public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) { StringItem sirun = new StringItem("Searcher: Device gefunden", null); results1.append(sirun); device[count++] = btDevice; } public void inquiryCompleted(int arg0) { StringItem sirun = new StringItem("Searcher: Inquiry beendet", null); results1.append(sirun); for (int i = 0; i < count; i++) { searchforservices(i); } } public void serviceSearchCompleted(int arg0, int arg1) { StringItem sirun = new StringItem("Searcher: Servicesuche beendet", null); results1.append(sirun); } public void servicesDiscovered(int arg0, ServiceRecord[] servRecords) { StringItem sirun = new StringItem("Searcher: Services gefunden", null); results1.append(sirun); for(int i=0;i<servRecords.length;i++) { int[] atrids = servRecords[i].getAttributeIDs(); connectionURL = servRecords[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,true); clients[clientscounter] = new Client(); clients[clientscounter].connectionString = connectionURL; clients[clientscounter].run(); StringItem siruncl = new StringItem("Searcher: Neuer Client", null); results1.append(siruncl); } }
And the Client CodeCode:package hello; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.bluetooth.UUID; import javax.microedition.io.*; public class Server implements Runnable{ StreamConnection con = null; StreamConnectionNotifier service= null; InputStream ip = null; OutputStream op = null; String namer = ""; String name = ""; public boolean connected; public void send(String message) { try { op.write(message.getBytes()); op.flush(); } catch (IOException ex) { connected = false; } } public String getinput() { String s = ""; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); int data; while ((data = ip.read()) != -1) { out.write(data); } s = out.toString(); } catch (IOException ex) { ex.printStackTrace(); } return s; } public void run() { try { UUID uuid = new UUID("1111",false); //Extends a stream for client to connect service = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + uuid.toString() + ";authenticate=false;encrypt=false;name=" + name); //Server waiting for client to connect con = service.acceptAndOpen(); //Open streams for two way communication. ip = con.openInputStream(); op = con.openOutputStream(); connected = true; } catch (IOException ex) { ex.printStackTrace(); } } }
... And yes I've already defined a server that runs in the beginning.Code:package hello; import java.io.*; import javax.microedition.io.*; public class Client implements Runnable { public String connectionString; public StreamConnection connection; public InputStream in; public OutputStream out; public boolean goodadress; public boolean asked; public String name; public void run() { try { connection = (StreamConnection) Connector.open(connectionString); out = connection.openOutputStream(); in = connection.openInputStream(); } catch (IOException ex) { goodadress = false; } } public void send(String message) { try { out.write(message.getBytes()); out.flush(); } catch (IOException ex) { goodadress = false; } } public String getinput() { String s = ""; try { ByteArrayOutputStream outer = new ByteArrayOutputStream(); int data; while ((data = in.read()) != -1) { outer.write(data); } s = outer.toString(); } catch (IOException ex) { ex.printStackTrace(); } return s; } }
Is the code right or wrong or is it because of my mobile phones? I used an Nokia 5800 Xpress music and a N82. I wasn't aible to test it in the emulator. It's not the whole code but the important part.
Sorry for your nice help.
Benni

Reply With Quote


