From 8ddc6a656d913a6b1874e83aa6946665684d5a8e Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 27 Jul 2015 17:22:54 +1000 Subject: [PATCH] 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. --- .../fdroid/net/bluetooth/BluetoothClient.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/net/bluetooth/BluetoothClient.java b/F-Droid/src/org/fdroid/fdroid/net/bluetooth/BluetoothClient.java index 21308b0f2..b488f552e 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/bluetooth/BluetoothClient.java +++ b/F-Droid/src/org/fdroid/fdroid/net/bluetooth/BluetoothClient.java @@ -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. + } } }