androidkotlinbluetooth

I want to assign multiple contexts based on seperate features in kotlin


so I'm building this app that is supposed to facilitate master slave relationship over bluetooth by which it gets data from the sensors sends it to master, logs it, shows it...etc I thought it might be a good idea to have a context for each feature (at least the main ones like sensor management and Bluetooth), My idea is that this will facilitate multi-threading and help make this a cleaner program I guess (easier to keep separate as well) i was reading the documentation didn't find much help

val btcontext : Context = public Context()

tried this in mainActivity no luck, um also the contexts are parameters of course to the classes that are going to be used I'm a tad bit chaotic I guess, I hope I explained it well

@SuppressLint("MissingPermission")
class BTController (private val context: Context, private val bluetoothAdapter: BluetoothAdapter?)
    : BTMenu {

some context btw im running this for 9+ android


Solution

  • If you're writing code within your MainActivity, you can use the term this, because it serves as your Context. This technique works in any class that extends an Activity.

    For example:

    BTController(
      context = this,
      bluetoothAdapter = yourAdapter
    )