-1

I've an activity where I receive data via BLE and display it (ControlActivity). There's a second activity where I want to display the received data in a different way (OnlineActivity).

In ControlActivity there's a method that get's called when data is received via BLE:

private fun onNotificationReceived(bytes: ByteArray){
    if(bytes[0] == 0x01){
        //handle with ControlActivity
    }
    else if(bytes[0] == 0x02){
        //handle with OnlineActivity
        //startedinstanceofOnlineActivity?.handleData(bytes)
    }
}

Data comes in about every second periodically.

There's a button in the ControlActivity which should start the OnlineActivity:

control_online.setOnClickListener {
    val intent = Intent(this,OnlineActivity::class.java)
    startActivity(intent)
}

To call a method of OnlineActivity from ControlActivity I would need the Instance of OnlineActivity that is created/started here. The method in OnlineActivity should change a few textViews in OnlineActivity based on the data from ControlActivity.

onNotificationReceived gets also called continuously, when OnlineActivity is active.

I just need a "quick and dirty"-solution for my problem, as this is just a very basic test-app for myself. I don't want to invest too much time with TabViews, etc.. I'm just an embedded software-engineer who wants to display his received data in any way for testing purposes. I never developed an app before.

Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33

1 Answers1

0

for a quick solution, You can declare a static variables for data that You received in onNotificationReceived of ControlActivity

To crate a static variables, You will define those variables inside companion object

And you can get that data via class name like ControlActivity.foo inside your OnlineActivity (where foo is the static variable inside ControlActivity)

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • Thank you, that really helped me! I totally forgot about the static keyword, since it's a bit different in non OOP. – Florian Moser Feb 08 '21 at 09:31
  • 1
    this can cause leaks. you can use singleton class if need be – Raghunandan Feb 08 '21 at 09:32
  • @Raghunandan I've actually done it like that. I've added a class called `MessageHandler`. `ControlActivity` has a single instance inside companion object. `OnlineActivity` gets that instance via `ControlActivity.msghdl`. – Florian Moser Feb 08 '21 at 09:36