Skip to content
This repository was archived by the owner on Nov 10, 2017. It is now read-only.

Commit 55d26d6

Browse files
committed
Merge pull request #156 from mvglasow/issue111
De-duplicate Bluetooth connection code, fixes DTC connection issues (#111)
2 parents 4e8ee3d + 6eb6e4b commit 55d26d6

File tree

3 files changed

+70
-39
lines changed

3 files changed

+70
-39
lines changed

src/main/java/com/github/pires/obd/reader/activity/TroubleCodesActivity.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
import android.widget.ListView;
2323
import android.widget.Toast;
2424

25-
2625
import com.github.pires.obd.commands.control.TroubleCodesCommand;
2726
import com.github.pires.obd.commands.protocol.EchoOffCommand;
2827
import com.github.pires.obd.commands.protocol.LineFeedOffCommand;
2928
import com.github.pires.obd.commands.protocol.ObdResetCommand;
30-
3129
import com.github.pires.obd.commands.protocol.ResetTroubleCodesCommand;
3230
import com.github.pires.obd.commands.protocol.SelectProtocolCommand;
3331
import com.github.pires.obd.enums.ObdProtocols;
3432
import com.github.pires.obd.exceptions.MisunderstoodCommandException;
3533
import com.github.pires.obd.exceptions.NoDataException;
3634
import com.github.pires.obd.exceptions.UnableToConnectException;
3735
import com.github.pires.obd.reader.R;
36+
import com.github.pires.obd.reader.io.BluetoothManager;
3837
import com.google.inject.Inject;
3938

4039
import java.io.IOException;
40+
import java.lang.reflect.Method;
4141
import java.util.ArrayList;
4242
import java.util.HashMap;
4343
import java.util.Map;
@@ -46,8 +46,6 @@
4646
public class TroubleCodesActivity extends Activity {
4747

4848
private static final String TAG = TroubleCodesActivity.class.getName();
49-
private static final UUID MY_UUID = UUID
50-
.fromString("00001101-0000-1000-8000-00805F9B34FB");
5149
private static final int NO_BLUETOOTH_DEVICE_SELECTED = 0;
5250
private static final int CANNOT_CONNECT_TO_DEVICE = 1;
5351
private static final int NO_DATA = 3;
@@ -118,7 +116,7 @@ public boolean handleMessage(Message msg) {
118116
return false;
119117
}
120118
});
121-
119+
122120
@Override
123121
protected void onCreate(Bundle savedInstanceState) {
124122
super.onCreate(savedInstanceState);
@@ -154,9 +152,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
154152
switch (item.getItemId()) {
155153
case R.id.action_clear_codes:
156154
try {
157-
sock = dev.createRfcommSocketToServiceRecord(MY_UUID);
158-
sock.connect();
159-
155+
sock = BluetoothManager.connect(dev);
160156
} catch (Exception e) {
161157
Log.e(
162158
TAG,
@@ -294,9 +290,7 @@ protected String doInBackground(String... params) {
294290

295291
// Instantiate a BluetoothSocket for the remote device and connect it.
296292
try {
297-
sock = dev.createRfcommSocketToServiceRecord(MY_UUID);
298-
sock.connect();
299-
293+
sock = BluetoothManager.connect(dev);
300294
} catch (Exception e) {
301295
Log.e(
302296
TAG,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.pires.obd.reader.io;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.Method;
5+
import java.util.UUID;
6+
7+
import android.bluetooth.BluetoothDevice;
8+
import android.bluetooth.BluetoothSocket;
9+
import android.util.Log;
10+
11+
public class BluetoothManager {
12+
13+
private static final String TAG = BluetoothManager.class.getName();
14+
/*
15+
* http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html
16+
* #createRfcommSocketToServiceRecord(java.util.UUID)
17+
*
18+
* "Hint: If you are connecting to a Bluetooth serial board then try using the
19+
* well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you
20+
* are connecting to an Android peer then please generate your own unique
21+
* UUID."
22+
*/
23+
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
24+
25+
/**
26+
* @brief Instantiates a BluetoothSocket for the remote device and connects it.
27+
* <p/>
28+
* See http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3/18786701#18786701
29+
*
30+
* @param dev The remote device to connect to
31+
* @return The BluetoothSocket
32+
* @throws IOException
33+
*/
34+
public static BluetoothSocket connect(BluetoothDevice dev) throws IOException {
35+
BluetoothSocket sock = null;
36+
BluetoothSocket sockFallback = null;
37+
38+
Log.d(TAG, "Starting Bluetooth connection..");
39+
try {
40+
sock = dev.createRfcommSocketToServiceRecord(MY_UUID);
41+
sock.connect();
42+
} catch (Exception e1) {
43+
Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1);
44+
Class<?> clazz = sock.getRemoteDevice().getClass();
45+
Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};
46+
try {
47+
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
48+
Object[] params = new Object[]{Integer.valueOf(1)};
49+
sockFallback = (BluetoothSocket) m.invoke(sock.getRemoteDevice(), params);
50+
sockFallback.connect();
51+
sock = sockFallback;
52+
} catch (Exception e2) {
53+
Log.e(TAG, "Couldn't fallback while establishing Bluetooth connection.", e2);
54+
throw new IOException(e2.getMessage());
55+
}
56+
}
57+
return sock;
58+
}
59+
}

src/main/java/com/github/pires/obd/reader/io/ObdGatewayService.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.github.pires.obd.reader.R;
2727
import com.github.pires.obd.reader.activity.ConfigActivity;
2828
import com.github.pires.obd.reader.activity.MainActivity;
29+
import com.github.pires.obd.reader.io.BluetoothManager;
2930
import com.github.pires.obd.reader.io.ObdCommandJob.ObdCommandJobState;
3031
import com.google.inject.Inject;
3132

@@ -46,16 +47,6 @@
4647
public class ObdGatewayService extends AbstractGatewayService {
4748

4849
private static final String TAG = ObdGatewayService.class.getName();
49-
/*
50-
* http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html
51-
* #createRfcommSocketToServiceRecord(java.util.UUID)
52-
*
53-
* "Hint: If you are connecting to a Bluetooth serial board then try using the
54-
* well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you
55-
* are connecting to an Android peer then please generate your own unique
56-
* UUID."
57-
*/
58-
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
5950
private final IBinder binder = new ObdGatewayServiceBinder();
6051
@Inject
6152
SharedPreferences prefs;
@@ -139,24 +130,11 @@ private void startObdConnection() throws IOException {
139130
Log.d(TAG, "Starting OBD connection..");
140131
isRunning = true;
141132
try {
142-
// Instantiate a BluetoothSocket for the remote device and connect it.
143-
sock = dev.createRfcommSocketToServiceRecord(MY_UUID);
144-
sock.connect();
145-
} catch (Exception e1) {
146-
Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1);
147-
Class<?> clazz = sock.getRemoteDevice().getClass();
148-
Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};
149-
try {
150-
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
151-
Object[] params = new Object[]{Integer.valueOf(1)};
152-
sockFallback = (BluetoothSocket) m.invoke(sock.getRemoteDevice(), params);
153-
sockFallback.connect();
154-
sock = sockFallback;
155-
} catch (Exception e2) {
156-
Log.e(TAG, "Couldn't fallback while establishing Bluetooth connection. Stopping app..", e2);
157-
stopService();
158-
throw new IOException();
159-
}
133+
sock = BluetoothManager.connect(dev);
134+
} catch (Exception e2) {
135+
Log.e(TAG, "There was an error while establishing Bluetooth connection. Stopping app..", e2);
136+
stopService();
137+
throw new IOException();
160138
}
161139

162140
// Let's configure the connection.

0 commit comments

Comments
 (0)