I am trying to read the AIS serial data on an Android device over WiFi by creating a TCP connection with the AIS Transponder. I am using the EM-Trak B360 AIS transponder and creating a socket on the android device using the following code:
public class Client extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
TextView textResponse;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice: inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
} catch (UnknownHostException e) {
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
The above code works for mobile-to-mobile connection by creating a mobile hotspot. However when connecting the AIS Transponder's Wifi network it throws the I/O Exception: Software Caused Connection Abort.
08-20 17:04:39.089 13505-13534/com.hfad.slave W/System.err: java.net.SocketException: Software caused connection abort
08-20 17:04:39.090 13505-13534/com.hfad.slave W/System.err:
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:586)
at java.net.Socket.connect(Socket.java:535)
at java.net.Socket.<init>(Socket.java:427)
at java.net.Socket.<init>(Socket.java:210)
at com.hfad.slave.Client.doInBackground(Client.java:47)
at com.hfad.slave.Client.doInBackground(Client.java:16)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
I got the solution to the problem. I used telnetclient library from Apache Commons. https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetClient.html
Now i'm able to communicate with AIS transponder.
Thanks!!