Pergunta

I have followed Google's tutorial on using Bluetooth on Android. On the server side which is a computer I've used Bluecove.

I have managed to establish the connection between the devices (they weren't paired first and when I ran the program both of the devices asked to accept the pairing). Now I have problem with sending data over the connection. In the Managing a connection section there's a method called write. With that function I'm not able to send anything. It doesn't fail (no errors) but the message doesn't reach the server.

On the server after accepting the connection (acceptAndConnect()) I have the following

DataInputStream inputStream = new DataInputStream(connection.openInputStream());

while(true) {
    String input = "";
    char c;

    while(((c = inputStream.readChar()) > 0) && (c != '\n')) {
        input += c;
    }

    System.out.println("Received: " + input);
}

It doesn't print anything.

The code on the client side is exactly the same as in the tutorial. Where is the problem?

Edit. Actually, if I print c in the server code shown up I get something strange. I've tried to send hello\n. When printing c, I get 桥 汬 漊

Foi útil?

Solução

After a little bit of more googleing I found Jon Skeet's answer and changed the DataInputStream to an InputStream wrapped in an InputStreamReader and that one in a BufferedReader. Then I can use readLine() and I get the characters right.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top