I am in the process of creating an Android application that communicates with the PC using a Bluetooth connection. I am attempting to send items from an ArrayList from the PC to the Android.
Asked
Active
Viewed 1,316 times
2 Answers
0
Try flushing the outputstream and Closing the outputstream. This should flush the remaining data in the buffer to the Android device. Also you need to add a new line escape character to every output (if there is not present one within the values of the ArrayList) i.e. on your PC code do something like this:
PC Code:
list = FilterFile.aList;
for(int z = 0; z < list.size(); z++) {
int a = list.size();
System.out.println("in for\n"+list.size());
String str = list.get(z);
System.out.println(str);
out.write((str+"\n").getBytes());
out.flush();
}
out.close();
Android (Client) side code:
public class ConnectedThread extends Thread {
private BluetoothSocket mmSocket;
private InputStream mmInStream = null;
public OutputStream mmOutStream = null;
private static final String TAG = "ConnectedThread";
private static final boolean D = true;
int i;
char a;
String line;
String y = "";
//DataInputStream din=null;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
//din = new DataInputStream(mmInStream);
if (D) Log.e(TAG, "-- in connected() --");
}
public void run() {
if (D) Log.e(TAG, "-- ConnectedThread --");
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
public String readList() {
String data = "";
try {
byte buff[] = new byte[1024];
int count = -1;
while((count = mmInStream.read(buff, 0, 1024)) != -1){
data += new String(buff,0,count);
}
String lines[] = data.split("\n");
for(int i = 0; i < lines.length; i++){
//this will print your individual lines...
System.out.println(lines[i]);
}
} catch (Exception e) {
///print your error message here
}
finally{
if(in != null){
try {
mmInStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return data;
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

user_CC
- 4,686
- 3
- 20
- 15
-
When I attempted this method i got the error shown in my post – user1909680 Apr 02 '13 at 15:12
-
There is no error in logs you posted.. Have a look and try to find where it shows you the exception. Also how did you know that there was an error? – user_CC Apr 02 '13 at 15:18
-
It is not an exception but it seems to be getting stuck in the while loop and eventually stops doing anything. I have updated the post with the amended code and the result – user1909680 Apr 02 '13 at 15:33
-
Any ideas on how to fix this problem? – user1909680 Apr 02 '13 at 16:22
-
Ok one step forward you are now receiving multiple lines instead of one line..are you receiving all the lines which have been sent from the PC? I have updated the PC Code in my post add this one line out.write("\n".getBytes()); and let me know what happens – user_CC Apr 02 '13 at 21:03
-
I added the two lines but this displays the same output as the one is my post with an additional empty line. The application is still stuck in this method therefore, all remaining lines of code are not executed. – user1909680 Apr 03 '13 at 12:59
-
Please see my edit. I have added the IOException that is thrown after a long wait. – user1909680 Apr 03 '13 at 13:24
-
The code on android receives all the strings and then get stuck? – user_CC Apr 03 '13 at 13:35
-
Yes. The LogCat snippet shows what is displayed. All the Strings are printed from the Android code and it gets stuck in the while loop. That is when I get the javaIO Exception – user1909680 Apr 03 '13 at 13:39
-
Which class objects are 'out' on PC Code and 'r' on android? – user_CC Apr 03 '13 at 13:41
-
'out' is an OutputStream on the PC code and 'r' is a BufferedReader on Android – user1909680 Apr 03 '13 at 13:44
-
Use DataInputStream on the android side and use the readLine method – user_CC Apr 03 '13 at 13:59
-
I have added the read() method using DataInputStream instead of BufferedReader. Is that what you meant because I still get the same Exception? – user1909680 Apr 03 '13 at 14:31
-
The problem is that you were using two different streams, BufferedReader on android side and OtuputStream on PC Side – user_CC Apr 03 '13 at 14:35
-
Use DataOutputStream on pc side and use DataInputStream on android side – user_CC Apr 03 '13 at 14:48
-
That didn't resolve the problem. Some unknown characters are printed on the Android side when the writeChars method is used. The program still freezes at the same point though. – user1909680 Apr 03 '13 at 15:15
-
I would prefer not to share them. Is there anyway of sharing them with you directly? – user1909680 Apr 03 '13 at 16:21
-
Close the socket after closing the outputstream on the pc side...This should definitely make readLine return null. – user_CC Apr 04 '13 at 08:25
-
I need to keep the socket open because I need to read from the PC InputStream once I have written to the OutputStream. – user1909680 Apr 04 '13 at 09:12
-
Yup that is what I thought, I think then we can't use readLine in this case you will have to use different streams. Revert your PC Code to use OutputStream and on the android side use the method I have just written for you see my updated answer. Enjoy – user_CC Apr 04 '13 at 09:36
-
I have updated my post with the code of the two relevant classes if you want to see if there is any issues with them. I will attempt your method later however, by looking at the code you have posted I have to call this method with an InputStream parameter which I cannot do from the class that I am calling it from. – user1909680 Apr 04 '13 at 09:53
-
ok I have updated your code to reflect my changes. Copy the class for android and update your writing method for PC Side code – user_CC Apr 04 '13 at 10:44
-
I made the changes and still have the same problem. The Android application just freezes and I receive a message saying the activity is not responding. The LogCat console displays a unusual message (see edit in post) – user1909680 Apr 04 '13 at 12:51
-
that is not the error, paste all of the logcat. Also can you place some logs in the while loop and paste all of the logs with error – user_CC Apr 04 '13 at 12:56
-
I realised that I get the JavaIOException error because I close the server side application. However, if I don't close it I get the Android app stops responding and the threadid=4... message is displayed in LogCat. See updates in my post – user1909680 Apr 04 '13 at 13:12
-
place a statement System.out.println("Data: "+data); to print the data after the while loop. Also did you commented out the DataInputStreams as per my changes.. – user_CC Apr 04 '13 at 13:26
-
I have now updated the code in my post. That application gets stuck in the while loop so does not execute the System.out.println("Data: "+data); line. I am no longer using the DataInputStream. – user1909680 Apr 04 '13 at 13:43
-
I'm not sure why this is happening. Do you think there is a problem with the ConnectedThread class? – user1909680 Apr 04 '13 at 14:16
-
I think these streams will only return -1 if the socket is closed and not when streams are closed. So what we will do is send a string from your pc code that will tell the number of items in the list and then only run the loop to get these strings. got it? – user_CC Apr 04 '13 at 14:39
-
Yes I understand what you mean? However, the loop shouldn't get stuck. And the it should keep printing "in while" constantly. But I could try your suggestion. – user1909680 Apr 04 '13 at 15:07
-
How can I implement your suggestion? – user1909680 Apr 04 '13 at 15:15
-
The loop is getting stuck because it is waiting for the end of stream character which is -1 and it only appears to come up when you close the socket. For instance lets say if you want to send something to your android client later on which outputstream will you use if you have already closed it on the PC? So that is why the eof will be sent when your socket is closed. – user_CC Apr 04 '13 at 15:25
-
However lets say you want to send 10 items from your pc so you will first send a string or integer 10 from your pc the android will read this integer and will execute a loop uptill this integer and read all the 10 items...and will not wait for eof like it is waiting for currently... – user_CC Apr 04 '13 at 15:28
-
Ok I understand. I will attempt this. – user1909680 Apr 04 '13 at 16:15
-
How can I read the ArrayList size as an int and the rest of the bytes as Strings? – user1909680 Apr 04 '13 at 16:24
-
good question use DataOutputStream and use its method writeInt() to write and integer and writeChars to write string – user_CC Apr 04 '13 at 16:30
-
similarly use readInt and readString on the other side (Android) – user_CC Apr 04 '13 at 16:32
-
So this means using DataOutputStream on PC side and DataInputStream on Android? – user1909680 Apr 04 '13 at 16:35
-
I have updated my post with the amended code and results. I have now come across a few more issues which are defined in my post. – user1909680 Apr 04 '13 at 17:16
-
Yup that is right you will have to use data streams. Your code should work but it is a bad idea to take x,s, and a as global variables where you are reading the strings. – user_CC Apr 04 '13 at 21:40
-
readList is the method which returns one string and not a list as per your method name 'readList'. Also I can see from the logcat that you are receiving the strings. – user_CC Apr 04 '13 at 21:49
-
As mentioned in my edit, the Logcat is not displaying all items from the ArrayList and the Strings are being displayed with unknown characters. How can I resolve these issues? – user1909680 Apr 05 '13 at 08:53
-
I can't see the code where you are dealing with ArrayList also you are concatenating every string in while loop that will cause it to become one line. If you place System.out.println() in while loop you will see each and every individual line. The unknown characters are because you are not using the UTF. So do something like this in the while loop s += new String(readLine().getBytes("UTF-8")); – user_CC Apr 05 '13 at 09:09
-
I tried 's = new String(din.readLine().getBytes("UTF-8"));' but that didn't resolve the problem – user1909680 Apr 05 '13 at 17:36
-1
Try to call the write
method only once (and flush/close the buffer just after calling it):
String str = "";
for(int z = 0; z < list.size(); z++) {
str += list.get(z);
}
System.out.println(str);
out.write(str.getBytes());
out.flush();
out.close();

The Good Giant
- 1,740
- 2
- 19
- 35
-
It is probably stupid but have you tried this? System.out.println(connectedThread.read().toString()); – The Good Giant Apr 02 '13 at 16:28
-
I tried this and still had the same problem. I think there is a problem with the read() method because when I print the 'line' String it prints each item in the ArrayList. Then it just seems to get stuck and Android application freezes. – user1909680 Apr 02 '13 at 16:47
-
I have edited my post so you can see the output that is displayed. – user1909680 Apr 02 '13 at 16:55
-
reading your output it seems that the readLine() method correctly works, try using a normal string instead of a stringBuilder, just init the string at the beginning of the method, and inside the for loop use yourString += ... – The Good Giant Apr 03 '13 at 07:27
-
Alternatively you can save the return value of your read() method in a StringBuffer variable so you'll be able to debug and see if it is null and/or what does it actually contain. StringBuffer sb = connectedThread.read(); String s = sb.toString(); System.out.println(s); – The Good Giant Apr 03 '13 at 07:29
-
I could do this but the program is getting stuck within the loop. The Logcat ouput in my post shows this. The following lines of code are not executed because of this. The correct information is displayed but it just seems to stop running after this. – user1909680 Apr 03 '13 at 13:14
-
Please see my edit. I have added the IOException which is thrown. It is referring to the line which is calling the read() method. – user1909680 Apr 03 '13 at 13:25
-
I suppose ConnectedThread.java:79 is (line = r.readLine()) != null ..maybe you are trying to read some strange characters.. which is the pc output? – The Good Giant Apr 03 '13 at 14:03
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27493/discussion-between-user1909680-and-the-good-giant) – user1909680 Apr 03 '13 at 14:13
-
What difference do you imagine this will make? And how will removing the newlines help when the receiver is reading lines? -1. – user207421 Apr 05 '13 at 01:15