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

This commit is contained in:
akwizgran
2016-02-23 11:53:54 +00:00
parent 6b802c2ea0
commit 9789c0ff52
3 changed files with 38 additions and 8 deletions

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,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;
}
}