5

UPDATE:

Maybe it works now since the March 21, 2023 update, maybe not. You can read more about it in the docs if you want to use it.

Note again that we haven't tested if it works now. We had to quickly decide on how to get barcode scanning working again. So we refactored our custom barcode scanning Capacitor plugin to use com.google.mlkit:barcode-scanning:17.1.0 instead. More info about it here. This requires a bit more effort, because you have to implement the UI yourself and the fancy scan animation is gone, but it just works and it seems to be faster.


We are using com.google.android.gms:play-services-code-scanner:16.0.0-beta3 in combination with a custom Capacitor plugin for scanning barcodes like EAN-13 and EAN-8, but on certain devices, the activity seems to crash whenever the user tries to open the Google's barcode scanner UI.

Here is the error log from logcat:

win=Window{f8c052d u0 com.[redacted]/com.google.mlkit.vision.codescanner.internal.GmsBarcodeScanningDelegateActivity} destroySurfaces: appStopped=true cleanupOnResume=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=4 caller=com.android.server.wm.ActivityRecord.destroySurfaces:6529 com.android.server.wm.ActivityRecord.destroySurfaces:6510 com.android.server.wm.ActivityRecord.notifyAppStopped:6574 com.android.server.wm.ActivityRecord.activityStopped:7162 com.android.server.wm.ActivityClientController.activityStopped:258 android.app.IActivityClientController$Stub.onTransact:613 com.android.server.wm.ActivityClientController.onTransact:136

The code that handles the scanning UI:

package com.[redacted];

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.google.mlkit.vision.barcode.common.Barcode;
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning;

@CapacitorPlugin()
public class BarcodeScanner extends Plugin  {
    private void scan() {
        GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder()
                .setBarcodeFormats(Barcode.FORMAT_EAN_13, Barcode.FORMAT_EAN_8, Barcode.FORMAT_CODE_128, Barcode.FORMAT_ITF)
                .allowManualInput()
                .build();

        GmsBarcodeScanning
                .getClient(getContext(), options)
                .startScan()
                .addOnSuccessListener(this::onSuccess);
    }

    private void onSuccess(Barcode result) {
        JSObject jsObject = new JSObject();
        jsObject.put("content", result.getDisplayValue());
        jsObject.put("format", result.getFormat());

        this.getSavedCall().success(jsObject);
    }

    @PluginMethod
    public void startScan(PluginCall call) {
        this.saveCall(call);
        this.scan();
    }
}

The versions

    minSdkVersion = 23
    compileSdkVersion = 33
    targetSdkVersion = 33
    androidxActivityVersion = '1.6.1'
    androidxAppCompatVersion = '1.6.1'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.9.0'
    androidxFragmentVersion = '1.5.5'
    coreSplashScreenVersion = '1.0.0-rc01'
    androidxWebkitVersion = '1.6.0'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.5'
    androidxEspressoCoreVersion = '3.5.1'
    cordovaAndroidVersion = '10.1.1'

We have updated the Android Webview to the latest version, as well as any implementation package inside the app and Capacitor plugin's build.gradle files. We have no clue what causes the error, because the error description is very vague. We cannot find a solution anywhere. The error happens on a Galaxy Tab S7, where it has worked before. It suddenly stopped working, probably because of an internal update we don't know anything of. We have also tested with a Samsung Galaxy A51, but it just works on that device.

  • this is happening in Samsung S22 Ultra as well. It used to work. Suddenly it stopped working. Looks like Google ML Kit is not reliable. – Vinoth K Apr 11 '23 at 13:48
  • @VinothK It does also not work on the Samsung Galaxy A51 anymore. We had decided to refactor our custom barcode scanning Capacitor plugin to use `com.google.mlkit:barcode-scanning:17.1.0` instead. More info [here](https://developers.google.com/ml-kit/vision/barcode-scanning/android). This requires a bit more effort, because you have to implement the UI yourself and the fancy scan animation is gone, but it works perfectly fine and is faster than the one mentioned in my post. – fruityloops Apr 12 '23 at 18:10
  • did we find any solution I am seeing the issue in Samsung S22 – Sweety Bertilla May 15 '23 at 23:33
  • @SweetyBertilla The solution for us was to refactor into using `com.google.mlkit:barcode-scanning:17.1.0` – fruityloops May 17 '23 at 07:32
  • @fruityloops but this is different from google code scanner right, we have launch the camera, where as this would directly launch camera outside for us.. also google code scanner was faster when it worked – Sweety Bertilla May 25 '23 at 17:21
  • The same issue is occurring today. On a Samsung Galaxy M21. – Kitswas Jul 18 '23 at 06:50

2 Answers2

0

The solution that worked for me was to remove the updates from "Google play services"

Story of how I got to this: Yesterday I was testing the library with a project that exists on github and everything worked ok

Today I tried to do it directly from a new project (thanks to the repository that I used yesterday, I just met "com.google.android.gms:play-services-code-scanner") and I got the error that you posted and I couldn't find a solution (Samsung S20 Ultra), then a similar error came to mind with the "android webview" that made many apps unusable and the solution was to remove all updates and everything worked normally, so I tried the same to "Google play services "and it turns out that now if I open the camera.

I hope my solution will help you, which I think is not the cleanest for the end user.

  • This may work theoretically, but the end user's device is going to auto update anyway. Imo, it is best to refactor into using `com.google.mlkit:barcode-scanning:17.1.0` – fruityloops May 17 '23 at 07:35
0

I met similar problem because of reuse GmsBarcodeScanner. Most of time it works. But once it stops working, the phone has to be reset to factory.

The old code reuse GmsBarcodeScanner:

class HomeFragment : Fragment() {
    private var mQRScanner : GmsBarcodeScanner?           = null
    private var mQrCode    : String?                      = null
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val options = GmsBarcodeScannerOptions.Builder()
            .setBarcodeFormats( Barcode.FORMAT_QR_CODE )
            .build()
        mQRScanner = GmsBarcodeScanning.getClient(requireContext(), options)
        
        binding.bnQrCode.setOnClickListener{
            mQRScanner?.startScan()
                ?.addOnSuccessListener { barcode ->
                    mQrCode = barcode.rawValue                    
                }
        }
    }
}

the new code:

class HomeFragment : Fragment() {
    private var mQrCode    : String?                      = null
    
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.bnQrCode.setOnClickListener{
            val options = GmsBarcodeScannerOptions.Builder()
                .setBarcodeFormats( Barcode.FORMAT_QR_CODE )
                .build()
            val QRScanner = GmsBarcodeScanning.getClient( requireContext(), options )

            QRScanner.startScan()
                .addOnSuccessListener { barcode ->
                    mQrCode = barcode.rawValue
                }                
        }
    }
}
suc
  • 19
  • 6