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; bluetoothSetting = !bluetoothSetting;
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) { if (adapter != null) {
AndroidUtils.setBluetooth(adapter, bluetoothSetting); AndroidUtils.enableBluetooth(adapter, bluetoothSetting);
} }
storeBluetoothSettings(); storeBluetoothSettings();
displaySettings(); displaySettings();

View File

@@ -2,9 +2,13 @@ package org.briarproject.android.util;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build; import android.os.Build;
import android.provider.Settings;
import android.support.design.widget.TextInputLayout; import android.support.design.widget.TextInputLayout;
import org.briarproject.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@@ -13,6 +17,9 @@ import java.util.List;
public class AndroidUtils { 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") @SuppressLint("NewApi")
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static Collection<String> getSupportedArchitectures() { public static Collection<String> getSupportedArchitectures() {
@@ -26,7 +33,8 @@ public class AndroidUtils {
return Collections.unmodifiableList(abis); 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 (condition) {
if (til.getError() == null) if (til.getError() == null)
til.setError(error); til.setError(error);
@@ -34,15 +42,33 @@ public class AndroidUtils {
til.setError(null); til.setError(null);
} }
public static void setBluetooth(final BluetoothAdapter adapter, public static void enableBluetooth(final BluetoothAdapter adapter,
final boolean activate) { final boolean enable) {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
if (activate) adapter.enable(); if (enable) adapter.enable();
else adapter.disable(); else adapter.disable();
} }
}.start(); }.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.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.api.TransportId; import org.briarproject.api.TransportId;
import org.briarproject.api.android.AndroidExecutor; import org.briarproject.api.android.AndroidExecutor;
import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactId;
@@ -152,12 +153,16 @@ class DroidtoothPlugin implements DuplexPlugin {
ioExecutor.execute(new Runnable() { ioExecutor.execute(new Runnable() {
public void run() { public void run() {
if (!isRunning()) return; if (!isRunning()) return;
String address = AndroidUtils.getBluetoothAddress(appContext,
adapter);
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Local address " + adapter.getAddress()); LOG.info("Local address " + address);
// Advertise the Bluetooth address to contacts if (!StringUtils.isNullOrEmpty(address)) {
TransportProperties p = new TransportProperties(); // Advertise the Bluetooth address to contacts
p.put("address", adapter.getAddress()); TransportProperties p = new TransportProperties();
callback.mergeLocalProperties(p); p.put("address", address);
callback.mergeLocalProperties(p);
}
// Bind a server socket to accept connections from contacts // Bind a server socket to accept connections from contacts
BluetoothServerSocket ss; BluetoothServerSocket ss;
try { try {