Code:
package Touche;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
/*
* Bluetoothclient.java
*
* Created on December 4, 2006, 1:40 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Farhan Hameed Khan
*/
public class Bluetoothclient implements DiscoveryListener,Runnable{
/** Creates a new instance of Bluetoothclient */
UUID RFCOMM_UUID = new UUID(0x0003);
private StreamConnection m_StrmConn = null;
private LocalDevice m_LclDevice = null;
private InputStream m_Input=null;
private OutputStream m_Output=null;
public boolean m_bIsServer=false,m_bServerFound=false,m_bInitServer=false,m_bInitClient=false;
private static String m_strUrl;
private final String CLIENT_RESPONSE="CLIENT_IS_READY";
private DiscoveryAgent m_DscrAgent=null;
private Touche parentMidlet;
private RemoteDevice btDevice;
private UUID uuid;
public Bluetoothclient(Touche parent)
{
parentMidlet = parent;
InitClient();
}
private void InitClient()
{
SearchAvailDevices();
}
public void SearchAvailDevices()
{
try
{
//First get the local device and obtain the discovery agent.
m_LclDevice = LocalDevice.getLocalDevice();
m_DscrAgent= m_LclDevice.getDiscoveryAgent();
m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
}
catch (BluetoothStateException ex)
{
Alert al= new Alert(null,"Problem in searching the blue tooth devices", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
System.out.println("Problem in searching the blue tooth devices");
ex.printStackTrace();
}
}
public void SendMessages(String v_strData)
{
try
{
m_Output.write(v_strData.length());
m_Output.write(v_strData.getBytes());
}
catch (IOException ex)
{
Alert al= new Alert(null,"sendmessage error", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
ex.printStackTrace();
}
}
public String RecieveMessages()
{
byte[] data = null;
String msg;
try
{
int length = m_Input.read();
data= new byte[length];
length = 0;
while (length != data.length)
{
int ch = m_Input.read(data, length, data.length - length);
if (ch == -1)
{
throw new IOException("Can't read data");
}
length += ch;
}
}
catch (IOException e)
{
Alert al= new Alert(null,"receivemessage error", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
System.err.println(e);
}
msg = new String(data);
Alert al= new Alert(null,"received message"+msg, null, AlertType.ERROR);
al.setTimeout(1000);
parentMidlet.getDisplay().setCurrent(al);
return msg;
}
/*********************************************************************************************
* below are the pure virtual methods of discoverlistern
*
*
*******************************************************************************************/
public void inquiryCompleted(int discType)
{
System.out.println("InquiryCompleted");
}
//called when service search gets complete
public void serviceSearchCompleted(int transID, int respCode)
{
if(m_bServerFound)
{
try
{ //lets the communication start by setting the url and send client reponse
m_StrmConn = (StreamConnection) Connector.open(m_strUrl);
Alert al= new Alert(null,"StreamConnection"+m_StrmConn, null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();
m_Output.write(CLIENT_RESPONSE.length());
m_Output.write(CLIENT_RESPONSE.getBytes());
//Alert al= new Alert(null,"serviceSearchCompleted"+ m_Output+m_Input, null, AlertType.ERROR);
//al.setTimeout(20000);
//parentMidlet.getDisplay().setCurrent(al);
System.out.println(m_strUrl);
System.out.println("serviceSearchCompleted");
}
catch (IOException ex)
{
Alert al= new Alert(null,"service search completed", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
ex.printStackTrace();
}
}
}
void CloseAll()
{
try
{
if(m_Output!=null)
m_Output.close();
if( m_Input!=null)
m_Input.close();
}
catch (IOException ex)
{
Alert al= new Alert(null,"closeall error", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
ex.printStackTrace();
}
}
//called when service found during service search
public void servicesDiscovered(int transID, ServiceRecord[] records)
{
for (int i = 0; i < records.length; i++)
{
try {
m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);
Alert al= new Alert(null,"m_strUrl "+m_strUrl, null, AlertType.ERROR);
al.setTimeout(20000);
parentMidlet.getDisplay().setCurrent(al);
}
catch (Exception e) {
Alert al= new Alert(null,"servicesDiscovered", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
}
System.out.println(m_strUrl);
if(m_strUrl.startsWith("btspp")) //we have found our service protocol
{
//Alert al= new Alert(null,"m_strUrl.startsWith btgoep", null, AlertType.ERROR);
//al.setTimeout(20000);
// parentMidlet.getDisplay().setCurrent(al);
m_bServerFound = true;
m_bInitClient=true;
break;
}
}
}
//Called when device is found during inquiry
public void deviceDiscovered(RemoteDevice btDevice2, DeviceClass cod)
{
Thread t = new Thread(this);
t.start();
btDevice = btDevice2;
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try
{
if(m_DscrAgent != null){
if (btDevice != null)
{
// Search for Services
UUID uuidSet[] = new UUID[1];
uuidSet[0] = RFCOMM_UUID;
int searchID = m_DscrAgent.searchServices(null,uuidSet,btDevice,this);
//Alert al= new Alert(null,"After search_services", null, AlertType.ERROR);
// al.setTimeout(10000);
// parentMidlet.getDisplay().setCurrent(al);
}
else
{
Alert al= new Alert(null,"btDevice null!!!", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
}
}
else
{
Alert al= new Alert(null,"m_DscrAgent null!!!", null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
}
}
catch (Exception e)
{
Alert al= new Alert(null,"Device Discovered Error: " + e, null, AlertType.ERROR);
al.setTimeout(10000);
parentMidlet.getDisplay().setCurrent(al);
System.out.println("Device Discovered Error: " + e);
}
}
}
Now when I run my code, the little bluetooth communication icon appears near the bluetooth icon. Now it seems that both the client and server connect and create streams, but don`t receive each others messages, it`s like they are talking in parallel...SOMEBODY anybody any ideeas??? PLEASE!!! pretty please!!