I have met a problem when implementing communication between PC and android using BT. I have an application which works correctly on WIFI communication, using standard java Socket, and its streams. I am trying to add Bt communication. Android side code for opening streams uses standard BluetoothSocket (communication is established, and it's ok):
mOos = new ObjectOutputStream(btSocket.getOutputStream());
mOos.flush();
mOis = new ObjectInputStream(btSocket.getInputStream());
On PC side I use Bluecove 2.1.
mOos = new ObjectOutputStream(mStreamConn.openOutputStream());
mOos .flush();
mOis = new ObjectInputStream(mStreamConn.openInputStream());
Streams are initialized properly. I am sending initial message from android to PC
protected synchronized void sendAwaitingMsg() throws IOException {
Message msg;
while((msg = mOutgoingMsgQueue.poll()) != null) {
mOos.writeObject(msg);
}
mOos.flush();
}
And then try to read it on PC side
protected void getIncomingMsg() throws IOException, ClassNotFoundException {
if(mOis.available() > 0) {
Message msg = (Message)mOis.readObject();
if(msg.mControlHeader > 0) {
mKeepRunning = false;
} else {
msg.setHandlerId(mId);
mConnectionManager.acceptNewMessage(msg);
}
}
}
But mOis.available() is always 0 which means that it does not receive send message. My Message object class:
public class Message extends LinkedHashMap implements Serializable, Comparable {
static final long serialVersionUID = 10275539472837495L;
protected long mHandlerId;
protected int mType;
protected int mPriority;
public int mControlHeader = 0;
public int getType() {
return mType;
}
public void setType(int type) {
this.mType = type;
}
public long getHandlerId() {
return mHandlerId;
}
public void setHandlerId(long handlerId) {
this.mHandlerId = handlerId;
}
public int getPriority() {
return mPriority;
}
public void setPriority(int priority) {
mPriority = priority;
}
@Override
public int compareTo(Object o) {
return mPriority - ((Message)o).mPriority;
}
}
The same operations, on standard java socket, and network communication works like a charm. Where is the problem?
I had similar troubles with bluetooh communication between android and PC. I finally found some info about bluecove (implementation of bluetooth libraries in java) and a great commented example here in the following link:
http://fivedots.coe.psu.ac.th/~ad/jg/blue4/blueCoveEcho.pdf
Here there's another example but without using bluecove:
http://fivedots.coe.psu.ac.th/~ad/jg/blue1/blueEcho.pdf
I hope it helps you.