Hello everyone ..
i am doing some project that allow me to send messages from mobile phone to PC (server) using Bluetooth:
and i can send from client (mobile) to server but i got a problem to send from server to client i hope some one can help :
This is my code :
Server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.bluetooth.*;
import javax.microedition.io.*;
/**
* Class SampleSPPServer
*
* @author ahmed
*/
public class SampleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
boolean exit=false;
while(!exit){
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//read string from spp client
InputStream inStream=connection.openInputStream();
inStream=connection.openInputStream();
String lineRead="";
//BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
//String lineRead=bReader.readLine();
//System.out.println(lineRead);
OutputStream outStream=connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.println("Start");
pWriter.flush();
pWriter.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(lineRead==null || !lineRead.equals("Quit")){
if(inStream.available()>0){
int l=inStream.read();
System.out.println(new String());
}
lineRead=in.readLine();
if(lineRead!=null){
pWriter.println(lineRead);
pWriter.flush();
}
exit=true;
pWriter.flush();
pWriter.close();
streamConnNotifier.close();
}
}
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SampleSPPServer sampleSPPServer=new SampleSPPServer();
sampleSPPServer.startServer();
}
}
Client:
package hello;
import java.io.*;
import java.util.Vector;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author ahmed
*/
public class HelloMIDlet extends MIDlet implements CommandListener {
SampleSPPClient client = null;
DiscoveryAgent agent = null;
private boolean midletPaused = false;
//public static Object lock = new Object();
////vector containing the devices discovered
//public static Vector vecDevices = new Vector();
//public static String connectionURL = null;
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
if (nextDisplayable == getForm()) {
try {
client = new SampleSPPClient();
agent = client.localDevice.getDiscoveryAgent();
System.out.println("Address: " + client.getAddr());
System.out.println("Name: " + client.getName());
stringItem1.setText("\nINFO :\n Name :" + client.getName() + "\n Address :\n" + client.getAddr());
stringItem1.setText(stringItem1.getText() + "\nSTATUS :\nStarting device inquiry...");
///stringItem1.setText(client.getName());
System.out.println("Starting device inquiry...");
agent.startInquiry(DiscoveryAgent.GIAC, client);
try {
synchronized (client.lock) {
client.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Device Inquiry Completed. ");
stringItem2.setLabel("Result");
stringItem1.setText(stringItem1.getText() + "\nDevice Inquiry Completed. ");
int deviceCount = client.vecDevices.size();
if (deviceCount <= 0) {
System.out.println("No Devices Found .");
stringItem2.setLabel(stringItem2.getLabel() + ": Not Found");
stringItem2.setText("No Devices Found .");
} else {
stringItem2.setLabel(stringItem2.getLabel() + ": Found");
stringItem2.setText("The devices");
for (int i = 0; i < deviceCount; i++) {
RemoteDevice remoteDevice = (RemoteDevice) client.vecDevices.elementAt(i);
System.out.println((i + 1) + ". " + remoteDevice.getBluetoothAddress() + " (" + remoteDevice.getFriendlyName(true) +")");
stringItem2.setText(stringItem2.getText() + "\n(" + (i + 1) + ") . " + remoteDevice.getBluetoothAddress() + " (" + remoteDevice.getFriendlyName(true) + ")");
}
}
} catch (BluetoothStateException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
//okCommand = new Command("STOP", Command.STOP, 0);
} else if (nextDisplayable == form1) { //see what form and add the code here
//read response
InputStream inStream = null;
try {
System.out.println("The number is "+textField1.getString());
String chosenIndex = textField1.getString();
int index = Integer.parseInt(chosenIndex.trim());
RemoteDevice remoteDevice = (RemoteDevice) client.vecDevices.elementAt(index - 1);
UUID[] uuidSet = new UUID[1];
uuidSet[0] = new UUID("1101", true);
System.out.println("\nSearching for service...");
agent.searchServices(null, uuidSet, remoteDevice, client);
//list.append("service search at :" +remoteDevice.getBluetoothAddress(), null);//////////////ADD///////////
System.out.println("\nt test");
try {
synchronized (client.lock) {
client.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (client.connectionURL == null) {
System.out.println("Device does not support Simple SPP Service.");
System.exit(0);
}
//connect to the server and send a line of text
StreamConnection streamConnection = (StreamConnection) Connector.open(client.connectionURL);
//send string
OutputStream outStream=streamConnection.openOutputStream();
OutputStreamWriter pWriter= new OutputStreamWriter(outStream);
pWriter.write(textField2.getString()+ "\r\n");
pWriter.flush();
pWriter.close(); //Added
//read response
inStream = streamConnection.openInputStream();
InputStreamReader bReader2 = new InputStreamReader(inStream);
int c;
StringBuffer str = new StringBuffer();
while((c=bReader2.read()) !=-1){
str.append((char)c);
}
// list.append("Server Response :" + str.toString(), null);
} catch (IOException ex) {
System.out.println("Error "+ex.getMessage());
ex.printStackTrace();
} finally {
try {
inStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I hope i can get the help from this great web side , because i need it urgent pleasssssssse