I'm struggling with some problem with my Bluetooth App. The App is connected to the OBD2 interface via Bluetooth and my goal is to receive some data from my car like Speed, RPM etc.
I made an app which has a button And when I click it I send a message to OBD2 interface and I get response. But my goal is to receive this data continuously. So I figured out that I can do that inside the
onResume()
method. Below is simple code that sends message.
@Override
protected void onResume() {
super.onResume();
HandlerDelay.postDelayed(new Runnable() {
@Override
public void run() {
checkSPDstatus();
}
},500);
}
private void checkSPDstatus(){
if (mmSocket != null){
try {
mmSocket.getOutputStream().write("010C\r".getBytes());
} catch (IOException e) {
Log.e("Status", String.valueOf(e));
}
}
}
But using that my app crashes and can't even connect with BT device.. To connect my app via BT Im using AsyncTask. I will be grateful for any help.
Regards Matt.
Ok I figured out how to do that. I used Service to connect with Bluetooth Device instead AsyncTask. I used two Handlers. Now everything works. I can send String message in every 500ms and also I can get response from OBD2 Interface. Below is code for Service Class. I used BroadcastReceiver to receive data from Service to my UI.