I am programming an application which connects to a Bluetooth device, then sends a message and gets an answer. This is the code I am using to get the answer :
StringBuilder res = new StringBuilder();
while ((char) (b = (byte) in.read()) != '>') {
if ((char) b != ' ') {
res.append((char) b);
}
}
rawData = res.toString().trim();
Basically, it takes every characters one by one and adds them to a StringBuilder
When there is an answer, no problem, it works. But the issue is when there is no answer from the device (because the connection was lost anyhow).
How can I put a limit on the time I am waiting for a character?
EDIT :
Here is my BlueTooth connection
final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
_dev = btAdapter.getRemoteDevice(remoteDevice);
try {
Method m;
m = _dev.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
_sock = (BluetoothSocket) m.invoke(_dev, 1);
_sock.connect();
setState(STATE_CONNECTED);
sockIn = _sock.getInputStream();
sockOut = _sock.getOutputStream();
} catch (Exception e) {
closeConnection();
}