Skip to content

Commit

Permalink
detekt fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
neoBortx authored and Borja committed Jan 4, 2024
1 parent 038d2b5 commit da1f977
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion simpleBleClient/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ potential-bugs:
active: true
UnsafeCallOnNullableType:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**', '**/BleManager.kt']
UnsafeCast:
active: true
UnusedUnaryOperator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface SimpleBleClientDeviceSeeker {
* @throws SimpleBleClientException Thrown when an error occurs during the BLE operation.
*
*/
public suspend fun getDevicesNearby(serviceUUID: UUID? = null, deviceName: String? = null): Flow<BluetoothDevice>
public fun getDevicesNearby(serviceUUID: UUID? = null, deviceName: String? = null): Flow<BluetoothDevice>

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface SimpleBleClientSubscription {
*
* @throws SimpleBleClientException Thrown when an error occurs during the BLE operation.
*/
public suspend fun subscribeToCharacteristicChanges(characteristicsUUid: List<UUID>): SharedFlow<BleNetworkMessage>
public suspend fun subscribeToCharacteristicChanges(characteristicsUUid: List<UUID>): Boolean

/**
* Ge the shared flow that emits [BleNetworkMessage] objects with the changes in the subscribed characteristics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import com.bortxapps.simplebleclient.api.data.BleNetworkMessage
import com.bortxapps.simplebleclient.exceptions.BleError
import com.bortxapps.simplebleclient.exceptions.SimpleBleClientException
import com.bortxapps.simplebleclient.manager.utils.launchBleOperationWithValidations
import com.bortxapps.simplebleclient.manager.utils.launchBleOperationWithValidationsSync
import com.bortxapps.simplebleclient.manager.utils.mapBleConnectionState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.util.UUID
Expand Down Expand Up @@ -40,7 +40,7 @@ internal class BleManager(
}

//region search devices
override suspend fun getDevicesNearby(serviceUUID: UUID?, deviceName: String?) = launchBleOperationWithValidations(context) {
override fun getDevicesNearby(serviceUUID: UUID?, deviceName: String?) = launchBleOperationWithValidationsSync(context) {
bleManagerDeviceConnection.getDevicesNearBy(serviceUUID, deviceName)
}

Expand All @@ -64,7 +64,7 @@ internal class BleManager(
true
}

override suspend fun subscribeToCharacteristicChanges(characteristicsUUid: List<UUID>): SharedFlow<BleNetworkMessage> =
override suspend fun subscribeToCharacteristicChanges(characteristicsUUid: List<UUID>): Boolean =
launchBleOperationWithValidations(context) {
checkGatt()
if (bleManagerGattConnectionOperations.discoverServices(bluetoothGatt!!)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ internal class BleManagerGattCallBacks(bleConfiguration: BleConfiguration, bleMe
private var onServicesDiscoveredDeferred: CompletableDeferred<Boolean>? = null
//endregion

private var connectionStatus: MutableStateFlow<Int> = MutableStateFlow(BluetoothProfile.STATE_DISCONNECTED)
private var incomeMessages: MutableSharedFlow<BleNetworkMessage> = MutableSharedFlow(
private val connectionStatus: MutableStateFlow<Int> = MutableStateFlow(BluetoothProfile.STATE_DISCONNECTED)
private val incomeMessages: MutableSharedFlow<BleNetworkMessage> = MutableSharedFlow(
replay = bleConfiguration.messageBufferRetries,
extraBufferCapacity = bleConfiguration.messageBufferSize
)

private var bleMessageProcessor: BleNetworkMessageProcessor = bleMessageProcessorProvider.getMessageProcessor()
private val bleMessageProcessor: BleNetworkMessageProcessor = bleMessageProcessorProvider.getMessageProcessor()

override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class BleManagerGattSubscriptions(
private const val BLE_DESCRIPTION_BASE_UUID = "00002902-0000-1000-8000-00805F9B34FB"
}

suspend fun subscribeToNotifications(bluetoothGatt: BluetoothGatt, characteristicsUUid: List<UUID>): SharedFlow<BleNetworkMessage> {
suspend fun subscribeToNotifications(bluetoothGatt: BluetoothGatt, characteristicsUUid: List<UUID>): Boolean {
getNotifiableCharacteristics(bluetoothGatt, characteristicsUUid).forEach { characteristic ->
withContext(IO) {
launchGattOperation {
Expand All @@ -46,7 +46,7 @@ internal class BleManagerGattSubscriptions(
}
}

return bleManagerGattCallBacks.subscribeToIncomeMessages()
return true
}

fun subscribeToIncomeMessages(): SharedFlow<BleNetworkMessage> = bleManagerGattCallBacks.subscribeToIncomeMessages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ internal suspend fun <T> launchBleOperationWithValidations(context: Context, act
throw SimpleBleClientException(BleError.OTHER)
}
}

internal fun <T> launchBleOperationWithValidationsSync(context: Context, action: () -> T): T {
return try {
checkBleHardwareAvailable(context)
checkBluetoothEnabled(context)
checkPermissions(context)
action()
} catch (ex: SimpleBleClientException) {
Log.e("RepositoryBaseBle", "launchBleOperationWithValidations SimpleBleClientException -> $ex - ${ex.stackTraceToString()}")
throw ex
} catch (ex: Exception) {
Log.e("RepositoryBaseBle", "launchBleOperationWithValidations error -> $ex - ${ex.stackTraceToString()}")
throw SimpleBleClientException(BleError.OTHER)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns true
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true

runBlocking {
assertTrue(bleManager.connectToDevice(contextMock, goProAddress))
Expand All @@ -167,7 +167,7 @@ internal class BleManagerTest {
fun `connectToDevice returns null expect SimpleBleClientException`() = runTest {
coEvery { bleManagerGattConnectionOperationsMock.connectToDevice(contextMock, goProAddress, bleManagerGattCallBacksMock) } returns null
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns true
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true

Assert.assertThrows(SimpleBleClientException::class.java) {
runBlocking {
Expand Down Expand Up @@ -199,7 +199,7 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns false
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true
coEvery { bleManagerGattConnectionOperationsMock.disconnect(bluetoothGattMock) } returns false

runBlocking {
Expand All @@ -219,7 +219,7 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns false
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true
coEvery { bleManagerGattConnectionOperationsMock.disconnect(bluetoothGattMock) } returns true
coEvery { bleManagerGattConnectionOperationsMock.freeConnection(bluetoothGattMock) } just runs
coEvery { bleManagerGattCallBacksMock.reset() } just runs
Expand Down Expand Up @@ -263,7 +263,7 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns false
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true
coEvery { bleManagerGattConnectionOperationsMock.disconnect(bluetoothGattMock) } returns false
coEvery {
bleManagerGattWriteOperationsMock.sendData(
Expand Down Expand Up @@ -301,7 +301,7 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns false
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true
coEvery { bleManagerGattConnectionOperationsMock.disconnect(bluetoothGattMock) } returns false
coEvery {
bleManagerGattReadOperationsMock.readData(
Expand Down Expand Up @@ -338,10 +338,10 @@ internal class BleManagerTest {
)
} returns bluetoothGattMock
coEvery { bleManagerGattConnectionOperationsMock.discoverServices(bluetoothGattMock) } returns true
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns characteristicMessageFlow
coEvery { bleManagerGattSubscriptionsMock.subscribeToNotifications(bluetoothGattMock, characteristics) } returns true

bleManager.connectToDevice(contextMock, goProAddress)
assertEquals(characteristicMessageFlow, bleManager.subscribeToCharacteristicChanges(characteristics))
assertEquals(true, bleManager.subscribeToCharacteristicChanges(characteristics))
}

@Test
Expand Down

0 comments on commit da1f977

Please sign in to comment.