Providing System Services through DI

1 week ago 11
ARTICLE AD BOX

I’m using Koin for dependency injection in an Android app, and I want to provide BluetoothManager through DI.

Right now, I have a module like this:

val bluetoothModule = module { single<BluetoothManager> { androidContext().getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager } }

What I’m unsure about is whether this is actually safe from an Android lifecycle / memory-leak perspective:

Could this approach cause any lifecycle problems, leaks, or "stale" references when Bluetooth is toggled on/off, or when the app goes to background/foreground?

Is holding BluetoothManager or BluetoothAdapter as Koin singles (i.e. app-wide singletons) okay, given that they come from getSystemService

Are there any other issues with this approach that I'm not aware of?

Read Entire Article