×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    Bluetooth server and client

    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
    Last edited by ug94bxs; 2006-12-30 at 16:44.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    369

    Re: Bluetooth server and client

    how do u know no errors are thrown?
    look at your server code..
    "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 catch is empty and since its not the phone that u are running the server you wont see any stack trace or the app wont close since its in try/catch block

    so place some debug msgs there and only then you can say whats realy going on..

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    Re: Bluetooth server and client

    hello, i have alread done that, i removes all output statemensts to reduce the amount of code,

    why would there be a prblem when switching the code over?

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    369

    Re: Bluetooth server and client

    have you done somthing in the catch clause???
    if so print that exception msg to the screen and write it here...

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    Re: Bluetooth server and client

    hey, its ok i sorted it out. the connection url was wrong

    chers

Similar Threads

  1. Question about bluetooth application (one client and server in the same device)
    By gonzi in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2008-07-24, 12:01
  2. J2ME client to C# server over Bluetooth?
    By Global_Inferno in forum Mobile Java Networking & Messaging & Security
    Replies: 17
    Last Post: 2007-03-26, 12:34
  3. Problem in reconnection from client to server
    By Palermo4Ever in forum Bluetooth Technology
    Replies: 0
    Last Post: 2006-06-19, 16:27
  4. Send multiple message from client to server
    By Palermo4Ever in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-06-19, 11:49
  5. Bluetooth client problem on games!
    By wmchan78 in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2006-03-22, 06:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×