Failed attempt at fixing bluetooth timeout error.

Kept getting a message in the Bluetooth _client_, which said the
socket timed out or received an invalid response or something. Most
people seem to believe that it is due to a bug in the Bluetooth stack
on Android 4.2.1 and suggest using reflection to access a method that
actually works as intented. I couldn't get it to work correctly though.
Kept the code here because this whole branch is a WIP, and we need
to figure out how to make Bluetooth work one way or another.
This commit is contained in:
Peter Serwylo 2015-07-27 17:22:54 +10:00
parent c57f7105e1
commit 8ddc6a656d

View File

@ -3,8 +3,11 @@ package org.fdroid.fdroid.net.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BluetoothClient {
@ -22,10 +25,36 @@ public class BluetoothClient {
}
public BluetoothConnection openConnection() throws IOException {
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(BluetoothConstants.fdroidUuid());
BluetoothConnection connection = new BluetoothConnection(socket);
connection.open();
return connection;
BluetoothSocket socket = null;
try {
socket = device.createInsecureRfcommSocketToServiceRecord(BluetoothConstants.fdroidUuid());
BluetoothConnection connection = new BluetoothConnection(socket);
connection.open();
return connection;
} catch (IOException e1) {
Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back to using reflection...");
Class<?> clazz = socket.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};
Method method;
try {
method = clazz.getMethod("createInsecureRfcommSocket", paramTypes);
Object[] params = new Object[]{1};
BluetoothSocket sockFallback = (BluetoothSocket) method.invoke(socket.getRemoteDevice(), params);
BluetoothConnection connection = new BluetoothConnection(sockFallback);
connection.open();
return connection;
} catch (NoSuchMethodException e) {
throw e1;
} catch (IllegalAccessException e) {
throw e1;
} catch (InvocationTargetException e) {
throw e1;
}
// Don't catch exceptions this time, let it bubble up as we did our best but don't
// have anythign else to offer in terms of resolving the problem right now.
}
}
}