Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Jan 16, 2024
1 parent 28f99b3 commit 947912c
Show file tree
Hide file tree
Showing 24 changed files with 546 additions and 490 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.signify.hue.reactivebleexample


import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity: FlutterActivity(){
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ 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
"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 Down Expand Up @@ -105,21 +105,21 @@ class PluginController {
private fun clearGattCache(call: MethodCall, result: Result) {
val args = pb.ClearGattCacheRequest.parseFrom(call.arguments as ByteArray)
bleClient.clearGattCache(args.deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
val info = pb.ClearGattCacheInfo.getDefaultInstance()
result.success(info.toByteArray())
},
{
val info = protoConverter.convertClearGattCacheError(
ClearGattCacheErrorType.UNKNOWN,
it.message
)
result.success(info.toByteArray())
}
)
.discard()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
val info = pb.ClearGattCacheInfo.getDefaultInstance()
result.success(info.toByteArray())
},
{
val info = protoConverter.convertClearGattCacheError(
ClearGattCacheErrorType.UNKNOWN,
it.message,
)
result.success(info.toByteArray())
},
)
.discard()
}

private fun disconnectFromDevice(call: MethodCall, result: Result) {
Expand All @@ -137,41 +137,45 @@ class PluginController {
val characteristicInstance = readCharMessage.characteristic.characteristicInstanceId.toInt()

bleClient.readCharacteristic(
deviceId,
characteristic,
characteristicInstance
deviceId,
characteristic,
characteristicInstance,
)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ charResult ->
when (charResult) {
is com.signify.hue.flutterreactiveble.ble.CharOperationSuccessful -> {
val charInfo = protoConverter.convertCharacteristicInfo(
readCharMessage.characteristic,
charResult.value.toByteArray()
)
charNotificationHandler.addSingleReadToStream(charInfo)
}
is com.signify.hue.flutterreactiveble.ble.CharOperationFailed -> {
protoConverter.convertCharacteristicError(readCharMessage.characteristic,
"Failed to connect")
charNotificationHandler.addSingleErrorToStream(
readCharMessage.characteristic,
charResult.errorMessage
)
}
}
},
{ throwable ->
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ charResult ->
when (charResult) {
is com.signify.hue.flutterreactiveble.ble.CharOperationSuccessful -> {
val charInfo = protoConverter.convertCharacteristicInfo(
readCharMessage.characteristic,
charResult.value.toByteArray(),
)
charNotificationHandler.addSingleReadToStream(charInfo)
}
is com.signify.hue.flutterreactiveble.ble.CharOperationFailed -> {
protoConverter.convertCharacteristicError(
readCharMessage.characteristic,
throwable.message)
readCharMessage.characteristic,
"Failed to connect",
)
charNotificationHandler.addSingleErrorToStream(
readCharMessage.characteristic,
throwable?.message ?: "Failure")
readCharMessage.characteristic,
charResult.errorMessage,
)
}
)
.discard()
}
},
{ throwable ->
protoConverter.convertCharacteristicError(
readCharMessage.characteristic,
throwable.message,
)
charNotificationHandler.addSingleErrorToStream(
readCharMessage.characteristic,
throwable?.message ?: "Failure",
)
},
)
.discard()
}

