3

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

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

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

4

0 回答 0