My code runs only once! properly
Please give me suggestion about why this code's not working continuously.
Client Code:
import java.io.IOException;
import java.io.OutputStream;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class BTclient{
StreamConnection conn;
public String sendTopc(String s1)
{
String done = null;
try{
//set up the bluetooth connection:
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent agent = local.getDiscoveryAgent();
String connString = agent.selectService(new UUID("6040", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if(connString != null){
try {
conn = (StreamConnection) Connector.open(connString);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
OutputStream os = conn.openOutputStream();
os.write(s1.getBytes());
os.flush();
conn.close();
done="Message sent to PC";
}
catch(IOException e)
{
e.printStackTrace();
}
} else {
//show the user an error if it occurs
done="Unable to locate service";
}
} catch (BluetoothStateException e){
//show the user an error if it occurs
done="Problem in the connection";
}
return(done);
}
}
Server Code
package server;
import java.awt.BorderLayout;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
public class Server {
public static final String UUID_STRING = "6040";
public static final String SERVICE_NAME = "BTCLIENT";
private StreamConnectionNotifier server;
JFrame jframe;
JTextArea textArea;
public static void main(String[] args) {
Server svr = new Server();
svr.doWork();
}
public void doWork() {
this.jframe = new JFrame("BT Server");
this.jframe.setLayout(new BorderLayout());
this.textArea = new JTextArea(6, 20);
JScrollPane jsp = new JScrollPane(this.textArea);
this.jframe.add(jsp, BorderLayout.CENTER);
this.jframe.pack();
this.jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.jframe.setVisible(true);
startServer();
}
public void logMessage(String message) {
this.textArea.setText(this.textArea.getText() + message + "\n");
this.textArea.setCaretPosition(this.textArea.getText().length());
}
public void startServer() {
LocalDevice local;
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
//this.logMessage("max of "+ LocalDevice.getProperty("bluetooth.connected.devices.max")+ " connection(s) supported");
String url = "btspp://localhost:" + UUID_STRING + ";name="+ SERVICE_NAME;
server = (StreamConnectionNotifier) Connector.open(url);
this.logMessage("waiting for connection...");
StreamConnection conn = server.acceptAndOpen();
this.logMessage("connection opened");
InputStream is = conn.openInputStream();
byte buffer[] = new byte[100];
while (true) {
int numChars = is.read(buffer);
String s = new String(buffer);
logMessage("received from mobile phone: " + s.substring(0, numChars));
conn.close();
startServer();
}
} catch (Exception e) {
this.logMessage(e.getMessage());
}
}
}

Reply With Quote

