Anda di halaman 1dari 8

3.

Bluetooth
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class BlueToothExp extends MIDlet implements CommandListener, Runnable {
/** Creates a new instance of BlueToothExp */
public BlueToothExp() {
initialize();
}

private List list1;//GEN-LINE:MVDFields


private ClientServer m_BlueObj;
private boolean m_bRunThread=false;
private boolean m_bIsServer=false;

public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin


// Insert global pre-action code here
if (displayable == list1) {//GEN-BEGIN:MVDCABody
if (command == list1.SELECT_COMMAND) {
switch (get_list1().getSelectedIndex()) {
case 0://GEN-END:MVDCABody
// Insert pre-action code here
// Do nothing//GEN-LINE:MVDCAAction5
// Insert post-action code here

break;//GEN-BEGIN:MVDCACase5
case 1://GEN-END:MVDCACase5
// Insert pre-action code here
// Do nothing//GEN-LINE:MVDCAAction7
// Insert post-action code here
//SERVER
if(m_bRunThread==false)
{
Thread thread = new Thread(this); thread.start();
m_bRunThread=true;
m_bIsServer=true;
}
break;//GEN-BEGIN:MVDCACase7
case 2://GEN-END:MVDCACase7
// Insert pre-action code here
//CLIENT
if(m_bRunThread==false)
{
Thread thread = new Thread(this); thread.start();
m_bRunThread=true;
m_bIsServer=false;
}
// Do nothing//GEN-LINE:MVDCAAction9
// Insert post-action code here
break;//GEN-BEGIN:MVDCACase9
}
}
}//GEN-END:MVDCACase9
// Insert global post-action code here
}//GEN-LINE:MVDCAEnd
private void initialize() {//GEN-END:MVDInitBegin
// Insert pre-init code here
getDisplay().setCurrent(get_list1());//GEN-LINE:MVDInitInit
// Insert post-init code here
}//GEN-LINE:MVDInitEnd

/**
* This method should return an instance of the display.
*/
public Display getDisplay() {//GEN-FIRST:MVDGetDisplay
return Display.getDisplay(this);
}//GEN-LAST:MVDGetDisplay

/**
* This method should exit the midlet.
*/
public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}//GEN-LAST:MVDExitMidlet

/** This method returns instance for list1 component and should be called instead of accessing list1
field directly.//GEN-BEGIN:MVDGetBegin2
* @return Instance for list1 component
*/
public List get_list1() {
if (list1 == null) {//GEN-END:MVDGetBegin2
// Insert pre-init code here
list1 = new List(null, Choice.IMPLICIT, new String[] {//GEN-BEGIN:MVDGetInit2
"Client Server",
"Server",
"Client"
}, new Image[] {
null,
null,
null
});
list1.setCommandListener(this);
list1.setSelectedFlags(new boolean[] {
false,
false,
false
});//GEN-END:MVDGetInit2
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd2
return list1;
}//GEN-END:MVDGetEnd2

public void startApp() {


}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {

m_bRunThread=false;
m_BlueObj.CloseAll();
}

public void run()


{
while(m_bRunThread)
{
try
{
if(m_BlueObj==null)
{
m_BlueObj=new ClientServer(m_bIsServer);
}

String str = m_BlueObj.RecieveMessages();

System.out.println(str);

if(m_bIsServer)
m_BlueObj.SendMessages("Hi there its Mr Server");
else
m_BlueObj.SendMessages("Hi there its Mr Client");

Thread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}

}
}//end while

Client:
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;

public class ClientServer implements DiscoveryListener{

/** Creates a new instance of ClientServer */


UUID RFCOMM_UUID = new UUID(0x0003);
private String m_ServerUrl = "btspp://localhost:" + RFCOMM_UUID +
";name=rfcommtest;authorize=true";
private StreamConnection m_StrmConn = null;
private LocalDevice m_LclDevice = null;
private InputStream m_Input=null;
private OutputStream m_Output=null;
private StreamConnectionNotifier m_StrmNotf=null;
public boolean m_bIsServer=false,m_bServerFound=false,m_bInitServer=false,m_bInitClient=false;
private static String m_strUrl;
private final String SEVER_RESPONSE= "RUN_THE_GAME",CLIENT_RESPONSE="CLIENT_IS_READY";
private DiscoveryAgent m_DscrAgent=null;

public ClientServer(boolean isServer)


{
m_bIsServer = isServer;

if(m_bIsServer)
{
InitServer();
}
else
{
InitClient();
}

private void InitServer()


{

m_strUrl= "btspp://localhost:" + RFCOMM_UUID + ";name=rfcommtest;authorize=true";

// m_StrmConn = BTFACADE.waitForClient(SERVICE_NBR);

try
{
m_LclDevice = LocalDevice.getLocalDevice();

m_LclDevice.setDiscoverable(DiscoveryAgent.GIAC);

m_StrmNotf = (StreamConnectionNotifier)Connector.open(m_strUrl);

m_StrmConn = m_StrmNotf.acceptAndOpen();

m_bInitServer = true;

m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();
}
catch (BluetoothStateException e)
{
System.err.println( "BluetoothStateException: " + e.getMessage() );
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch(Exception e)
{
System.err.println( "Exception: " + e.getMessage() );
}

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)
{
System.out.println("Problem in searching the blue tooth devices");
ex.printStackTrace();
}

public void SendMessages(String v_strData)


{
if((m_bInitClient) || (m_bInitServer) )
{
try
{
m_Output.write(v_strData.length());
m_Output.write(v_strData.getBytes());

}
catch (IOException ex)
{
ex.printStackTrace();
}

}
}

public String RecieveMessages()


{
byte[] data = null;

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)
{
System.err.println(e);
}
return new String(data);
}
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);

m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();

m_Output.write(CLIENT_RESPONSE.length());
m_Output.write(CLIENT_RESPONSE.getBytes());

System.out.println("serviceSearchCompleted");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
void CloseAll()
{
try
{
if(m_Output!=null)
m_Output.close();

if( m_Input!=null)
m_Input.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
//called when service found during service search
public void servicesDiscovered(int transID, ServiceRecord[] records)
{

for (int i = 0; i < records.length; i++)


{
m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);

System.out.println(m_strUrl);
if(m_strUrl.startsWith("btspp")) //we have found our service protocol
{
m_bServerFound = true;
m_bInitClient=true;
break;
}
}
}

//Called when device is found during inquiry


public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{

try
{
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " +
cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));
UUID uuidSet[] = new UUID[1];
uuidSet[0] = RFCOMM_UUID;
int searchID = m_DscrAgent.searchServices(null,uuidSet,btDevice,this);
}
catch (Exception e)
{
System.out.println("Device Discovered Error: " + e);
}
}
}
OUTPUT:

Anda mungkin juga menyukai