diff --git a/plugins/radar-android-phone/src/main/java/org/radarbase/passive/phone/PhoneBluetoothManager.kt b/plugins/radar-android-phone/src/main/java/org/radarbase/passive/phone/PhoneBluetoothManager.kt index 9d1e818b0..86e8101dc 100644 --- a/plugins/radar-android-phone/src/main/java/org/radarbase/passive/phone/PhoneBluetoothManager.kt +++ b/plugins/radar-android-phone/src/main/java/org/radarbase/passive/phone/PhoneBluetoothManager.kt @@ -24,7 +24,6 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter -import android.content.SharedPreferences import android.content.pm.PackageManager import android.os.Build import androidx.core.app.ActivityCompat @@ -42,7 +41,6 @@ import org.radarcns.passive.phone.PhoneBluetoothDeviceScanned import org.radarcns.passive.phone.PhoneBluetoothDevices import org.slf4j.LoggerFactory import java.nio.ByteBuffer -import java.util.concurrent.ThreadLocalRandom import java.util.concurrent.TimeUnit class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceManager(service) { @@ -52,10 +50,6 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana private var bluetoothBroadcastReceiver: BroadcastReceiver? = null private val hashGenerator: HashGenerator = HashGenerator(service, "bluetooth_devices") - private val preferences: SharedPreferences - get() = service.getSharedPreferences(PhoneBluetoothManager::class.java.name, Context.MODE_PRIVATE) - - private var hashSaltReference: Int = 0 init { name = service.getString(R.string.bluetooth_devices) @@ -65,17 +59,6 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana requestName = ACTION_SCAN_DEVICES wake = true } - preferences.apply { - if (contains(HASH_SALT_REFERENCE)) { - hashSaltReference = getInt(HASH_SALT_REFERENCE, -1) - } else { - val random = ThreadLocalRandom.current() - while (hashSaltReference == 0) { - hashSaltReference = random.nextInt() - } - edit().putInt(HASH_SALT_REFERENCE, hashSaltReference).apply() - } - } } override fun start(acceptableIds: Set) { @@ -122,32 +105,32 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana } ?: return val macAddress = device.address - val macAddressHash: ByteBuffer = hashGenerator.createHashByteBuffer(macAddress + "$hashSaltReference") - - val scannedTopicBuilder = PhoneBluetoothDeviceScanned.newBuilder().apply { - time = currentTime - timeReceived = currentTime - } + val macAddressHash: ByteBuffer = hashGenerator.createHashByteBuffer(macAddress) val pairedDevices: Set = if (hasConnectPermission) bluetoothAdapter.bondedDevices else emptySet() pairedDevices.forEach { bd -> val mac = bd.address - val hash = hashGenerator.createHashByteBuffer(mac + "$hashSaltReference") + val hash = hashGenerator.createHashByteBuffer(mac) + + val scannedTopicBuilder = PhoneBluetoothDeviceScanned.newBuilder().apply { + time = currentTime + timeReceived = currentTime + } send(bluetoothScannedTopic, scannedTopicBuilder.apply { this.macAddressHash = hash this.pairedState = bd.bondState.toPairedState() - this.hashSaltReference = hashSaltReference }.build()) } - send(bluetoothScannedTopic, scannedTopicBuilder.apply { + + send(bluetoothScannedTopic, PhoneBluetoothDeviceScanned.newBuilder().apply { + this.time = currentTime + this.timeReceived = currentTime this.macAddressHash = macAddressHash this.pairedState = device.bondState.toPairedState() - this.hashSaltReference = hashSaltReference }.build()) - } BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> { @@ -196,12 +179,11 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana private const val SCAN_DEVICES_REQUEST_CODE = 3248902 private const val ACTION_SCAN_DEVICES = "org.radarbase.passive.phone.PhoneBluetoothManager.ACTION_SCAN_DEVICES" - private const val HASH_SALT_REFERENCE = "hash_salt_reference" private fun Int.toPairedState(): PairedState = when(this) { - 10 -> PairedState.NOT_PAIRED - 11 -> PairedState.PAIRING - 12 -> PairedState.PAIRED + BluetoothDevice.BOND_NONE -> PairedState.NOT_PAIRED + BluetoothDevice.BOND_BONDING -> PairedState.PAIRING + BluetoothDevice.BOND_BONDED -> PairedState.PAIRED else -> PairedState.UNKNOWN } }