mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Android Bluetooth code cleanup.
This commit is contained in:
@@ -259,12 +259,8 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
// Try to connect
|
// Try to connect
|
||||||
BluetoothDevice d = adapter.getRemoteDevice(address);
|
BluetoothDevice d = adapter.getRemoteDevice(address);
|
||||||
try {
|
try {
|
||||||
if(LOG.isLoggable(INFO))
|
|
||||||
LOG.info("Creating socket for " + address);
|
|
||||||
BluetoothSocket s = InsecureBluetooth.createSocket(d, u);
|
BluetoothSocket s = InsecureBluetooth.createSocket(d, u);
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Connecting");
|
|
||||||
s.connect();
|
s.connect();
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Connected");
|
|
||||||
return new DroidtoothTransportConnection(s);
|
return new DroidtoothTransportConnection(s);
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
@@ -328,7 +324,6 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Listening");
|
|
||||||
// Return the first connection received by the socket, if any
|
// Return the first connection received by the socket, if any
|
||||||
try {
|
try {
|
||||||
return new DroidtoothTransportConnection(ss.accept((int) timeout));
|
return new DroidtoothTransportConnection(ss.accept((int) timeout));
|
||||||
@@ -387,14 +382,11 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
public void onReceive(Context ctx, Intent intent) {
|
public void onReceive(Context ctx, Intent intent) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
if(action.equals(DISCOVERY_FINISHED)) {
|
if(action.equals(DISCOVERY_FINISHED)) {
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Discovery finished");
|
|
||||||
ctx.unregisterReceiver(this);
|
ctx.unregisterReceiver(this);
|
||||||
connectToDiscoveredDevices();
|
connectToDiscoveredDevices();
|
||||||
} else if(action.equals(FOUND)) {
|
} else if(action.equals(FOUND)) {
|
||||||
BluetoothDevice d = intent.getParcelableExtra(EXTRA_DEVICE);
|
BluetoothDevice d = intent.getParcelableExtra(EXTRA_DEVICE);
|
||||||
String address = d.getAddress();
|
addresses.add(d.getAddress());
|
||||||
if(LOG.isLoggable(INFO)) LOG.info("Discovered " + address);
|
|
||||||
addresses.add(address);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import android.os.ParcelUuid;
|
|||||||
// Based on http://stanford.edu/~tpurtell/InsecureBluetooth.java by T.J. Purtell
|
// Based on http://stanford.edu/~tpurtell/InsecureBluetooth.java by T.J. Purtell
|
||||||
class InsecureBluetooth {
|
class InsecureBluetooth {
|
||||||
|
|
||||||
|
private static final int TYPE_RFCOMM = 1;
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
static BluetoothServerSocket listen(BluetoothAdapter adapter, String name,
|
static BluetoothServerSocket listen(BluetoothAdapter adapter, String name,
|
||||||
UUID uuid) throws IOException {
|
UUID uuid) throws IOException {
|
||||||
@@ -102,13 +104,10 @@ class InsecureBluetooth {
|
|||||||
if(constructor == null)
|
if(constructor == null)
|
||||||
throw new IOException("Can't find server socket constructor");
|
throw new IOException("Can't find server socket constructor");
|
||||||
constructor.setAccessible(true);
|
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);
|
f.setAccessible(true);
|
||||||
int rfcommType = (Integer) f.get(null);
|
Object mSocket = f.get(socket);
|
||||||
socket = constructor.newInstance(rfcommType, false, false, port);
|
|
||||||
Field f1 = socket.getClass().getDeclaredField("mSocket");
|
|
||||||
f1.setAccessible(true);
|
|
||||||
Object mSocket = f1.get(socket);
|
|
||||||
Method bindListen = mSocket.getClass().getDeclaredMethod(
|
Method bindListen = mSocket.getClass().getDeclaredMethod(
|
||||||
"bindListen", new Class[0]);
|
"bindListen", new Class[0]);
|
||||||
bindListen.setAccessible(true);
|
bindListen.setAccessible(true);
|
||||||
@@ -150,16 +149,11 @@ class InsecureBluetooth {
|
|||||||
if(constructor == null)
|
if(constructor == null)
|
||||||
throw new IOException("Can't find socket constructor");
|
throw new IOException("Can't find socket constructor");
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM");
|
socket = constructor.newInstance(TYPE_RFCOMM, -1, false, true,
|
||||||
f.setAccessible(true);
|
|
||||||
int typeRfcomm = (Integer) f.get(null);
|
|
||||||
socket = constructor.newInstance(typeRfcomm, -1, false, true,
|
|
||||||
device, -1, uuid != null ? new ParcelUuid(uuid) : null);
|
device, -1, uuid != null ? new ParcelUuid(uuid) : null);
|
||||||
return socket;
|
return socket;
|
||||||
} catch(NoSuchMethodException e) {
|
} catch(NoSuchMethodException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
} catch(NoSuchFieldException e) {
|
|
||||||
throw new IOException(e.toString());
|
|
||||||
} catch(IllegalAccessException e) {
|
} catch(IllegalAccessException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
} catch(InstantiationException e) {
|
} catch(InstantiationException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user