Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

RFCOMM client can't reconnect to server

$
0
0
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.

Viewing all articles
Browse latest Browse all 51036

Trending Articles