Академический Документы
Профессиональный Документы
Культура Документы
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();
}
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
m_bRunThread=false;
m_BlueObj.CloseAll();
}
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;
if(m_bIsServer)
{
InitServer();
}
else
{
InitClient();
}
// 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() );
}
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();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
try
{
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)
{
System.out.println(m_strUrl);
if(m_strUrl.startsWith("btspp")) //we have found our service protocol
{
m_bServerFound = true;
m_bInitClient=true;
break;
}
}
}
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: