Merge branch '225-bluetooth-address' into 'master'

Try to use the real Bluetooth address on Android 6. #225

Fixes #225 until Google breaks the Settings.Secure workaround.

See merge request !109
This commit is contained in:
akwizgran
2016-02-24 20:07:05 +00:00
3 changed files with 42 additions and 11 deletions

View File

@@ -342,7 +342,7 @@ public class SettingsFragment extends BaseEventFragment implements
bluetoothSetting = !bluetoothSetting;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
AndroidUtils.setBluetooth(adapter, bluetoothSetting);
AndroidUtils.enableBluetooth(adapter, bluetoothSetting);
}
storeBluetoothSettings();
displaySettings();

View File

@@ -2,9 +2,13 @@ package org.briarproject.android.util;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.support.design.widget.TextInputLayout;
import org.briarproject.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -13,6 +17,9 @@ import java.util.List;
public class AndroidUtils {
// Fake Bluetooth address returned by BluetoothAdapter on API 23 and later
private static final String FAKE_BLUETOOTH_ADDRESS = "02:00:00:00:00:00";
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static Collection<String> getSupportedArchitectures() {
@@ -26,7 +33,8 @@ public class AndroidUtils {
return Collections.unmodifiableList(abis);
}
public static void setError(TextInputLayout til, String error, boolean condition) {
public static void setError(TextInputLayout til, String error,
boolean condition) {
if (condition) {
if (til.getError() == null)
til.setError(error);
@@ -34,15 +42,33 @@ public class AndroidUtils {
til.setError(null);
}
public static void setBluetooth(final BluetoothAdapter adapter,
final boolean activate) {
public static void enableBluetooth(final BluetoothAdapter adapter,
final boolean enable) {
new Thread() {
@Override
public void run() {
if (activate) adapter.enable();
if (enable) adapter.enable();
else adapter.disable();
}
}.start();
}
public static String getBluetoothAddress(Context ctx,
BluetoothAdapter adapter) {
// Return the adapter's address if it's valid and not fake
String address = adapter.getAddress();
if (isValidBluetoothAddress(address)) return address;
// Return the address from settings if it's valid and not fake
address = Settings.Secure.getString(ctx.getContentResolver(),
"bluetooth_address");
if (isValidBluetoothAddress(address)) return address;
// Let the caller know we can't find the address
return "";
}
private static boolean isValidBluetoothAddress(String address) {
return !StringUtils.isNullOrEmpty(address)
&& BluetoothAdapter.checkBluetoothAddress(address)
&& !address.equals(FAKE_BLUETOOTH_ADDRESS);
}
}

View File

@@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.api.TransportId;
import org.briarproject.api.android.AndroidExecutor;
import org.briarproject.api.contact.ContactId;
@@ -152,12 +153,16 @@ class DroidtoothPlugin implements DuplexPlugin {
ioExecutor.execute(new Runnable() {
public void run() {
if (!isRunning()) return;
String address = AndroidUtils.getBluetoothAddress(appContext,
adapter);
if (LOG.isLoggable(INFO))
LOG.info("Local address " + adapter.getAddress());
// Advertise the Bluetooth address to contacts
TransportProperties p = new TransportProperties();
p.put("address", adapter.getAddress());
callback.mergeLocalProperties(p);
LOG.info("Local address " + address);
if (!StringUtils.isNullOrEmpty(address)) {
// Advertise the Bluetooth address to contacts
TransportProperties p = new TransportProperties();
p.put("address", address);
callback.mergeLocalProperties(p);
}
// Bind a server socket to accept connections from contacts
BluetoothServerSocket ss;
try {