Skip to content
Snippets Groups Projects
Unverified Commit 9789c0ff authored by akwizgran's avatar akwizgran
Browse files

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

parent 6b802c2e
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
......@@ -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,34 @@ 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 (!StringUtils.isNullOrEmpty(address)
&& BluetoothAdapter.checkBluetoothAddress(address)
&& !address.equals(FAKE_BLUETOOTH_ADDRESS)) {
return address;
}
// Return the address from settings if it's valid
address = Settings.Secure.getString(ctx.getContentResolver(),
"bluetooth_address");
if (!StringUtils.isNullOrEmpty(address)
&& BluetoothAdapter.checkBluetoothAddress(address)) {
return address;
}
// As a last resort, return a fake but valid address
return FAKE_BLUETOOTH_ADDRESS;
}
}
......@@ -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,11 +153,13 @@ 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());
LOG.info("Local address " + address);
// Advertise the Bluetooth address to contacts
TransportProperties p = new TransportProperties();
p.put("address", adapter.getAddress());
p.put("address", address);
callback.mergeLocalProperties(p);
// Bind a server socket to accept connections from contacts
BluetoothServerSocket ss;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment