8 Replies - 4091 Views - Last Post: 02 February 2013 - 08:11 AM

#1 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

RFCOMM client can't reconnect to server

Posted 25 January 2013 - 11:32 PM

Dear all,
I am new to j2me programming and i have a problem with my code. I was succesfully created a j2me application that act as RFCOMM Client. My problem is when i close connection to the server (three times), I couldn't reconnect my application to the server again and i got exception : "Can't Create RFCOMM Connection".

This is code on my thread:

import java.io.*;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class way implements Runnable
{
   public StreamConnection connection;
   public OutputStream output;
   public InputStream input;
   public OutputStreamWriter writer;
   public InputStreamReader reader;
   public DiscoveryAgent agent;

   public LocalDevice device;
   public String url;
   public String address;
   public String name;
   public boolean connected = false;
   public boolean reconnect = true;
   public String exeption = "";
   public String e = "";
   Main main;                 // "Main" is my midlet
   Rcvthread rcvthread;       // "Rcvthread" is my thread for receive data from the server

   public way(Main m)
   {
      this.main = m;
      this.rcvthread = m.rcv;
   }

   public void refresh()
   {
       reconnect = true;
   }

   public void run() 
   {
       initialis();
       connect();
       while(connected == true)
       {
        if(!e.equals(""))
        {
            try 
            {
                writer.write(e);
                e = "";
            } 
            catch (IOException ex) 
            {
                main.label1.setText("Error, cann't sending !");
                connect();
            }
        }
        try 
        {
            Thread.sleep(100);
        } 
        catch (InterruptedException ex) 
        {
            //
        }
      }
   }

public void initialis()
{
    try 
    {
        device = LocalDevice.getLocalDevice();
        url = "000002256500";
        connected = false;
    } 
    catch (BluetoothStateException ex) 
    {
        //
    }
}

public synchronized void connect()
{
    main.label1.setText("Connecting......");
    try
    {
        connection = (StreamConnection)Connector.open("btspp://" + url + ":1", Connector.READ_WRITE, true);

        output = connection.openOutputStream();
        input = connection.openInputStream();
        writer = new OutputStreamWriter(output);
        connected = true;
        main.label1.setText("*** Connection Success ***");
        main.startReceive();
        reconnect = false;
    }
    catch (IOException ex)
    {
        main.label1.setText(ex.getMessage());           // I get exception here after three times 
                                                        //  reconnecting to the server
        connected = false;
        main.stopReceive();
    }
}

public synchronized void disconnect()
{
    main.stopReceive();
    if(writer != null)
    {
        try
        {
            writer.close();
            writer.flush();
        }
        catch (IOException ex)
        { 
        }
    }
    if(input != null)
    {
        try
        {
            input.close();
        }
        catch (IOException ex)
        { 
        }
    }
    if(output != null)
    {
        try
        {
            output.close();
            output.flush();
        }
        catch (IOException ex)
        { 
        }
    }
    try
    {
        connection.close();
    }
    catch (IOException ex)
    { 
    }
    connected = false;
    Thread current = new Thread(this);
    current.interrupt();
  }
}



Why i can't reconnect to the server again after connection has been disconnected?
Please help me, thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: RFCOMM client can't reconnect to server

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: RFCOMM client can't reconnect to server

Posted 27 January 2013 - 10:23 AM

Don't ignore your Exceptions. Always printStackTrace() to see if you get Exceptions.
Was This Post Helpful? 0
  • +
  • -

#3 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

Re: RFCOMM client can't reconnect to server

Posted 27 January 2013 - 01:36 PM

