Skip to content

Commit

Permalink
fix detekt issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eldcn committed Oct 27, 2024
1 parent 3783482 commit 4f22762
Show file tree
Hide file tree
Showing 38 changed files with 128 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.time.Duration

class Bluetooth(
@ApplicationContext val context: Context,
val configuration: Set<DeviceDiscoveryDescriptor>
val configuration: Set<DeviceDiscoveryDescriptor>,
) {

private class Storage {
Expand Down Expand Up @@ -43,21 +43,21 @@ class Bluetooth(
manager.powerOff()
}

fun <Device: BluetoothDevice> nearbyDevices(type: KClass<Device>): List<Device> {
return storage.nearbyDevices.mapNotNull { it as? Device } // TODO
fun <Device : BluetoothDevice> nearbyDevices(type: KClass<Device>): List<Device> {
return storage.nearbyDevices.mapNotNull { it as? Device } // TODO
}

suspend fun <Device: BluetoothDevice> retrieveDevice(
suspend fun <Device : BluetoothDevice> retrieveDevice(
uuid: BTUUID,
type: KClass<Device>
type: KClass<Device>,
): Device? {
TODO()
}

fun scanNearbyDevices(
minimumRssi: Int? = null,
advertisementStaleInterval: Duration? = null,
autoConnect: Boolean = false
autoConnect: Boolean = false,
) {
TODO()
/*
Expand All @@ -73,4 +73,6 @@ class Bluetooth(
fun stopScanning() {
manager.stopScanning()
}
}
}

class BluetoothError(message: String, cause: Throwable? = null) : Exception(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import edu.stanford.bdh.engagehf.bluetooth.spezi.model.BluetoothService
import edu.stanford.bdh.engagehf.bluetooth.spezi.model.properties.Characteristic
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID

data class DeviceInformationService(override val id: BTUUID = BTUUID("180A")): BluetoothService {
data class DeviceInformationService(override val id: BTUUID = BTUUID("180A")) : BluetoothService {
val manufacturerName by Characteristic<String>("2A29")
val modelNumber by Characteristic<String>("2A24")
val serialNumber by Characteristic<String>("2A25")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import edu.stanford.bdh.engagehf.bluetooth.spezi.model.properties.DeviceAction
import edu.stanford.bdh.engagehf.bluetooth.spezi.model.properties.DeviceState
import edu.stanford.bdh.engagehf.bluetooth.spezi.model.properties.Service

class MockDevice: BluetoothDevice {
class MockDevice : BluetoothDevice {
val id by DeviceState(BluetoothPeripheral::id)
val name by DeviceState(BluetoothPeripheral::name)
val state by DeviceState(BluetoothPeripheral::state)
Expand All @@ -17,4 +17,4 @@ class MockDevice: BluetoothDevice {
val connect by DeviceAction.connect

val deviceInformation by Service(DeviceInformationService())
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package edu.stanford.bdh.engagehf.bluetooth.spezi.configuration

import edu.stanford.bdh.engagehf.bluetooth.spezi.model.BluetoothDevice
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration.DiscoveryCriteria
import edu.stanford.bdh.engagehf.bluetooth.spezi.model.BluetoothDevice
import kotlin.reflect.KClass

data class DeviceDiscoveryDescriptor(
val discoveryCriteria: DiscoveryCriteria,
val deviceType: KClass<BluetoothDevice>,
)
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package edu.stanford.bdh.engagehf.bluetooth.spezi.configuration

import edu.stanford.bdh.engagehf.bluetooth.spezi.model.BluetoothDevice
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration.DiscoveryCriteria
import edu.stanford.bdh.engagehf.bluetooth.spezi.model.BluetoothDevice
import kotlin.reflect.KClass

data class Discover<Device: BluetoothDevice>(
data class Discover<Device : BluetoothDevice>(
val discoveryCriteria: DiscoveryCriteria,
val deviceType: KClass<Device>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.content.Context
import androidx.annotation.RequiresPermission
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration.DiscoveryDescription
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration.DeviceDescription
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration.DiscoveryDescription
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.BluetoothManagerStorage
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.BluetoothState
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
import kotlin.time.Duration

class BluetoothManager(context: Context) {
// TODO: Throw better errors
private val manager: BluetoothManager =
context.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager ?: throw Error("")
private val manager: BluetoothManager = context.getSystemService(BluetoothManager::class.java)

private val storage = BluetoothManagerStorage()
private val callbacks = mutableListOf<BluetoothManagerScanCallback>()

val isScanning: Boolean
get() = TODO()

val nearbyPeripherals: List<BluetoothPeripheral>
get() = TODO()

Expand All @@ -37,7 +38,7 @@ class BluetoothManager(context: Context) {
discovery: Set<DiscoveryDescription>,
minimumRssi: Int?,
advertisementStaleInterval: Duration? = null,
autoConnect: Boolean = false
autoConnect: Boolean = false,
) {
val callbacks = discovery.map {
BluetoothManagerScanCallback(
Expand Down Expand Up @@ -73,7 +74,7 @@ class BluetoothManager(context: Context) {
val result = callback.results.firstOrNull {
it.device.uuids.contains(uuid.parcelUuid)
}
if (result != null) return BluetoothPeripheral(result.device)
if (result != null) return BluetoothPeripheral(manager, result)
}
return null
}
Expand All @@ -84,7 +85,7 @@ internal class BluetoothManagerScanCallback(
val minimumRssi: Int?,
val advertisementStaleInterval: Duration?,
val autoConnect: Boolean,
): ScanCallback() {
) : ScanCallback() {
val scanFilters by lazy<List<ScanFilter>> {
val scanFilters = mutableListOf<ScanFilter>()
TODO()
Expand All @@ -110,7 +111,8 @@ internal class BluetoothManagerScanCallback(
}
}
ScanSettings.CALLBACK_TYPE_FIRST_MATCH,
ScanSettings.CALLBACK_TYPE_ALL_MATCHES -> {
ScanSettings.CALLBACK_TYPE_ALL_MATCHES,
-> {
println("onScanResult: Received result")
results.add(result)
}
Expand Down Expand Up @@ -144,4 +146,4 @@ internal class BluetoothManagerScanCallback(
println("onBatchScanResults: Received ${results?.count() ?: 0} results")
results?.let { results.addAll(it) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
package edu.stanford.bdh.engagehf.bluetooth.spezi.core

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothManager
import android.bluetooth.le.ScanResult
import android.content.Context
import android.os.ParcelUuid
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.AdvertisementData
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.GATTCharacteristic
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.GATTService
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.AdvertisementData
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.OnChangeRegistration
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.PeripheralState
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
import java.util.Date

class BluetoothPeripheral(
private val manager: BluetoothManager,
private val result: ScanResult,
) {
class Storage {

}
class Storage

private var gatt: BluetoothGatt? = null
private val storage = Storage()

val id: BTUUID @SuppressLint("MissingPermission")
val id: BTUUID
@SuppressLint("MissingPermission")
get() = BTUUID(result.device.uuids.first())

val name: String? @SuppressLint("MissingPermission")
val name: String?
@SuppressLint("MissingPermission")
get() = result.device.name

val rssi: Int get() = result.rssi
Expand All @@ -45,7 +42,11 @@ class BluetoothPeripheral(
overflowServiceIdentifiers = null, // TODO: Figure out whether this exists or how it is set on iOS
txPowerLevel = result.scanRecord?.txPowerLevel,
isConnectable = result.isConnectable,
solicitedServiceIdentifiers = result.scanRecord?.serviceSolicitationUuids?.map { BTUUID(it) }
solicitedServiceIdentifiers = result.scanRecord?.serviceSolicitationUuids?.map {
BTUUID(
it
)
}
)

val services: List<GATTService>? get() = TODO()
Expand All @@ -56,33 +57,37 @@ class BluetoothPeripheral(
suspend fun connect(context: Context) {
gatt = result.device.connectGatt(context, true, null)
}

fun disconnect(): Unit = TODO()

fun getService(id: BTUUID): GATTService? = TODO()
fun getCharacteristic(characteristicId: BTUUID, serviceId: ParcelUuid): GATTCharacteristic? = TODO()
fun getCharacteristic(characteristicId: BTUUID, serviceId: ParcelUuid): GATTCharacteristic? =
TODO()

fun registerOnChangeHandler(
characteristic: GATTCharacteristic,
onChange: (ByteArray) -> Unit
onChange: (ByteArray) -> Unit,
): OnChangeRegistration = TODO()

fun registerOnChangeHandler(
service: ParcelUuid,
characteristic: ParcelUuid,
onChange: (ByteArray) -> Unit
onChange: (ByteArray) -> Unit,
): OnChangeRegistration = TODO()

fun enableNotifications(
enabled: Boolean = true,
serviceId: ParcelUuid,
characteristicId: ParcelUuid
characteristicId: ParcelUuid,
): Unit = TODO()

suspend fun setNotifications(enabled: Boolean, characteristic: GATTCharacteristic): Unit = TODO()
suspend fun setNotifications(enabled: Boolean, characteristic: GATTCharacteristic): Unit =
TODO()

suspend fun write(data: ByteArray, characteristic: GATTCharacteristic): Unit = TODO()

suspend fun writeWithoutResponse(data: ByteArray, characteristic: GATTCharacteristic): Unit = TODO()
suspend fun writeWithoutResponse(data: ByteArray, characteristic: GATTCharacteristic): Unit =
TODO()

suspend fun read(characteristic: GATTCharacteristic): ByteArray = TODO()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
data class CharacteristicDescription(
val identifier: BTUUID,
val discoverDescriptors: Boolean = false,
val autoRead: Boolean = true // TODO: Think about renaming
)
val autoRead: Boolean = true, // TODO: Think about renaming
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID

data class DeviceDescription(
val services: Set<ServiceDescription>? = null
val services: Set<ServiceDescription>? = null,
) {
fun description(identifier: BTUUID): ServiceDescription? =
services?.firstOrNull { it.identifier == identifier }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration

import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID
import edu.stanford.bdh.engagehf.bluetooth.spezi.core.model.ManufacturerIdentifier
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID

sealed class DiscoveryCriteria {
data class AdvertisedServices(
val uuids: List<BTUUID>
): DiscoveryCriteria()
val uuids: List<BTUUID>,
) : DiscoveryCriteria()

data class Accessory(
val manufacturer: ManufacturerIdentifier,
val uuids: List<BTUUID>
): DiscoveryCriteria()
val uuids: List<BTUUID>,
) : DiscoveryCriteria()

val discoveryIds: List<BTUUID> get() =
when (this) {
is AdvertisedServices -> this.uuids
is Accessory -> this.uuids
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration

data class DiscoveryDescription(
val criteria: DiscoveryCriteria,
val device: DeviceDescription
)
val device: DeviceDescription,
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package edu.stanford.bdh.engagehf.bluetooth.spezi.core.configuration

import android.os.ParcelUuid
import edu.stanford.bdh.engagehf.bluetooth.spezi.utils.BTUUID

data class ServiceDescription(
val identifier: BTUUID,
val characteristics: Set<CharacteristicDescription>? = null
val characteristics: Set<CharacteristicDescription>? = null,
) {
fun description(identifier: BTUUID): CharacteristicDescription? =
characteristics?.firstOrNull { it.identifier == identifier }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class AdvertisementData(
val overflowServiceIdentifiers: List<BTUUID>?,
val txPowerLevel: Int?,
val isConnectable: Boolean?,
val solicitedServiceIdentifiers: List<BTUUID>?
val solicitedServiceIdentifiers: List<BTUUID>?,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down Expand Up @@ -44,4 +44,4 @@ data class AdvertisementData(
result = 31 * result + (solicitedServiceIdentifiers?.hashCode() ?: 0)
return result
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package edu.stanford.bdh.engagehf.bluetooth.spezi.core.model

import android.os.ParcelUuid

sealed class BluetoothError: Error() {
data object IncompatibleDataFormat: BluetoothError() {
sealed class BluetoothError : Error() {
@Suppress("UnusedPrivateMember")
data object IncompatibleDataFormat : BluetoothError() {
private fun readResolve(): Any = IncompatibleDataFormat // TODO: What is this?
}

data class NotPresent(
val service: ParcelUuid?,
val characteristic: ParcelUuid
): BluetoothError()
val characteristic: ParcelUuid,
) : BluetoothError()

data class ControlPointRequiresNotifying(
val service: ParcelUuid,
val characteristic: ParcelUuid
): BluetoothError()
val characteristic: ParcelUuid,
) : BluetoothError()

data class ControlPointInProgress(
val service: ParcelUuid,
val characteristic: ParcelUuid
): BluetoothError()
}
val characteristic: ParcelUuid,
) : BluetoothError()
}
Loading

0 comments on commit 4f22762

Please sign in to comment.