This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from mvglasow/issue111
De-duplicate Bluetooth connection code, fixes DTC connection issues (#111)
- Loading branch information
Showing
3 changed files
with
70 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/github/pires/obd/reader/io/BluetoothManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.pires.obd.reader.io; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.util.UUID; | ||
|
||
import android.bluetooth.BluetoothDevice; | ||
import android.bluetooth.BluetoothSocket; | ||
import android.util.Log; | ||
|
||
public class BluetoothManager { | ||
|
||
private static final String TAG = BluetoothManager.class.getName(); | ||
/* | ||
* http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html | ||
* #createRfcommSocketToServiceRecord(java.util.UUID) | ||
* | ||
* "Hint: If you are connecting to a Bluetooth serial board then try using the | ||
* well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you | ||
* are connecting to an Android peer then please generate your own unique | ||
* UUID." | ||
*/ | ||
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); | ||
|
||
/** | ||
* @brief Instantiates a BluetoothSocket for the remote device and connects it. | ||
* <p/> | ||
* See http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3/18786701#18786701 | ||
* | ||
* @param dev The remote device to connect to | ||
* @return The BluetoothSocket | ||
* @throws IOException | ||
*/ | ||
public static BluetoothSocket connect(BluetoothDevice dev) throws IOException { | ||
BluetoothSocket sock = null; | ||
BluetoothSocket sockFallback = null; | ||
|
||
Log.d(TAG, "Starting Bluetooth connection.."); | ||
try { | ||
sock = dev.createRfcommSocketToServiceRecord(MY_UUID); | ||
sock.connect(); | ||
} catch (Exception e1) { | ||
Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1); | ||
Class<?> clazz = sock.getRemoteDevice().getClass(); | ||
Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE}; | ||
try { | ||
Method m = clazz.getMethod("createRfcommSocket", paramTypes); | ||
Object[] params = new Object[]{Integer.valueOf(1)}; | ||
sockFallback = (BluetoothSocket) m.invoke(sock.getRemoteDevice(), params); | ||
sockFallback.connect(); | ||
sock = sockFallback; | ||
} catch (Exception e2) { | ||
Log.e(TAG, "Couldn't fallback while establishing Bluetooth connection.", e2); | ||
throw new IOException(e2.getMessage()); | ||
} | ||
} | ||
return sock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters