0

I am currently reading data from a Bluetooth Sensor, hence the data changes in real-time and continuously changes. I have stored the data in a variable: liveData:ByteArray

Now I am trying to send liveData from MainActivity to Sensordisplayfragment.

UPDATE

Based on @CTD's comment, this is what I have tried, unfortunately I do not have much knowledge on viewModel, and online research is just confusing as there seems to be many methods to implement a viewModel.

In my MainActivity class where variable liveData is stored:

val model:MyViewModel by viewModels()
    private fun processLiveData(liveData : ByteArray){
        livedata = liveData
        model.uploadData(livedata)
    }

In MyViewModel.class where the viewModel is at:

class MyViewModel: ViewModel() {
    private val realtimedata = MutableLiveData<ByteArray>()

    fun uploadData(data:ByteArray){
        realtimedata.value = data
    }

    fun loadData():LiveData<ByteArray>{
        return realtimedata
    }
}

Finally, in my Sensordisplay fragment where I am fetching the data:

val model:MyViewModel by viewModels()

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    model.loadData().observe(viewLifecycleOwner,Observer<ByteArray>{
        passandprocessLiveData(it)
    })
    return inflater.inflate(R.layout.sensordisplay, container, false)
}

override fun onResume(){
    activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    model.loadData().observe(viewLifecycleOwner,Observer<ByteArray>{
        passandprocessLiveData(it)
    })
    super.onResume()
}

fun passandprocessLiveData(data:Bytearray){
    //extract information from data and make 
    //cardviews move in realtime according to the extracted data
    }

Unfortunately,nothing is getting transferred and my cardviews are not moving. I can guarantee there is no error in the moving of the cardview codes. Anyone able to advice on what I can add? Apparently there is an init() function that I need to use.

Do Ji
  • 45
  • 1
  • 8
  • 2
    stored the data in liveData from ViewModel, activity and fragment observe same liveData. no need to send liveData. https://developer.android.com/topic/libraries/architecture/viewmodel – CTD Apr 21 '22 at 08:51
  • @CTD Hi, I have 2 questions regarding this method, hope you would be able to clarify. 1)For my use case where data needs to be updated based on the current values from the sensor, I should be using synchronous not asynchronous transfer right? 2)Also, what do you mean by no need to send liveData? – Do Ji Apr 22 '22 at 01:50
  • 1.Synchronize; 2. make sure your Activity and Fragment use the same ViewModel, use the 'by activityViewModels()' in fragment. your data will be shared in ViewModel, so you no need to send data from Activity to Fragment directly, call uploadData() method when you get new data from the sensor. – CTD Apr 22 '22 at 06:47
  • You answer is the simplest amongst all the other tutorials. It also works flawlessly, thanks so much! – Do Ji Apr 22 '22 at 08:12

1 Answers1

6
class MyViewModel : ViewModel() {
    private val realtimedata = MutableLiveData<ByteArray>()

    val sensorData: LiveData<ByteArray> = realtimedata

    fun update(data: ByteArray){
        realtimedata.value = data
    }
}

class MainActivity: Activity() {

    private val viewModel: MyViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        bluetoothSensorCallBack { data ->
            // Update the realtimedata 
            viewModel.update(data)
        }
    }
}

class SensordisplayFragment : Fragment() {

    // Use the 'by activityViewModels()' Kotlin property delegate
    // from the fragment-ktx artifact
    private val model: MyViewModel by activityViewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        model.sensorData.observe(viewLifecycleOwner, Observer<ByteArray> { data ->
            // Update the UI
        })
    }
}
CTD
  • 306
  • 3
  • 8