private fun writeCharacteristicWithResponse(call: MethodCall, result: Result) {
Expand All @@ -183,39 +187,54 @@ class PluginController {
}

private fun executeWriteAndPropagateResultToChannel(
call: MethodCall,
result: Result,
writeOperation: com.signify.hue.flutterreactiveble.ble.BleClient.(
deviceId: String,
characteristic: UUID,
characteristicInstanceId: Int,
value: ByteArray
) -> Single<com.signify.hue.flutterreactiveble.ble.CharOperationResult>
call: MethodCall,
result: Result,
writeOperation: com.signify.hue.flutterreactiveble.ble.BleClient.(
deviceId: String,
characteristic: UUID,
characteristicInstanceId: Int,
value: ByteArray,
) -> Single<com.signify.hue.flutterreactiveble.ble.CharOperationResult>,
) {
val writeCharMessage = pb.WriteCharacteristicRequest.parseFrom(call.arguments as ByteArray)
bleClient.writeOperation(writeCharMessage.characteristic.deviceId,
uuidConverter.uuidFromByteArray(writeCharMessage.characteristic.characteristicUuid.data.toByteArray()),
writeCharMessage.characteristic.characteristicInstanceId.toInt(),
writeCharMessage.value.toByteArray())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ operationResult ->
bleClient.writeOperation(
writeCharMessage.characteristic.deviceId,
uuidConverter.uuidFromByteArray(writeCharMessage.characteristic.characteristicUuid.data.toByteArray()),
writeCharMessage.characteristic.characteristicInstanceId.toInt(),
writeCharMessage.value.toByteArray(),
)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ operationResult ->
when (operationResult) {
is com.signify.hue.flutterreactiveble.ble.CharOperationSuccessful -> {
result.success(protoConverter.convertWriteCharacteristicInfo(writeCharMessage,
null).toByteArray())
result.success(
protoConverter.convertWriteCharacteristicInfo(
writeCharMessage,
null,
).toByteArray(),
)
}
is com.signify.hue.flutterreactiveble.ble.CharOperationFailed -> {
result.success(protoConverter.convertWriteCharacteristicInfo(writeCharMessage,
operationResult.errorMessage).toByteArray())
result.success(
protoConverter.convertWriteCharacteristicInfo(
writeCharMessage,
operationResult.errorMessage,
).toByteArray(),
)
}
}
},
{ throwable ->
result.success(protoConverter.convertWriteCharacteristicInfo(writeCharMessage,
throwable.message).toByteArray())
}
)
.discard()
{ throwable ->
result.success(
protoConverter.convertWriteCharacteristicInfo(
writeCharMessage,
throwable.message,
).toByteArray(),
)
},
)
.discard()
}

private fun readNotifications(call: MethodCall, result: Result) {
Expand All @@ -233,44 +252,63 @@ class PluginController {
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())
.subscribe({ mtuResult ->
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ mtuResult ->
result.success(protoConverter.convertNegotiateMtuInfo(mtuResult).toByteArray())
}, { throwable ->
result.success(protoConverter.convertNegotiateMtuInfo(com.signify.hue.flutterreactiveble.ble.MtuNegotiateFailed(request.deviceId,
throwable.message ?: "")).toByteArray())
}
)
.discard()
},
{ throwable ->
result.success(
protoConverter.convertNegotiateMtuInfo(
com.signify.hue.flutterreactiveble.ble.MtuNegotiateFailed(
request.deviceId,
throwable.message ?: "",
),
).toByteArray(),
)
},
)
.discard()
}

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

bleClient.requestConnectionPriority(request.deviceId, request.priority.toConnectionPriority())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ requestResult ->
result.success(protoConverter
.convertRequestConnectionPriorityInfo(requestResult).toByteArray())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ requestResult ->
result.success(
protoConverter
.convertRequestConnectionPriorityInfo(requestResult).toByteArray(),
)
},
{ throwable ->
result.success(
protoConverter.convertRequestConnectionPriorityInfo(
RequestConnectionPriorityFailed(
request.deviceId,
throwable?.message
?: "Unknown error",
),
).toByteArray(),
)
},
{ throwable ->
result.success(protoConverter.convertRequestConnectionPriorityInfo(
RequestConnectionPriorityFailed(request.deviceId, throwable?.message
?: "Unknown error")).toByteArray())
})
.discard()
)
.discard()
}

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

bleClient.discoverServices(request.deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ discoverResult ->
result.success(protoConverter.convertDiscoverServicesInfo(request.deviceId, discoverResult).toByteArray())
}, {
throwable -> result.error("service_discovery_failure", throwable.toString(), throwable.stackTrace.toList().toString())
})
.discard()
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ discoverResult ->
result.success(protoConverter.convertDiscoverServicesInfo(request.deviceId, discoverResult).toByteArray())
}, {
throwable ->
result.error("service_discovery_failure", throwable.toString(), throwable.stackTrace.toList().toString())
})
.discard()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ReactiveBlePlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
private fun initializePlugin(
messenger: BinaryMessenger,
context: Context,
plugin: ReactiveBlePlugin
plugin: ReactiveBlePlugin,
) {
val channel = MethodChannel(messenger, "flutter_reactive_ble_method")
channel.setMethodCallHandler(plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,26 @@ interface BleClient {
fun readCharacteristic(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int
characteristicInstanceId: Int,
): Single<CharOperationResult>
fun setupNotification(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int
characteristicInstanceId: Int,
): Observable<ByteArray>
fun writeCharacteristicWithResponse(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
value: ByteArray
value: ByteArray,
): Single<CharOperationResult>
fun writeCharacteristicWithoutResponse(
deviceId: String,
characteristicId: UUID,
characteristicInstanceId: Int,
value: ByteArray
value: ByteArray,
): Single<CharOperationResult>
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>
}
Loading

0 comments on commit 947912c

Please sign in to comment.