Allow getting data through intent

Fix display not loading on create
This commit is contained in:
Kumi 2024-01-08 17:49:42 +01:00
parent 7325e43efb
commit ce6181b61d
2 changed files with 48 additions and 0 deletions

View file

@ -19,6 +19,7 @@
android:theme="@style/Theme.SimpleBLEReceiver">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="systems.kumi.simplerfiddemo.NEW_TAG" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

View file

@ -10,8 +10,10 @@ import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.Manifest
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.ParcelUuid
@ -63,6 +65,51 @@ class MainActivity : ComponentActivity() {
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
val intentFilter = IntentFilter("systems.kumi.simplerfiddemo.NEW_TAG")
registerReceiver(appReceiver, intentFilter)
setContent {
SimpleBLEReceiverTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column {
Text("Waiting for data...")
Button(onClick = { emitIntent("sample-id") }) {
Text("Emit sample intent")
}
}
}
}
}
}
private val appReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val data = intent?.getStringExtra("data")
if (data != null) {
runOnUiThread {
setContent {
SimpleBLEReceiverTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column {
Text("Received data: $data (from intent)")
Button(onClick = { emitIntent("sample-id") }) {
Text("Emit sample data")
}
}
}
}
}
}
}
}
}
private fun startScanning() {