Skip to content

Instantly share code, notes, and snippets.

@sankarganesh
Created August 29, 2013 09:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarganesh/6376039 to your computer and use it in GitHub Desktop.
Save sankarganesh/6376039 to your computer and use it in GitHub Desktop.
package com.example.obddemo;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.bluetooth.BluetoothSocket;
public class BluetoothSocketListener implements Runnable {
InputStream inStream = null;
private BluetoothSocket btSocketConnected = null;
@SuppressWarnings("rawtypes")
Class MessagePasser = null;
Method methodObject = null;
Method secondaryMethodObject = null;
String invokeMethodName = null;
Class[] paramString = null;
Object object = null;
public BluetoothSocketListener(BluetoothSocket socket,
String paramClassName, String methodName) {
this.btSocketConnected = socket;
paramString = new Class[1];
paramString[0] = String.class;
try {
MessagePasser = Class.forName(paramClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
this.invokeMethodName = methodName;
}
@SuppressWarnings("unchecked")
@Override
public void run() {
while (true) {
try {
inStream = btSocketConnected.getInputStream();
String str1 = "";
char c = (char) inStream.read();
str1 = str1 + c;
if (c == '>') {
// String str1 = "";
// str1 = str1 + "410C1231";
String datafromOBD = str1.substring(0, -2 + str1.length())
.replaceAll(" ", "").trim();
try {
secondaryMethodObject = MessagePasser.getMethod(
invokeMethodName, paramString);
secondaryMethodObject.invoke(object, datafromOBD);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
} catch (IOException localIOException) {
localIOException.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment