Broadcast receiver stops receiving after a while

G Gunnar Dagbjartsson 7 months 2 weeks ago
107 0 0

I'm working on a Zebra CC6000 scanner that should be listening constantly.

I wrote an app that listens to a broadcast receiver and it stops listening after a while.

I haven't found anything about that in the FAQ or docs or in the demo apps.

Here's my main code

package com.package.name
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.WindowManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.engine.FlutterEngine
import android.util.Log

class MainActivity: FlutterActivity() {
    private val CHANNEL = "com.package.name"
    private lateinit var scannerBroadcastReceiver: ScannerBroadcastReceiver
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        val channel =  MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL);
        scannerBroadcastReceiver = ScannerBroadcastReceiver(channel);
        val filter = IntentFilter()
        filter.addCategory(Intent.CATEGORY_DEFAULT)
        filter.addAction("com.dwbasicintent1.ACTION")
        registerReceiver(scannerBroadcastReceiver, filter)
    }
    override fun onDestroy() {
        unregisterReceiver(scannerBroadcastReceiver)
        super.onDestroy()
    }
}

class ScannerBroadcastReceiver(private val channel: MethodChannel) : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        when (intent?.action) {
            "com.dwbasicintent1.ACTION" -> {
                val decodedData = intent.getStringExtra("com.symbol.datawedge.data_string")

                channel.invokeMethod("onScan", decodedData)
            }
        }
    }
}

My Manifest only has 2 intent filters, no receiver registered there.

Do you have any tips for me?

 

CONTACT
Can’t find what you’re looking for?