Skip to content

Commit

Permalink
ktlint 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Jan 22, 2024
1 parent 947912c commit a2aaf85
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
override fun configureFlutterEngine(
@NonNull flutterEngine: FlutterEngine,
) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ import com.signify.hue.flutterreactiveble.ProtobufModel as pb

@Suppress("TooManyFunctions")
class PluginController {
private val pluginMethods = mapOf<String, (call: MethodCall, result: Result) -> Unit>(
"initialize" to this::initializeClient,
"deinitialize" to this::deinitializeClient,
"scanForDevices" to this::scanForDevices,
"connectToDevice" to this::connectToDevice,
"clearGattCache" to this::clearGattCache,
"disconnectFromDevice" to this::disconnectFromDevice,
"readCharacteristic" to this::readCharacteristic,
"writeCharacteristicWithResponse" to this::writeCharacteristicWithResponse,
"writeCharacteristicWithoutResponse" to this::writeCharacteristicWithoutResponse,
"readNotifications" to this::readNotifications,
"stopNotifications" to this::stopNotifications,
"negotiateMtuSize" to this::negotiateMtuSize,
"requestConnectionPriority" to this::requestConnectionPriority,
"discoverServices" to this::discoverServices,
"getDiscoveredServices" to this::discoverServices,
)
private val pluginMethods =
mapOf<String, (call: MethodCall, result: Result) -> Unit>(
"initialize" to this::initializeClient,
"deinitialize" to this::deinitializeClient,
"scanForDevices" to this::scanForDevices,
"connectToDevice" to this::connectToDevice,
"clearGattCache" to this::clearGattCache,
"disconnectFromDevice" to this::disconnectFromDevice,
"readCharacteristic" to this::readCharacteristic,
"writeCharacteristicWithResponse" to this::writeCharacteristicWithResponse,
"writeCharacteristicWithoutResponse" to this::writeCharacteristicWithoutResponse,
"readNotifications" to this::readNotifications,
"stopNotifications" to this::stopNotifications,
"negotiateMtuSize" to this::negotiateMtuSize,
"requestConnectionPriority" to this::requestConnectionPriority,
"discoverServices" to this::discoverServices,
"getDiscoveredServices" to this::discoverServices,
)

private lateinit var bleClient: com.signify.hue.flutterreactiveble.ble.BleClient

Expand All @@ -53,7 +54,10 @@ class PluginController {
private val uuidConverter = UuidConverter()
private val protoConverter = ProtobufMessageConverter()

internal fun initialize(messenger: BinaryMessenger, context: Context) {
internal fun initialize(
messenger: BinaryMessenger,
context: Context,
) {
bleClient = com.signify.hue.flutterreactiveble.ble.ReactiveBleClient(context)

scanchannel = EventChannel(messenger, "flutter_reactive_ble_scan")
Expand All @@ -77,32 +81,50 @@ class PluginController {
deviceConnectionHandler.disconnectAll()
}

internal fun execute(call: MethodCall, result: Result) {
internal fun execute(
call: MethodCall,
result: Result,
) {
pluginMethods[call.method]?.invoke(call, result) ?: result.notImplemented()
}

private fun initializeClient(call: MethodCall, result: Result) {
private fun initializeClient(
call: MethodCall,
result: Result,
) {
bleClient.initializeClient()
result.success(null)
}

private fun deinitializeClient(call: MethodCall, result: Result) {
private fun deinitializeClient(
call: MethodCall,
result: Result,
) {
deinitialize()
result.success(null)
}

private fun scanForDevices(call: MethodCall, result: Result) {
private fun scanForDevices(
call: MethodCall,
result: Result,
) {
scanDevicesHandler.prepareScan(pb.ScanForDevicesRequest.parseFrom(call.arguments as ByteArray))
result.success(null)
}

private fun connectToDevice(call: MethodCall, result: Result) {
private fun connectToDevice(
call: MethodCall,
result: Result,
) {
result.success(null)
val connectDeviceMessage = pb.ConnectToDeviceRequest.parseFrom(call.arguments as ByteArray)
deviceConnectionHandler.connectToDevice(connectDeviceMessage)
}

private fun clearGattCache(call: MethodCall, result: Result) {
private fun clearGattCache(
call: MethodCall,
result: Result,
) {
val args = pb.ClearGattCacheRequest.parseFrom(call.arguments as ByteArray)
bleClient.clearGattCache(args.deviceId)
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -112,23 +134,30 @@ class PluginController {
result.success(info.toByteArray())
},
{
val info = protoConverter.convertClearGattCacheError(
ClearGattCacheErrorType.UNKNOWN,
it.message,
)
val info =
protoConverter.convertClearGattCacheError(
ClearGattCacheErrorType.UNKNOWN,
it.message,
)
result.success(info.toByteArray())
},
)
.discard()
}

private fun disconnectFromDevice(call: MethodCall, result: Result) {
private fun disconnectFromDevice(
call: MethodCall,
result: Result,
) {
result.success(null)
val connectDeviceMessage = pb.DisconnectFromDeviceRequest.parseFrom(call.arguments as ByteArray)
deviceConnectionHandler.disconnectDevice(connectDeviceMessage.deviceId)
}

private fun readCharacteristic(call: MethodCall, result: Result) {
private fun readCharacteristic(
call: MethodCall,
result: Result,
) {
result.success(null)

val readCharMessage = pb.ReadCharacteristicRequest.parseFrom(call.arguments as ByteArray)
Expand All @@ -146,10 +175,11 @@ class PluginController {
{ charResult ->
when (charResult) {
is com.signify.hue.flutterreactiveble.ble.CharOperationSuccessful -> {
val charInfo = protoConverter.convertCharacteristicInfo(
readCharMessage.characteristic,
charResult.value.toByteArray(),
)
val charInfo =
protoConverter.convertCharacteristicInfo(
readCharMessage.characteristic,
charResult.value.toByteArray(),
)
charNotificationHandler.addSingleReadToStream(charInfo)
}
is com.signify.hue.flutterreactiveble.ble.CharOperationFailed -> {
Expand Down Expand Up @@ -178,12 +208,26 @@ class PluginController {
.discard()
}

private fun writeCharacteristicWithResponse(call: MethodCall, result: Result) {
executeWriteAndPropagateResultToChannel(call, result, com.signify.hue.flutterreactiveble.ble.BleClient::writeCharacteristicWithResponse)
private fun writeCharacteristicWithResponse(
call: MethodCall,
result: Result,
) {
executeWriteAndPropagateResultToChannel(
call,
result,
com.signify.hue.flutterreactiveble.ble.BleClient::writeCharacteristicWithResponse,
)
}

private fun writeCharacteristicWithoutResponse(call: MethodCall, result: Result) {
executeWriteAndPropagateResultToChannel(call, result, com.signify.hue.flutterreactiveble.ble.BleClient::writeCharacteristicWithoutResponse)
private fun writeCharacteristicWithoutResponse(
call: MethodCall,
result: Result,
) {
executeWriteAndPropagateResultToChannel(
call,
result,
com.signify.hue.flutterreactiveble.ble.BleClient::writeCharacteristicWithoutResponse,
)
}

private fun executeWriteAndPropagateResultToChannel(
Expand Down Expand Up @@ -237,19 +281,28 @@ class PluginController {
.discard()
}

private fun readNotifications(call: MethodCall, result: Result) {
private fun readNotifications(
call: MethodCall,
result: Result,
) {
val request = pb.NotifyCharacteristicRequest.parseFrom(call.arguments as ByteArray)
charNotificationHandler.subscribeToNotifications(request)
result.success(null)
}

private fun stopNotifications(call: MethodCall, result: Result) {
private fun stopNotifications(
call: MethodCall,
result: Result,
) {
val request = pb.NotifyNoMoreCharacteristicRequest.parseFrom(call.arguments as ByteArray)
charNotificationHandler.unsubscribeFromNotifications(request)
result.success(null)
}

private fun negotiateMtuSize(call: MethodCall, result: Result) {
private fun negotiateMtuSize(
call: MethodCall,
result: Result,
) {
val request = pb.NegotiateMtuRequest.parseFrom(call.arguments as ByteArray)
bleClient.negotiateMtuSize(request.deviceId, request.mtuSize)
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -271,7 +324,10 @@ class PluginController {
.discard()
}

private fun requestConnectionPriority(call: MethodCall, result: Result) {
private fun requestConnectionPriority(
call: MethodCall,
result: Result,
) {
val request = pb.ChangeConnectionPriorityRequest.parseFrom(call.arguments as ByteArray)

bleClient.requestConnectionPriority(request.deviceId, request.priority.toConnectionPriority())
Expand All @@ -298,7 +354,10 @@ class PluginController {
.discard()
}

private fun discoverServices(call: MethodCall, result: Result) {
private fun discoverServices(
call: MethodCall,
result: Result,
) {
val request = pb.DiscoverServicesRequest.parseFrom(call.arguments as ByteArray)

bleClient.discoverServices(request.deviceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class ReactiveBlePlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
}
}

override fun onMethodCall(call: MethodCall, result: Result) {
override fun onMethodCall(
call: MethodCall,
result: Result,
) {
pluginController.execute(call, result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,64 @@ import java.util.UUID

@Suppress("TooManyFunctions")
interface BleClient {

val connectionUpdateSubject: BehaviorSubject<ConnectionUpdate>

fun initializeClient()
fun scanForDevices(services: List<ParcelUuid>, scanMode: ScanMode, requireLocationServicesEnabled: Boolean): Observable<ScanInfo>
fun connectToDevice(deviceId: String, timeout: Duration)

fun scanForDevices(
services: List<ParcelUuid>,
scanMode: ScanMode,
requireLocationServicesEnabled: Boolean,
): Observable<ScanInfo>

fun connectToDevice(
deviceId: String,
timeout: Duration,
)

fun disconnectDevice(deviceId: String)

fun disconnectAllDevices()

fun discoverServices(deviceId: String): Single<RxBleDeviceServices>

fun clearGattCache(deviceId: String): Completable

fun readCharacteristic(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
): Single<CharOperationResult>

fun setupNotification(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
): Observable<ByteArray>

fun writeCharacteristicWithResponse(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
value: ByteArray,
): Single<CharOperationResult>

fun writeCharacteristicWithoutResponse(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
value: ByteArray,
): Single<CharOperationResult>
fun negotiateMtuSize(deviceId: String, size: Int): Single<MtuNegotiateResult>

fun negotiateMtuSize(
deviceId: String,
size: Int,
): Single<MtuNegotiateResult>

fun observeBleStatus(): Observable<BleStatus>
fun requestConnectionPriority(deviceId: String, priority: ConnectionPriority): Single<RequestConnectionPriorityResult>

fun requestConnectionPriority(
deviceId: String,
priority: ConnectionPriority,
): Single<RequestConnectionPriorityResult>
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,33 @@ data class ScanInfo(val deviceId: String, val name: String, val rssi: Int, val c
}

sealed class ConnectionUpdate

data class ConnectionUpdateSuccess(val deviceId: String, val connectionState: Int) : ConnectionUpdate()

data class ConnectionUpdateError(val deviceId: String, val errorMessage: String) : ConnectionUpdate()

sealed class EstablishConnectionResult

data class EstablishedConnection(val deviceId: String, val rxConnection: RxBleConnection) : EstablishConnectionResult()

data class EstablishConnectionFailure(val deviceId: String, val errorMessage: String) : EstablishConnectionResult()

sealed class MtuNegotiateResult

data class MtuNegotiateSuccesful(val deviceId: String, val size: Int) : MtuNegotiateResult()

data class MtuNegotiateFailed(val deviceId: String, val errorMessage: String) : MtuNegotiateResult()

sealed class CharOperationResult

data class CharOperationSuccessful(val deviceId: String, val value: List<Byte>) : CharOperationResult()

data class CharOperationFailed(val deviceId: String, val errorMessage: String) : CharOperationResult()

sealed class RequestConnectionPriorityResult

data class RequestConnectionPrioritySuccess(val deviceId: String) : RequestConnectionPriorityResult()

data class RequestConnectionPriorityFailed(val deviceId: String, val errorMessage: String) : RequestConnectionPriorityResult()

enum class BleStatus(val code: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.annotation.VisibleForTesting
import io.reactivex.subjects.BehaviorSubject

internal class ConnectionQueue {

private val queueSubject = BehaviorSubject.createDefault(listOf<String>())

fun observeQueue() = queueSubject
Expand Down
Loading

0 comments on commit a2aaf85

Please sign in to comment.