From e8a4d778646f7c1187c3f3cd962bd41688a0cb45 Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Tue, 13 Nov 2012 15:44:49 +0000 Subject: [PATCH] Android Bluetooth code cleanup. --- .../plugins/droidtooth/DroidtoothPlugin.java | 10 +--------- .../plugins/droidtooth/InsecureBluetooth.java | 18 ++++++------------ 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java b/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java index 3996ced56a..df250dede3 100644 --- a/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java +++ b/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java @@ -259,12 +259,8 @@ class DroidtoothPlugin implements DuplexPlugin { // Try to connect BluetoothDevice d = adapter.getRemoteDevice(address); try { - if(LOG.isLoggable(INFO)) - LOG.info("Creating socket for " + address); BluetoothSocket s = InsecureBluetooth.createSocket(d, u); - if(LOG.isLoggable(INFO)) LOG.info("Connecting"); s.connect(); - if(LOG.isLoggable(INFO)) LOG.info("Connected"); return new DroidtoothTransportConnection(s); } catch(IOException e) { if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); @@ -328,7 +324,6 @@ class DroidtoothPlugin implements DuplexPlugin { if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); return null; } - if(LOG.isLoggable(INFO)) LOG.info("Listening"); // Return the first connection received by the socket, if any try { return new DroidtoothTransportConnection(ss.accept((int) timeout)); @@ -387,14 +382,11 @@ class DroidtoothPlugin implements DuplexPlugin { public void onReceive(Context ctx, Intent intent) { String action = intent.getAction(); if(action.equals(DISCOVERY_FINISHED)) { - if(LOG.isLoggable(INFO)) LOG.info("Discovery finished"); ctx.unregisterReceiver(this); connectToDiscoveredDevices(); } else if(action.equals(FOUND)) { BluetoothDevice d = intent.getParcelableExtra(EXTRA_DEVICE); - String address = d.getAddress(); - if(LOG.isLoggable(INFO)) LOG.info("Discovered " + address); - addresses.add(address); + addresses.add(d.getAddress()); } } diff --git a/src/net/sf/briar/plugins/droidtooth/InsecureBluetooth.java b/src/net/sf/briar/plugins/droidtooth/InsecureBluetooth.java index cff43fafb9..5537dde0aa 100644 --- a/src/net/sf/briar/plugins/droidtooth/InsecureBluetooth.java +++ b/src/net/sf/briar/plugins/droidtooth/InsecureBluetooth.java @@ -21,6 +21,8 @@ import android.os.ParcelUuid; // Based on http://stanford.edu/~tpurtell/InsecureBluetooth.java by T.J. Purtell class InsecureBluetooth { + private static final int TYPE_RFCOMM = 1; + @SuppressLint("NewApi") static BluetoothServerSocket listen(BluetoothAdapter adapter, String name, UUID uuid) throws IOException { @@ -102,13 +104,10 @@ class InsecureBluetooth { if(constructor == null) throw new IOException("Can't find server socket constructor"); constructor.setAccessible(true); - Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM"); + socket = constructor.newInstance(TYPE_RFCOMM, false, false, port); + Field f = socket.getClass().getDeclaredField("mSocket"); f.setAccessible(true); - int rfcommType = (Integer) f.get(null); - socket = constructor.newInstance(rfcommType, false, false, port); - Field f1 = socket.getClass().getDeclaredField("mSocket"); - f1.setAccessible(true); - Object mSocket = f1.get(socket); + Object mSocket = f.get(socket); Method bindListen = mSocket.getClass().getDeclaredMethod( "bindListen", new Class[0]); bindListen.setAccessible(true); @@ -150,16 +149,11 @@ class InsecureBluetooth { if(constructor == null) throw new IOException("Can't find socket constructor"); constructor.setAccessible(true); - Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM"); - f.setAccessible(true); - int typeRfcomm = (Integer) f.get(null); - socket = constructor.newInstance(typeRfcomm, -1, false, true, + socket = constructor.newInstance(TYPE_RFCOMM, -1, false, true, device, -1, uuid != null ? new ParcelUuid(uuid) : null); return socket; } catch(NoSuchMethodException e) { throw new IOException(e.toString()); - } catch(NoSuchFieldException e) { - throw new IOException(e.toString()); } catch(IllegalAccessException e) { throw new IOException(e.toString()); } catch(InstantiationException e) { -- GitLab