Hello, I have setup a server on a pc and a client on a phone (6600) using java.
The code works fine, the phone searches for the serevr and sends and recives data.
However i want the server on the phone and the client on the pc. I have simply switrched
over the code however nothing happens. No exceptions are thrown, the pc find the phone (server)
but doesnt do anyuthing even though the pc opens a connection and writes to it!
The following code is running on the phone (works fine as a pc server)
public class BTServer{
private String serviceName = "BCal"; // service name
private UUID serviceUUID = new UUID("2d26618601fb47c28d9f10b8ec891363", false);
//private calenderPanel panel; // allows to write to the panel
public BTServer(){
try {
LocalDevice local =LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
}catch (BluetoothStateException e){
}
setUpServer();
}
// sets up the sever
public void setUpServer(){
// Define the server connection URL
String connection = "btspp://localhost:"+serviceUUID+";name="+serviceName;
try{
// server connection
// also registers the service record in database
StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector.open(connection, Connector.READ_WRITE);
StreamConnection client = notifier.acceptAndOpen();
// handle client
RemoteDevice rd = RemoteDevice.getRemoteDevice(client);
DataInputStream in = client.openDataInputStream();
String recived = in.readUTF();
System.out.println(recived);
recived = in.readUTF();
System.out.println(recived);
// panel.addToPanel(recived);
in.close();
}catch(IOException io){
}
}
}
The following is running on the pc (client). (it works fine on the phone as a client). I use the
xp bluetooth stack
public class BTClient{
public static void main(String [] args){
BTClient{b = new BTClient{
}
protected LocalDevice local = null;
protected DiscoveryAgent agent = null;
protected DeviceListener listener;
public BTServer(){
doDeviceDiscovery();
}
// searche for local devices
public void doDeviceDiscovery() {
try {
local = LocalDevice.getLocalDevice();
}catch (BluetoothStateException bse) {
System.out.println("error");
}
agent = local.getDiscoveryAgent();
try {
// the device listner has the device listenrs in
listener = new DeviceListener();
if(!agent.startInquiry(DiscoveryAgent.GIAC,listener)) {
// System.out.println("Inquiry not started");
System.out.println("erro 1");
}
}catch(BluetoothStateException bse) {
//System.out.println("BluetoothStateException " + bse.getMessage());
System.out.println("erro r3");
}
}
// The device and service listern
class DeviceListener implements DiscoveryListener{
protected Vector list = new Vector(); // holds the devices found in the search
public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
list.addElement(remoteDevice);
}
// the inquiry has completed
public void inquiryCompleted(int param) {
switch (param) {
case DiscoveryListener.INQUIRY_COMPLETED:
for(int i=0; i<list.size(); i++){
// remote device
RemoteDevice rd = (RemoteDevice)list.elementAt(i);
// found server, hardcoded the server phone bluetooth adress
if(rd.getBluetoothAddress().equals("000e6d51b034")){
// the uuid is the same as the one in the server
UUID [] uuid = new UUID[1];
uuid[0] = new UUID("2d26618601fb47c28d9f10b8ec891363",false);
try{
// *CARRY OUT A SERVICE SEARCH ON THE REMOTE DEVICE (E.G IF
// BLUETOOTH ADDRESS IS 000e6d51b034 I.E. THE SERVER)
// *
// * AS A RESULT THERE WILL ONLY BE ONE SERVICE FOUND IN
// * THE SERVICEDISCOVERED METHOD
// *
agent.searchServices(null,uuid,rd,this);
}catch(BluetoothStateException bse){
// Calender.numberFound.setText("Error when search began");
System.out.println("error 3");
}
}
}
break;
case DiscoveryListener.INQUIRY_ERROR:
// System.out.println("Inquiry error");
// synMemo.setText("inquiry error");
System.out.println("error");
break;
case DiscoveryListener.INQUIRY_TERMINATED:
// System.out.println("Inquity completed");
System.out.println("error");
break;
}
}
public void servicesDiscovered(int transID,ServiceRecord[] serviceRecord) {
/*
IS THE PROBLEM HERE? , HOWEVER I AM SEARCHING WITH THE UUID, THERE SAME AS THE ONE IN THE SERVER
*/
ServiceRecord sr = (ServiceRecord)serviceRecord[transID];
String connURL = sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
try{
StreamConnection sc = (StreamConnection) Connector.open(connURL);
DataOutputStream out = sc.openDataOutputStream();
out.writeUTF("end");
System.out.println("written");
out.close();
}catch(IOException io){
// synMemo.setText("ioerror");
System.out.println("IO ERROR, MAIN");
}
}
public void serviceSearchCompleted(int transID, int respCode) {
}
}
tHATS IT, THE FUNNY think is that the above code fully excutes e.g. outputs end!
Its just the server on the phone doesnt "StreamConnection client = notifier.acceptAndOpen();"
But no erroe are thrown,
Any help will be great.
Thanks alot
Biran

Reply With Quote

