diff --git a/briar-android/src/org/briarproject/android/fragment/SettingsFragment.java b/briar-android/src/org/briarproject/android/fragment/SettingsFragment.java index b250861c8b86aa427f1853148bdb93dc24d8dfb3..89590d4fe367a64ab1354dc4a0d48f127857a41f 100644 --- a/briar-android/src/org/briarproject/android/fragment/SettingsFragment.java +++ b/briar-android/src/org/briarproject/android/fragment/SettingsFragment.java @@ -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(); diff --git a/briar-android/src/org/briarproject/android/util/AndroidUtils.java b/briar-android/src/org/briarproject/android/util/AndroidUtils.java index 01d2cbf4b1cd1276d1bcacd0b05015e92c08eb2f..41599dd4ed8a24036d265a02cfb89704fe2a0798 100644 --- a/briar-android/src/org/briarproject/android/util/AndroidUtils.java +++ b/briar-android/src/org/briarproject/android/util/AndroidUtils.java @@ -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); + } } diff --git a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java index 646e498d90cdf25511a249e2e8e2d3195356cc39..2bd6d3d37c6077b076e7ade19af620b61fd4dc0b 100644 --- a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java +++ b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java @@ -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 {