Removed unused Bluetooth code; use public API if available.

This commit is contained in:
akwizgran
2012-11-02 17:58:48 +00:00
parent 74b8a95a23
commit ba07c00907
2 changed files with 26 additions and 51 deletions

View File

@@ -130,7 +130,7 @@ class DroidtoothPlugin implements DuplexPlugin {
// Bind a server socket to accept connections from contacts // Bind a server socket to accept connections from contacts
BluetoothServerSocket ss; BluetoothServerSocket ss;
try { try {
ss = InsecureBluetooth.listen(adapter, "RFCOMM", getUuid(), false); ss = InsecureBluetooth.listen(adapter, "RFCOMM", getUuid());
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
return; return;
@@ -262,7 +262,7 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
// Try to connect // Try to connect
try { try {
BluetoothSocket s = InsecureBluetooth.createSocket(d, u, false); BluetoothSocket s = InsecureBluetooth.createSocket(d, u);
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());
@@ -323,7 +323,7 @@ class DroidtoothPlugin implements DuplexPlugin {
// Bind a new server socket to accept the invitation connection // Bind a new server socket to accept the invitation connection
final BluetoothServerSocket ss; final BluetoothServerSocket ss;
try { try {
ss = InsecureBluetooth.listen(adapter, "RFCOMM", uuid, false); ss = InsecureBluetooth.listen(adapter, "RFCOMM", uuid);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
return null; return null;

View File

@@ -7,11 +7,13 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.UUID; import java.util.UUID;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket; import android.bluetooth.BluetoothSocket;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelUuid; import android.os.ParcelUuid;
@@ -19,8 +21,13 @@ 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 {
@SuppressLint("NewApi")
static BluetoothServerSocket listen(BluetoothAdapter adapter, String name, static BluetoothServerSocket listen(BluetoothAdapter adapter, String name,
UUID uuid, boolean encrypt) throws IOException { UUID uuid) throws IOException {
if(Build.VERSION.SDK_INT >= 10) {
return adapter.listenUsingInsecureRfcommWithServiceRecord(name,
uuid);
}
try { try {
String className = BluetoothAdapter.class.getName() String className = BluetoothAdapter.class.getName()
+ ".RfcommChannelPicker"; + ".RfcommChannelPicker";
@@ -43,19 +50,10 @@ class InsecureBluetooth {
"nextChannel", new Class[0]); "nextChannel", new Class[0]);
nextChannel.setAccessible(true); nextChannel.setAccessible(true);
BluetoothServerSocket socket = null; BluetoothServerSocket socket = null;
int channel; int channel = (Integer) nextChannel.invoke(channelPicker,
while(true) { new Object[0]);
channel = (Integer) nextChannel.invoke(channelPicker, if(channel == -1) throw new IOException("No available channels");
new Object[0]); socket = listen(channel);
if(channel == -1)
throw new IOException("No available channels");
try {
socket = listen(channel, encrypt);
break;
} catch(InUseException e) {
continue;
}
}
Field f = adapter.getClass().getDeclaredField("mService"); Field f = adapter.getClass().getDeclaredField("mService");
f.setAccessible(true); f.setAccessible(true);
Object mService = f.get(adapter); Object mService = f.get(adapter);
@@ -97,8 +95,7 @@ class InsecureBluetooth {
} }
} }
private static BluetoothServerSocket listen(int port, boolean encrypt, private static BluetoothServerSocket listen(int port) throws IOException {
boolean reuse) throws IOException, InUseException {
BluetoothServerSocket socket = null; BluetoothServerSocket socket = null;
try { try {
Constructor<BluetoothServerSocket> constructor = Constructor<BluetoothServerSocket> constructor =
@@ -110,21 +107,16 @@ class InsecureBluetooth {
Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM"); Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM");
f.setAccessible(true); f.setAccessible(true);
int rfcommType = (Integer) f.get(null); int rfcommType = (Integer) f.get(null);
Field f1 = BluetoothSocket.class.getDeclaredField("EADDRINUSE"); socket = constructor.newInstance(rfcommType, false, false, port);
Field f1 = socket.getClass().getDeclaredField("mSocket");
f1.setAccessible(true); f1.setAccessible(true);
int eAddrInUse = (Integer) f1.get(null); Object mSocket = f1.get(socket);
socket = constructor.newInstance(rfcommType, false, encrypt, port);
Field f2 = socket.getClass().getDeclaredField("mSocket");
f2.setAccessible(true);
Object mSocket = f2.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);
Object result = bindListen.invoke(mSocket, new Object[0]); Object result = bindListen.invoke(mSocket, new Object[0]);
int errno = (Integer) result; int errno = (Integer) result;
if(reuse && errno == eAddrInUse) { if(errno != 0) {
throw new InUseException();
} else if(errno != 0) {
try { try {
socket.close(); socket.close();
} catch(IOException ignored) {} } catch(IOException ignored) {}
@@ -150,13 +142,12 @@ class InsecureBluetooth {
} }
} }
static BluetoothServerSocket listen(int port, boolean encrypt) @SuppressLint("NewApi")
static BluetoothSocket createSocket(BluetoothDevice device, UUID uuid)
throws IOException { throws IOException {
return listen(port, encrypt, false); if(Build.VERSION.SDK_INT >= 10) {
} return device.createInsecureRfcommSocketToServiceRecord(uuid);
}
private static BluetoothSocket createSocket(BluetoothDevice device,
int port, UUID uuid, boolean encrypt) throws IOException {
try { try {
BluetoothSocket socket = null; BluetoothSocket socket = null;
Constructor<BluetoothSocket> constructor = Constructor<BluetoothSocket> constructor =
@@ -165,13 +156,12 @@ class InsecureBluetooth {
BluetoothDevice.class, int.class, ParcelUuid.class); BluetoothDevice.class, int.class, ParcelUuid.class);
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"); Field f = BluetoothSocket.class.getDeclaredField("TYPE_RFCOMM");
f.setAccessible(true); f.setAccessible(true);
int typeRfcomm = (Integer) f.get(null); int typeRfcomm = (Integer) f.get(null);
socket = constructor.newInstance(typeRfcomm, -1, false, true, socket = constructor.newInstance(typeRfcomm, -1, false, true,
device, port, 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());
@@ -189,19 +179,4 @@ class InsecureBluetooth {
} }
} }
} }
static BluetoothSocket createSocket(BluetoothDevice device, UUID uuid,
boolean encrypt) throws IOException {
return createSocket(device, -1, uuid, encrypt);
}
static BluetoothSocket createSocket(BluetoothDevice device, int port,
boolean encrypt) throws IOException {
return createSocket(device, port, null, encrypt);
}
private static class InUseException extends RuntimeException {
private static final long serialVersionUID = -5983642322821496023L;
}
} }