2

编写我的第一个 Android 应用程序,它连接到我最近安装的家庭安全设备。我能够连接到设备并发送/接收数据,但是当我最初连接时,当我调用套接字 getInputStream 方法时,应用程序大约需要 3-4 秒。我认为这部分是因为它正在进行 SSL 握手。我想知道是否有什么我可以做的来加快这个速度?下面是我的代码:

KeyStore trustStore = KeyStore.getInstance("BKS");
InputStream trustStoreStream = _service.getResources().openRawResource(R.raw.mykeystore);
trustStore.load(trustStoreStream, "keystorepass".toCharArray());

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory factory = sslContext.getSocketFactory();
_socket = (SSLSocket) factory.createSocket(SERVER_IP, SERVER_PORT);
_socket.setUseClientMode(true);

if (_socket.isConnected()) {
    _nis = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
    _nos = new OutputStreamWriter(_socket.getOutputStream());

    String text;
    while((text = _nis.readLine()) != null){ //This is blocking
        messageReceived(text);
    }
}
4

0 回答 0