Thanks a lot macosxnerd101, after put printStackTrace() i got this on my emulator :
javax.bluetooth.BluetoothConnectionException: IO error in btspp::connect\n
	at com.sun.jsr082.bluetooth.btspp.BTSPPConnectionImpl.doOpen(), bci=80
	at com.sun.jsr082.bluetooth.btspp.BTSPPConnectionImpl.<init>(), bci=59
	at com.sun.jsr082.bluetooth.btspp.BTSPPConnectionImpl.<init>(), bci=4
	at com.sun.jsr082.bluetooth.btspp.Protocol.clientConnection(), bci=19
	at com.sun.jsr082.bluetooth.BluetoothProtocol.openPrimImpl(), bci=52
	at com.sun.jsr082.bluetooth.BluetoothProtocol.openPrim(), bci=11
	at com.sun.midp.io.j2me.btspp.Protocol.openPrim(), bci=4
	at javax.microedition.io.Connector.open(), bci=47
 - way.connect(way.java:86)
 - way.run(way.java:43)
	at java.lang.Thread.run(Thread.java:721)


Was This Post Helpful? 0
  • +
  • -

#4 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

Re: RFCOMM client can't reconnect to server

Posted 27 January 2013 - 04:08 PM

Okay, my exeptions above are when i run my app on emulator. Thats exceptions occured because (may be) my emulator can't access bluetooth on my computer.

If i run the app on my phone, i get this Exception :

javax.bluetooth.BluetoothConnectionException: Can not create RFCOMM connection



please help
Was This Post Helpful? 0
  • +
  • -

#5 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

Re: RFCOMM client can't reconnect to server

Posted 28 January 2013 - 06:58 AM

:surrender: :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: RFCOMM client can't reconnect to server

Posted 29 January 2013 - 08:13 AM

Please avoid bumping your thread if you don't have anything new to add.

Let's do a little debugging. Are you sure you closed your Connection? Has anything been left open (a Writer, a Connection, etc.) when you move away from the app? I don't have a Java ME compatible phone to test on. Where does the Exception trace back to in your code?
Was This Post Helpful? 0
  • +
  • -

#7 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

Re: RFCOMM client can't reconnect to server

Posted 31 January 2013 - 02:48 AM

I am so sorry for that.

Quote

Let's do a little debugging. Are you sure you closed your Connection? Has anything been left open (a Writer, a Connection, etc.) when you move away from the app?


Thank you so much macosxnerd101 for you remain me do a little debugging.
I was insert some code to see the state of my connection after try to disconnecting my connection and before my midlet closed.
these are the result:
writer stay opened
input was closed
output was closed
connection was closed


I was try to move the
    if(writer != null)
    {
        try
        {
            writer.close();
            writer.flush();
        }
        catch (IOException ex)
        { 
        }
    }


after closing input and output. But StreamWriter stay left opened. And so i try to interupt the thread and the result does not changed.

how should i closing the StreamWriter?
Sorry for my english.
Thanks in advanced.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: RFCOMM client can't reconnect to server

Posted 01 February 2013 - 05:58 PM

Just to note- flushing your Writer will occur when you close it, and you can't flush() after closing. Also, don't ignore your Exceptions when you catch them. They have valuable information. Use the printStackTrace() method to see what the problem is.
Was This Post Helpful? 0
  • +
  • -

#9 H_Dev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-January 13

Re: RFCOMM client can't reconnect to server

Posted 02 February 2013 - 08:11 AM

Quote

Just to note- flushing your Writer will occur when you close it, and you can't flush() after closing.


As i understand you, we can not flush() after closing. Am i right?

I was change my code and seem to work properly.
if(writer != null)
{
    try
    {
        writer.flush();
        writer.close();
        main.label1.setText("writer was closed");
    }
    catch (IOException ex)
    { 
        main.label1.setText("writer stay opened : " + ex.toString());
        ex.printStackTrace();
    }
}


And the result is
writer was closed


Now all my connection seem to be closed, and i can reconnect again to the server even if i not close my midlet.

But when i close and reopen my midlet (on third time), i still get exceptions :
javax.bluetooth.BluetoothConnectionException: Can not create RFCOMM connection

Why ?
I would like to try to connect to my server via my emulator but i can't find reference for that.
I use NetBeans 7.1

Sorry for my long questions. Thanks in advanced.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1