3

I am developing andorid aplication for connection to ELM327 for car unit trought Wifi (my adapter: http://www.obdinnovations.com/mini-vgate-elm327-wifi-obd2-auto-diagnostics-scanner). How should I connect to this OBD2 adapter and then send some signals?

OutputStream outStream = null;
InputStream inStream = null;
Socket socket = null;
InetAddress serverAddr = null;
String serverProtocol = "http";
String serverIpAddress = "192.168.0.10";
public static final int SERVERPORT = 35000;

try {
    serverAddr = InetAddress.getByName(serverIpAddress);
    socket = new Socket(serverAddr, SERVERPORT);
    socket.setKeepAlive(true);
} catch (UnknownHostException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

sendDataToOBD(socket1, "ATZ\r");    
Log.e("OBD: ATZ", readDataFromOBD(socket));

public void sendDataToOBD(Socket socket1, String paramString) {
    try {
        outStream = socket1.getOutputStream();
        byte[] arrayOfBytes = paramString.getBytes();
        outStream.write(arrayOfBytes);
    } catch (Exception localIOException1) {
        localIOException1.printStackTrace();
    }
}

public String readDataFromOBD(Socket socket1) {
    while (true) {
        try {
            inStream = socket1.getInputStream();
            String str1 = "";
            char c = (char) inStream.read();
            str1 = str1 + c;
            if (c == '>') {
                String datafromOBD = str1.substring(0, -2 + str1.length()).replaceAll(" ", "").trim();

                return datafromOBD;
            }
        } catch (IOException localIOException) {
            return localIOException.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I try also with:

URL url = null;
try {
    url = new URL(serverProtocol, serverIpAddress, SERVERPORT, "");
} catch (MalformedURLException e) {
    e.printStackTrace();
}
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
} catch (IOException e) {
    e.printStackTrace();
}

And methods param was HttpURLConnection connection instead of Socket socket1.

But I can't receive any signal. What's wrong with my code? Any suggestions?

Tom
  • 51
  • 3

0 Answers0