LeandroSQ / android-ble-made-easy

An Android Library for handling Bluetooth Low Energy on Android Easy
MIT License
88 stars 29 forks source link

Instance BLE with compose #14

Closed JanissaryID closed 1 year ago

JanissaryID commented 2 years ago

how to use BLE with compose?

LeandroSQ commented 1 year ago

The README.md has been updated with instructions

rocketraman commented 1 year ago

How do I get ComponentActivity in a Composable?

rocketraman commented 1 year ago

It's not super-clear what to do here.

I've tried:

tailrec fun Context.getActivity(): AppCompatActivity? = when (this) {
  is AppCompatActivity -> this
  is Activity -> getActivity()
  is ContextWrapper -> baseContext.getActivity()
  else -> null
}

BLE(LocalContext.current.getActivity())

but that just results in:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 14836
    java.lang.IllegalStateException: LifecycleOwner com.myapp.MainActivity@183df6e is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
        at androidx.activity.result.ActivityResultRegistry.register(ActivityResultRegistry.java:123)
        at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:833)
        at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:842)
        at quevedo.soares.leandro.blemadeeasy.ContractHandler.<init>(ContractHandler.kt:21)
        at quevedo.soares.leandro.blemadeeasy.BLE.registerContracts(BLE.kt:159)
        at quevedo.soares.leandro.blemadeeasy.BLE.setup(BLE.kt:150)
        at quevedo.soares.leandro.blemadeeasy.BLE.<init>(BLE.kt:133)