如何在bluecove中重新启动蓝牙服务?

时间:2013-04-18 04:57:16

标签: java sockets bluetooth bluecove

我有桌面和Android应用程序,通过蓝牙连接(在桌面端我使用Bluecove 2.1.1库)。桌面应用程序创建蓝牙服务然后android应用程序连接到它。我想从桌面和Android端添加注销功能。例如,在桌面应用程序用户单击断开连接时,桌面和Android应用程序都会重置其连接,并且应该能够再次连接。这是桌面端的bluetoothService代码:

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();
        }
    }
}

重启服务我做:

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

但似乎我无法重新连接。也许我应该创建不同名称的服务?

0 个答案:

没有答案
相关问题