简体   繁体   English

蓝牙 SPP + Android MVVM + 协程

[英]Bluetooth SPP + Android MVVM + Coroutines

I'm creating an app to communicate with an external device through bluetooth, the connection is with serial protocol (rfcomm).我正在创建一个应用程序来通过蓝牙与外部设备通信,连接是使用串行协议(rfcomm)。 Fortunately I found a good lib on GitHub named BlueFlow written completely in Kotlin and coroutines.幸运的是,我在 GitHub 上找到了一个很好的库,名为BlueFlow ,完全用 Kotlin 和协程编写。 I want to implement the MVVM pattern suggested in the android dev portal but I can't figure which is the right role of the bluetooth.我想实现 android 开发门户中建议的 MVVM 模式,但我不知道哪个是蓝牙的正确角色。

I have a base activity and a fragment where I want to manage the ui components and I want to update the text fields of the ui using data binding.我有一个基本活动和一个片段,我想在其中管理 ui 组件,并且我想使用数据绑定更新 ui 的文本字段。 I think I need to create a variable in the ViewModel of type LiveData and Observe it in the fragment for changes.我想我需要在 LiveData 类型的 ViewModel 中创建一个变量,并在片段中观察它以进行更改。

The bluetooth library has a singleton class that I instantiate in the ViewModel and I need to pass it a context.蓝牙库有一个 singleton class 我在 ViewModel 中实例化,我需要向它传递一个上下文。 The bluetooth class has a function for read incoming data "readByteArray" and it returns a Flow<ByteArray> .蓝牙 class 有一个 function 用于读取传入数据“readByteArray”,它返回一个Flow<ByteArray> I suppose this is the "Remote Data Source" in the MVVM architecture, am I right?我想这是 MVVM 架构中的“远程数据源”,对吗? Then I need to build a repository on top of it.然后我需要在它之上构建一个存储库。 Here there's the first stumbling block, how can I use the function readByteArray here?这里有第一个绊脚石,我如何在这里使用 function readByteArray? I can not use the singleton without pass a context and I think is not good to use context in this part of the architecture.我不能在没有传递上下文的情况下使用 singleton,我认为在这部分架构中使用上下文并不好。 I also wrote a model class for the data received and it's like:我还为收到的数据写了一个 model class ,它就像:

    @Parcelize
data class IncomingResult(
        @SerializedName("battery")
        val battery: Int,
        @SerializedName("sensor_one")
        val sensorOne: Int,
        @SerializedName("sensor_two")
        val sensorTwo: Int,
): Parcelable

I need this class because sometimes I have to save these data into a room database.我需要这个 class 因为有时我必须将这些数据保存到房间数据库中。

I really appreciate any help/suggestion.我非常感谢任何帮助/建议。 I'm struggling with this problem from a week without find a solution.我从一个星期以来一直在努力解决这个问题,但没有找到解决方案。 This is how I thought MVVM should be implemented in my project but I'm not sure it's correct:这就是我认为应该在我的项目中实现 MVVM 的方式,但我不确定它是否正确:

            Fragment
                |
                |
            ViewModel
                |
                |
            Repository
                |
    __________  |   _________
    |                       |
    |                       |
Room Database           Bluetooth 
                        incoming data

You don't need LiveData everywhere, Only use it if you have to observe it or update the UI continuously like you want to use it in the variable when you have to take live data from the UI like editText.您不需要到处使用 LiveData,只有当您必须从 UI 中获取实时数据(如 editText)时,才需要观察它或不断更新 UI,就像您想在变量中使用它一样。 You can use liveData everywhere in your project, it is up to you whether you want to use it or not, I recommend that you have to look for the necessity when choosing it.你可以在你的项目中到处使用liveData,你要不要使用它取决于你,我建议你在选择它时要寻找必要性。

You are correct about the readByteArray as it is a source of data that we are using, so it is considered as a repository in MVVM.您对 readByteArray 的看法是正确的,因为它是我们正在使用的数据源,因此它被视为 MVVM 中的存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM