Skip to content
Snippets Groups Projects
Commit e8a4d778 authored by akwizgran's avatar akwizgran
Browse files

Android Bluetooth code cleanup.

parent 18cd0c5f
No related branches found
No related tags found
No related merge requests found
......@@ -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());
}
}
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment