This is a tutorial for Android to do non-blocking socket communication. I am using 32feet Bluetooth library, but it should be the same if you were using other network socket communication that reply on inputstream mechanism.
In fact it is not asynchronous, however with a bit of threading magic, this system can work the way as asynchronous.
To move data from a background thread to the UI thread, use a Handler that’s running on the UI thread.
BluetoothSocketListener bsl = new BluetoothSocketListener(handler, socket, message);
Thread messageListener = new Thread(bsl);
messageListener.start();
messaging system
private XYPlot dPlot = null;
private SimpleXYSeries dSeries = null;
private int value;
public MessagePoster(XYPlot dPlot, SimpleXYSeries dSeries, int value) {
this.dPlot = dPlot;
this.dSeries = dSeries;
this.value = value;
}
public void run() {
dSeries.addLast(null, value);
dPlot.redraw();
}
}