3

I have desktop and android applications, which connected by bluetooth(in desktop side I use Bluecove 2.1.1 library). Desktop application create bluetooth service then android application connects to it. I want to add logout functionality from both desktop and android sides. For example in desktop app user click disconnect, both desktop and android apps reset their connections and should be able to connect again. Here is bluetoothService code for desktop side:

public class BluetoothService
{
    private static final String serviceName = "btspp://localhost:"
            // + new UUID("0000110100001000800000805F9B34F7", false).toString()
            // + new UUID("0000110100001000800000805F9B34F8", false).toString()
            + new UUID("0000110100001000800000805F9B34F9", false).toString()
            + ";name=serviceName";
    private StreamConnectionNotifier m_service = null;
    private ListenerThread m_listenerThread;
    private DataOutputStream m_outStream;

    public BluetoothService()
    {
        Open();
    }

    public void Open()
    {
        try
        {
            assert (m_service == null);
            m_service = (StreamConnectionNotifier) Connector.open(serviceName);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void Start()
    {
        try
        {
            StreamConnection connection = (StreamConnection) m_service
                    .acceptAndOpen();
            System.out.println("Connected");

            m_listenerThread = new ListenerThread(connection);
            Thread listener = new Thread(m_listenerThread);
            listener.start();
            m_outStream = new DataOutputStream(connection.openOutputStream());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void Send(String message)
    {
        assert (m_listenerThread != null);
        try
        {
            m_outStream.writeUTF(message);
            m_outStream.flush();
            System.out.println("Sent: " + message);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void Close()
    {
        try
        {
            m_service.close();
            m_listenerThread.Stop();
            m_listenerThread = null;
            m_outStream.close();
            m_outStream = null;
            m_service = null;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

class ListenerThread implements Runnable
{
    private DataInputStream m_inStream;
    private boolean m_isRunning;
    public ListenerThread(StreamConnection connection)
    {
        try
        {
            this.m_inStream = new DataInputStream(connection.openInputStream());
            m_isRunning = true;
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ;
    }

    public void run()
    {
        while (m_isRunning)
        {
            try
            {
                assert (m_inStream != null);
                if (m_inStream.available() > 0)
                {
                    String message = m_inStream.readUTF();
                    System.out.println("Received command: " + message);
                    CommandManager.getInstance().Parse(message);
                }
            }
            catch (IOException e)
            {
                System.err.println(e.toString());
            }
        }
    }

    public void Stop()
    {
        m_isRunning = false;
        try
        {
            m_inStream.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

for restarting service I do:

    BluetoothService::Close();
    BluetoothService::Open();
    BluetoothService::Start();

but seems I cannot reconnect. Maybe I should create service with different name?

IKM2007
  • 716
  • 1
  • 8
  • 31

0 Answers0