Witam mam mały problem, gdyż chciałbym zrobić komunikacje między kilkoma telefonami między sobą aby można było np komunikować się między sobą, mam taki oto kod klient-serwer lecz on pozwala mi komunikować się jedynie jednemu telefonowi z serwerem, jak go przerobić żeby można się było komunikować większą liczbą telefonów naraz ? Proszę o jakąkolwiek pomoc
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Farhan Hameed Khan
*/
public class BlueToothExp extends MIDlet implements CommandListener, Runnable {
/** Creates a new instance of BlueToothExp */
public BlueToothExp() {
klien=new Form("klien");
pole=new TextField("WPISZ COS","",40,TextField.ANY);
klien.append(pole);
Command ok=new Command("OK",Command.OK,0);
Command koniec=new Command("Koniec",Command.EXIT,1);
klien.addCommand(ok);
klien.addCommand(koniec);
klien.setCommandListener(this);
ekran=Display.getDisplay(this);
ekran.setCurrent(list1);
initialize();
}
String co;
private List list1;//GEN-LINE:MVDFields
private ClientServer m_BlueObj;
private boolean m_bRunThread=false;
private boolean m_bIsServer=false;
Display ekran;
Form klien;
TextField pole;
//GEN-LINE:MVDMethods
/** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
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;
ekran.setCurrent(klien);
}
// Do nothing//GEN-LINE:MVDCAAction9
// Insert post-action code here
break;//GEN-BEGIN:MVDCACase9
}
}
}
if(command.getCommandType()==Command.EXIT)
{
notifyDestroyed();
}
if(command.getCommandType()==Command.OK)
{
co=pole.getString();
m_BlueObj.SendMessages(co);
}
//GEN-END:MVDCACase9
// Insert global post-action code here
}//GEN-LINE:MVDCAEnd
/** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
*/
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
klien.append(co);
m_BlueObj.SendMessages(co);
Thread.sleep(100);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}//end while
}
//plik ClientServer
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;
/*
* ClientServer.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 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);
}
/*********************************************************************************************
* 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);
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));
// Search for Services
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);
}
}
}