Thread
#compose
    j

    Jason Inbody

    1 year ago
    I'm trying to integrate a bluetooth accessory into my compose app. The sample code is written in java and utilizes a ServiceConnection and a ServiceListener. Should I use a view model to house the service listener? Or should I just try to kotlin-fy the java code and run it in the background as a service connection + listener. The service listerner will need to update my composable view through out the bluetooth connection process.
    For SwiftUI I used an Observable Object singleton to house the code + callbacks but the structure for android is a little different...
    d

    darkmoon_uk

    1 year ago
    TBH This sounds more like a general question of 'should I structure my code into layers' more than anything specifically about Bluetooth Accessories. If what you're looking for to drive the Bluetooth service is lifecycle callbacks; then yes you can get this out of Compose (see sth like
    DisposableEffect
    /
    LaunchedEffect
    ). Even if you put this inside a ViewModel though you'd still need that ViewModel to be receiving lifecycle callbacks. A ViewModel after all is just a notional, organisation unit for your application code. I would strongly recommend you to take that opportunity to layer your code with a logical ViewModel separate to the View though. Frankly speaking; I hate maintaining or dealing with applications where all the logic is written in-line with the View. They turn to spaghetti and/or blow-out in size very quickly.